标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21384 Accepted Submission(s): 5180
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <iostream> 5 #include <queue> 6 using namespace std; 7 int dx[]={0,0,-1,1},dy[]={1,-1,0,0}; 8 char map[102][102]; 9 int ans[102][102]; 10 int m,n,k; 11 int x01,x02,y01,y02; 12 struct node 13 { 14 int x,y; 15 int dix,diy; 16 int num; 17 }; 18 bool bfs() 19 { 20 int i,j; 21 queue<node> Q; 22 node p,u; 23 p.x=y01,p.y=x01; 24 p.num=-1; 25 p.dix=p.diy=-1; 26 Q.push(p); 27 while(!Q.empty()) 28 { 29 p=Q.front(); 30 Q.pop(); 31 if(p.num>ans[p.x][p.y]||p.num>k) 32 continue; 33 else 34 ans[p.x][p.y]=p.num; 35 if(p.x==y02&&p.y==x02) 36 return 1; 37 for(i=0;i<4;i++) 38 { 39 u.x=p.x+dx[i]; 40 u.y=p.y+dy[i]; 41 u.dix=dx[i],u.diy=dy[i]; 42 u.num=p.num+1; 43 if(u.x<1||u.y<1||u.x>m||u.y>n||map[u.x][u.y]==‘*‘) 44 continue; 45 if(dx[i]==p.dix&&dy[i]==p.diy) 46 { 47 u.num--; 48 Q.push(u); 49 } 50 else 51 Q.push(u); 52 } 53 } 54 return 0; 55 } 56 int main() 57 { 58 int t,i,j; 59 freopen("in.txt","r",stdin); 60 scanf("%d",&t); 61 while(t--) 62 { 63 scanf("%d%d",&m,&n); 64 getchar(); 65 for(i=1;i<=m;i++) 66 { 67 for(j=1;j<=n;j++) 68 scanf("%c",&map[i][j]); 69 getchar(); 70 } 71 scanf("%d%d%d%d%d",&k,&x01,&y01,&x02,&y02); 72 for(i=1;i<=m;i++) 73 for(j=1;j<=n;j++) 74 ans[i][j]=15; 75 if(bfs()) 76 printf("yes\n"); 77 else 78 printf("no\n"); 79 } 80 return 0; 81 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5303634.html