Problem 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
题意:在一个图中,有一只驴和一只老虎。分别给定他们的起点与方向,如果不能继续前走,驴向右转如还是不能走则停在原地,老虎向右走,还是不能走也停在原地。判断相遇的位置,不能相遇则输出-1
题解:因为都是同时的,故可以采用递归的方法,也可直接模拟求解!
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int flag[1005][1005]; int pos[1005][1005]; int dir[4][2]={ {0,1},{1,0},{0,-1},{-1,0}}; int n,p1,p2,f1,f2,biaoji; void dfs(int x1,int y1,int d1,int x2,int y2,int d2) { int xa,ya,xb,yb,dm,dn; flag[x1][y1]=1; pos[x2][y2]=1; if(biaoji) return; if(x1==x2&&y1==y2) {biaoji=1;printf("%d %d\n",x1,y1);return;} if(f1&&f2) {biaoji=1;printf("-1\n");return;} if(f1){ xa=x1;ya=y1;dm=d1;} else { xa=x1+dir[d1][0]; ya=y1+dir[d1][1]; dm=d1; if(flag[xa][ya]==1||xa<0||xa>=n||ya<0||ya>=n) { dm=(d1+1)%4; xa=x1+dir[dm][0]; ya=y1+dir[dm][1]; if(flag[xa][ya]==1||xa<0||xa>=n||ya<0||ya>=n) {f1=1;xa=x1;ya=y1;} } } if(f2) {xb=x2;yb=y2;dn=d2;} else { xb=x2+dir[d2][0]; yb=y2+dir[d2][1]; dn=d2; if(pos[xb][yb]==1||xb<0||xb>=n||yb<0||yb>=n) { dn=(d2+3)%4; xb=x2+dir[dn][0]; yb=y2+dir[dn][1]; if(pos[xb][yb]==1||xb<0||xb>=n||yb<0||yb>=n) {f2=1;xb=x2;yb=y2;} } } dfs(xa,ya,dm,xb,yb,dn); } int main() { int x1,y1,d1,x2,y2,d2; while(scanf("%d",&n),n) { scanf("%d %d %d",&x1,&y1,&d1); scanf("%d %d %d",&x2,&y2,&d2); if(x1==x2&&y1==y2) printf("%d %d\n",x1,y1); else { memset(flag,0,sizeof(flag)); memset(pos,0,sizeof(pos)); f1=0;f2=0; biaoji=0; dfs(x1,y1,d1,x2,y2,d2); } } return 0; }
非递归写法
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; int n,f1,f2,x1,y1,d1,x2,y2,d2,pos; int flg1[1005][1005]; int flg2[1005][1005]; int dirc[4][2]={ {0,1},{1,0},{0,-1},{-1,0}}; void doit() { int xa,ya,dir1,xb,yb,dir2; xa=x1,ya=y1,dir1=d1; xb=x2,yb=y2,dir2=d2; while(1) { if(xa==xb&&ya==yb) { printf("%d %d\n",xa,ya);break;} if(f1&&f2){printf("-1\n");break;} int tempx=xa; int tempy=ya; int tpx=xb; int tpy=yb; flg1[xa][ya]=1; flg2[xb][yb]=1; if(f1==0) { xa=xa+dirc[dir1][0]; ya=ya+dirc[dir1][1]; if(flg1[xa][ya]==1||xa<0||ya>=n||xa>=n||ya<0) { dir1=(dir1+1)%4; xa=tempx+dirc[dir1][0]; ya=tempy+dirc[dir1][1]; if(flg1[xa][ya]==1||xa<0||ya>=n||xa>=n||ya<0) { f1=1; xa=tempx; ya=tempy; } } } if(f2==0) { xb=xb+dirc[dir2][0]; yb=yb+dirc[dir2][1]; if(flg2[xb][yb]==1||xb<0||yb>=n||xb>=n||yb<0) { dir2=(dir2+3)%4; xb=tpx+dirc[dir2][0]; yb=tpy+dirc[dir2][1]; if(flg2[xb][yb]==1||xb<0||xb>=n||yb>=n||yb<0) { f2=1; xb=tpx; yb=tpy; } } } } } int main() { while(scanf("%d",&n),n) { scanf("%d %d %d",&x1,&y1,&d1); scanf("%d %d %d",&x2,&y2,&d2); f1=0; f2=0; memset(flg1,0,sizeof(flg1)); memset(flg2,0,sizeof(flg2)); doit(); } return 0; }