HDU 1728 搜索
2015-04-22 18:24
120 查看
MLE.......本来是想拿水题找找自信的,,,结果,,,,QAQ!!!!!!!!!
AC代码:
刚开始做的时候超时了,本来想用A*做,但是发现还是超时了,,也许A*在这里并不是很实用,因为这里优先级第一的是拐弯的次数,所以步数是次之的,可能出现步数少但是拐弯次数多的情况(真的吗= = )恩。。。。
#include <stdio.h> #include <iostream> #include <queue> #include <string.h> using namespace std; #define maxn 110 char 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 110 char 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*在这里并不是很实用,因为这里优先级第一的是拐弯的次数,所以步数是次之的,可能出现步数少但是拐弯次数多的情况(真的吗= = )恩。。。。
相关文章推荐
- ACM 搜索 HDU 1728 搜索迷宫
- HDU 1728 逃离迷宫 (搜索)
- hdu 1728 搜索求最少的转向次数
- 搜索专题(DFS&&BFS&&剪枝)HDU 1728-逃离迷宫
- 逃离迷宫 HDU - 1728 深度优先搜索
- HDU 1728 逃离迷宫(广度搜索)
- hdu 1728 逃离迷宫(BFS+稍微改变一下搜索步骤)
- hdu 1728 逃离迷宫(搜索:BFS+优先队列)
- 逃离迷宫 HDU-1728 BFS深度搜索
- Hduoj1728【搜索+DP】
- hdu 1728 搜索求最少的转向次数
- hdu 1728(搜索)
- HDU 1728 搜索
- 【搜索】 HDU 4770 Lights Against Dudely
- HDU 1401 Solitaire(双向搜索)
- HDU 1428 漫步校园 (搜索 + dp)
- hdu 3639 有向图缩点+建反向图+搜索
- HDU 2553 N皇后问题(递归搜索)
- 简单搜索练习(一)二分入门 HDU 2141 Can you find it?POJ 2785 4 Value whose Sum is 0
- HDU 1728 逃离迷宫