标签:des style blog http io ar color os sp
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19491 Accepted Submission(s): 5054
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<queue> 7 using namespace std; 8 int map[1005][1005]; 9 int vis[1005][1005]; 10 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 11 int n,m; 12 int x1i,y1j,x2i,y2j; 13 int flag; 14 struct ln{ 15 int x; 16 int y; 17 int step; 18 }abc,q; 19 20 void bfs() 21 { 22 int i; 23 queue<struct ln>p; 24 memset(vis,0,sizeof(vis)); 25 abc.x=x1i; 26 abc.y=y1j; 27 abc.step=-1; 28 vis[abc.x][abc.y]=1; 29 p.push(abc); 30 while(!p.empty()) 31 { 32 abc=p.front(); 33 p.pop(); 34 // if( abc.x==x2i && abc.y==y2j )//判断条件 35 // { 36 // if(abc.step<=2)//放里面 37 // flag=1; 38 // return ; 39 // } 40 abc.step++;//每次取值都代表上一个值的四周都已遍历所以++代表一次转弯 41 42 for(i=0;i<4;i++) 43 { 44 q.x=abc.x+dir[i][0]; 45 q.y=abc.y+dir[i][1]; 46 q.step=abc.step; 47 while(q.x>=1&&q.x<=n &&q.y>=1&&q.y<=m &&!vis[q.x][q.y]) 48 { 49 if(map[q.x][q.y]!=0)//当map不为0时 50 { 51 if(q.x!=x2i || q.y!=y2j)//当不是终点时,标记走过 52 { 53 vis[q.x][q.y]=1; 54 break; 55 } 56 if(q.x==x2i && q.y==y2j &&q.step<=2)//当时终点时,判断转弯数 57 { 58 flag=1; 59 return ; 60 } 61 } 62 vis[q.x][q.y]=1;//其他为0的情况 63 p.push(q); 64 q.x+=dir[i][0]; 65 q.y+=dir[i][1]; 66 } 67 } 68 } 69 return ; 70 } 71 72 int main() 73 { 74 //freopen("in.txt","r",stdin); 75 while(~scanf("%d%d",&n,&m)) 76 { 77 if(!n&&!m) 78 break; 79 int i,j; 80 for(i=1;i<=n;i++) 81 { 82 for(j=1;j<=m;j++) 83 { 84 scanf("%d",&map[i][j]); 85 } 86 } 87 int q; 88 scanf("%d",&q); 89 for(i=1;i<=q;i++) 90 { 91 flag=0; 92 scanf("%d%d%d%d",&x1i,&y1j,&x2i,&y2j); 93 if(map[x1i][y1j]!=map[x2i][y2j] || map[x1i][y1j]==0 || map[x2i][y2j]==0) 94 { 95 printf("NO\n"); 96 continue; 97 } 98 99 bfs(); 100 101 if(flag==1) 102 printf("YES\n"); 103 else 104 printf("NO\n"); 105 106 } 107 108 109 } 110 return 0; 111 }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/xuesen1995/p/4120018.html