The Donkey of Gui Zhou
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4740
Description
There was no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could be considered as an N×N grid. The coordinates of the up-left cell is (0,0) , the down-right cell is (N-1,N-1) and the cell below the up-left cell is (1,0)..... A 4×4 grid is shown below:
The donkey lived happily until it saw a tiger far away. The donkey had never seen a tiger ,and the tiger had never seen a donkey. Both of them were frightened and wanted to escape from each other. So they started running fast. Because they were scared, they were running in a way that didn't make any sense. Each step they moved to the next cell in their running direction, but they couldn't get out of the forest. And because they both wanted to go to new places, the donkey would never stepped into a cell which had already been visited by itself, and the tiger acted the same way. Both the donkey and the tiger ran in a random direction at the beginning and they always had the same speed. They would not change their directions until they couldn't run straight ahead any more. If they couldn't go ahead any more ,they changed their directions immediately. When changing direction, the donkey always turned right and the tiger always turned left. If they made a turn and still couldn't go ahead, they would stop running and stayed where they were, without trying to make another turn. Now given their starting positions and directions, please count whether they would meet in a cell.
Input
There are several test cases.
In each test case:
First line is an integer N, meaning that the forest is a N×N grid.
The second line contains three integers R, C and D, meaning that the donkey is in the cell (R,C) when they started running, and it's original direction is D. D can be 0, 1, 2 or 3. 0 means east, 1 means south , 2 means west, and 3 means north.
The third line has the same format and meaning as the second line, but it is for the tiger.
The input ends with N = 0. ( 2 <= N <= 1000, 0 <= R, C < N)
Output
For each test case, if the donkey and the tiger would meet in a cell, print the coordinate of the cell where they meet first time. If they would never meet, print -1 instead.
Sample Input
2
0 0 0
0 1 2
4
0 1 0
3 2 0
0
Sample Output
-1
1 3
HINT
题意
驴和老虎在n*n的格子里面跑呀跑,驴遇到障碍或者自己走过的路,就会往右转,而老虎往左转,问你最早在哪儿相遇。
注意,如果转一次还是不能走的话,就会停下来
题解:
啊,读懂题,用bfs搞一搞就好了……
模拟每一步
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) const int maxn=2501; #define mod 1000000009 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** int dy[4]={1,0,-1,0}; int dx[4]={0,1,0,-1};//这是驴++,老虎-- int vis1[1100][1100]; int vis2[1100][1100]; int main() { int n; while(scanf("%d",&n)!=EOF) { if(n==0) break; memset(vis1,0,sizeof(vis1)); memset(vis2,0,sizeof(vis2)); int x=read(),y=read(),z=read(); int xx=read(),yy=read(),zz=read(); int flag=0; int flag1=1,flag2=1; int flag11=1,flag22=1; int tt=0; while(tt<5) { flag1=0,flag2=0; vis1[x][y]=1; vis2[xx][yy]=1; if(x==xx&&y==yy) { printf("%d %d\n",x,y); flag=1; break; } int nowx=x,nowy=y,nowz=z; for(int i=0;i<2;i++) { if(flag11==0) break; int nextx=nowx,nexty=nowy,nextz=nowz; nextz=(nowz+i+4)%4; nextx+=dx[nextz]; nexty+=dy[nextz]; if(nextx<0||nextx>=n) continue; if(nexty<0||nexty>=n) continue; if(vis1[nextx][nexty]) continue; flag1=1; x=nextx,y=nexty,z=nextz; break; } nowx=xx,nowy=yy,nowz=zz; for(int i=0;i<2;i++) { if(flag22==0) break; int nextx=nowx,nexty=nowy,nextz=nowz; nextz=(nowz-i+4)%4; nextx+=dx[nextz]; nexty+=dy[nextz]; if(nextx<0||nextx>=n) continue; if(nexty<0||nexty>=n) continue; if(vis2[nextx][nexty]) continue; flag2=1; xx=nextx,yy=nexty,zz=nextz; break; } if(flag1==0&&flag2==0) tt++; if(flag1==0) flag11=0; if(flag2==0) flag22=0; } if(!flag) printf("-1\n"); } }