HDU 1728 搜索
来源:互联网 发布:hr如何面试java程序员 编辑:程序博客网 时间:2024/06/10 14:36
MLE.......本来是想拿水题找找自信的,,,结果,,,,QAQ!!!!!!!!!
#include <stdio.h>#include <iostream>#include <queue>#include <string.h>using namespace std;#define maxn 110char map[maxn][maxn];int vis[maxn][maxn];int cmpx[4]={0,0,1,-1};int cmpy[4]={-1,1,0,0};int n,m;class node{public: int x,y,k,pos; node(){k=pos=0;}};int check(int xx,int yy){ if(xx<0||yy<0||xx>=m||yy>=n) return 0; if(map[xx][yy]=='*'||vis[xx][yy]) return 0; return 1;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); int i; memset(vis,0,sizeof(vis)); for(i=0;i<m;i++) { scanf("%s",map[i]);//printf("%s\n",map[i]); } int turn,sty,stx,enx,eny; scanf("%d%d%d%d%d",&turn,&sty,&stx,&eny,&enx); sty--,stx--,eny--,enx--; node init;init.x=stx,init.y=sty;init.k=-1,init.pos=-1; queue<node>que; que.push(init); int flag=0; while(!que.empty()) { node pre=que.front(); que.pop(); vis[pre.x][pre.y]=1; if(pre.x==enx&&pre.y==eny) {flag=1;break;} for(i=0;i<4;i++) { node tmp=pre; tmp.x+=cmpx[i],tmp.y+=cmpy[i]; if(check(tmp.x,tmp.y)) { if(i!=pre.pos) tmp.k++; tmp.pos=i; if(tmp.k>turn) continue; que.push(tmp); } } } if(flag) cout<<"yes"<<endl;else cout<<"no"<<endl; } return 0;}
AC代码:
#include <stdio.h>#include <iostream>#include <queue>#include <string.h>using namespace std;#define maxn 110char map[maxn][maxn];int vis[maxn][maxn];int cmpx[4]={0,0,1,-1};int cmpy[4]={-1,1,0,0};int n,m;class node{public: int x,y,k,pos; node(){k=pos=0;}};int check(int xx,int yy){ if(xx<0||yy<0||xx>=m||yy>=n) return 0; if(map[xx][yy]=='*') return 0; return 1;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); int i; memset(vis,-1,sizeof(vis)); for(i=0;i<m;i++) { scanf("%s",map[i]);//printf("%s\n",map[i]); } int turn,sty,stx,enx,eny; scanf("%d%d%d%d%d",&turn,&sty,&stx,&eny,&enx); sty--,stx--,eny--,enx--; node init;init.x=stx,init.y=sty;init.k=-1,init.pos=-1; vis[init.x][init.y]=0; queue<node>que; que.push(init); int flag=0; while(!que.empty()) { node pre=que.front(); que.pop(); if(pre.x==enx&&pre.y==eny) {flag=1;break;} for(i=0;i<4;i++) { node tmp=pre; tmp.x+=cmpx[i],tmp.y+=cmpy[i]; if(check(tmp.x,tmp.y)) { if(i!=pre.pos) tmp.k=pre.k+1; tmp.pos=i; if(tmp.k>turn) continue; if(vis[tmp.x][tmp.y]==-1) vis[tmp.x][tmp.y]=tmp.k; else { if(vis[tmp.x][tmp.y]>=tmp.k) vis[tmp.x][tmp.y]=tmp.k; //就因为一个等于号!!!!!!坑了我好久!!!! else continue; } que.push(tmp); } } } if(flag) cout<<"yes"<<endl;else cout<<"no"<<endl; } return 0;}
刚开始做的时候超时了,本来想用A*做,但是发现还是超时了,,也许A*在这里并不是很实用,因为这里优先级第一的是拐弯的次数,所以步数是次之的,可能出现步数少但是拐弯次数多的情况(真的吗= = )恩。。。。
0 0
- HDU 1728 搜索
- HDU 1728 搜索
- ACM 搜索 HDU 1728 搜索迷宫
- hdu搜索
- hdu搜索
- HDU 1728 逃离迷宫 (搜索)
- HDU 1728 逃离迷宫(广度搜索)
- 逃离迷宫 HDU-1728 BFS深度搜索
- HDU 1704 (搜索题)
- HDU 4016 搜索
- HDU 1181 搜索
- HDU-1128 暴力搜索
- 搜索 hdu 1238
- hdu 3786 搜索
- HDU 1430 魔板 搜索
- HDU-1548(BFS搜索)
- 搜索hdu 1240
- HDU 4536 状态搜索
- MyEclipse常用快捷键
- 魔方
- 单列选择器 UIPickView
- viewDidLoad
- repo用法详解
- HDU 1728 搜索
- 【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0、OpenCV 2.4.8、OpenCV 2.4.9 +VS 开发环境配置
- Hbase配置项粗解(1)
- 14-使用自定义的高亮标签和搜索title和content中包含搜索关键字的内容
- 01背包 背包容量大时新解
- 【OpenCV入门教程之二】 一览众山小:OpenCV 2.4.8 or OpenCV 2.4.9组件结构全解析
- oracle 游标使用全解【转】
- DLX_HDU_2828
- Hbase配置项粗解(2)