标签:bfs
bfs
搞清楚是转弯而不是步数。所以需要一个方向一直走下去直到边界或者墙。
还有就是注意题意。给出起点终点的 x,y 位置是交换的。 题目是下标1开始。注意。
#include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<vector> #include<cmath> #define INF 0x7fffffff #define eps 1e-8 #define LL long long #define PI 3.141592654 #define CLR(a,b) memset(a,b,sizeof(a)) #define FOR(i,a,n) for(int i= a;i< n ;i++) #define FOR0(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mp make_pair #define ft first #define sd second #define sf scanf #define pf printf #define acfun std::ios::sync_with_stdio(false) #define SIZE 100+1 using namespace std; char g[SIZE][SIZE]; struct lx { int x,y,t; void init(int xx,int yy,int tt) { x=xx,y=yy,t=tt; } }start,thend; int n,m; int xx[]= {0,0,-1,1}; int yy[]= {-1,1,0,0}; //2 //5 5 //..... //..... //.**.* //..*.* //..... //2 4 4 2 4 void bfs() { bool vis[SIZE][SIZE]; CLR(vis,0); queue<lx>q; vis[start.x][start.y]=1; q.push(start); while(!q.empty()) { lx tmp=q.front(); q.pop(); // pf("%d %d t=%d\n",tmp.x,tmp.y,tmp.t); // system("pause"); if(tmp.x==thend.x&&tmp.y==thend.y&&tmp.t<=thend.t) { puts("yes"); return; } if(tmp.t>thend.t)continue; FOR(k,0,4) { lx now; int x=tmp.x+xx[k]; int y=tmp.y+yy[k]; while(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='.') { if(!vis[x][y]) { now.init(x,y,tmp.t+1); vis[x][y]=1; q.push(now); } x+=xx[k]; y+=yy[k]; } } } puts("no"); } int main() { int tt; sf("%d",&tt); while(tt--) { sf("%d%d",&n,&m); char str[SIZE]; FOR(i,0,n) sf("%s",g[i]); int x,y,t; sf("%d%d%d",&t,&y,&x);start.init(x-1,y-1,-1); sf("%d%d",&y,&x);thend.init(x-1,y-1,t); //pf("%d %d == %d %d\n",start.x,start.y,thend.x,thend.y); bfs(); } }
标签:bfs
原文地址:http://blog.csdn.net/dongshimou/article/details/40780423