标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22233 Accepted Submission(s): 5413
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> #include<queue> #include<iostream> using namespace std; typedef long long LL; char graph[105][105]; int n,m,cnt; struct Node{ int x,y; int step; }; Node s,t; bool vis[105][105]; bool check(int x,int y){ if(x<0||x>=m||y<0||y>=n||graph[x][y]==‘*‘) return false; return true; } int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}}; bool bfs(){ memset(vis,false,sizeof(vis)); queue<Node> q; q.push(s); vis[s.x][s.y] = true; s.step = 0; while(!q.empty()){ Node now = q.front(); q.pop(); if(now.step>cnt) return false; if(now.x==t.x&&now.y==t.y) return true; Node next; for(int i=0;i<4;i++){ next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; next.step = now.step+1; while(check(next.x,next.y)){ if(next.x==t.x&&next.y==t.y) return true; if(vis[next.x][next.y]==false){ ///没访问过进入队列 vis[next.x][next.y]=true; q.push(next); } next.x+=dir[i][0]; ///笔直走向下一个点 next.y+=dir[i][1]; } } } return false; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d%d",&m,&n); for(int i=0;i<m;i++){ scanf("%s",&graph[i]); } scanf("%d%d%d%d%d",&cnt,&s.y,&s.x,&t.y,&t.x); ///莫名的坑 s.x-=1,s.y-=1,t.x-=1,t.y-=1; bool flag = bfs(); if(flag) printf("yes\n"); else printf("no\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5573340.html