Hdu 4740 The donkey of Gui Zhou BFS

Source: Internet
Author: User

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=4740

Description

There is no donkey in the province of Gui Zhou, China. A trouble maker shipped one and put it in the forest which could is considered as an nxn grid. The coordinates of the Up-left cell is (0,0), the Down-right cell was (n-1,n-1) and the cell below the Up-left cell is (1, 0) ..... A 4x4 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 of the other. So they started running fast. Because They were scared, they were running in a-on-the-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 visite D by itself, and the tiger acted the same. Both the donkey and the Tiger ran in a random direction at the beginning and they all 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 is several test cases.

In each test case:
First line was an integer N, meaning the forest is a nxn grid.

The second line contains three integers R, C and D, meaning the donkey are in the cell (r,c) when they started running , and it's original direction are 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 had the same format and meaning as the second line, but it was for the tiger.

The input ends with N = 0. (2 <= N <=, 0 <= R, C < N)

Output

For all test case, if the donkey and the Tiger would meet in a cell, print the coordinate of the cell where they meet fir St 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

Test instructions

The donkey and the Tiger run in the N*n lattice, the donkey encounters the obstacle or the road which oneself passes, will turn to the right, but the tiger turns left, asks you where first met.

Note that if you do not go again, you will stop.

Exercises

Ah, read the problem, with BFS to do a good job ...

Simulate every step

Code

#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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)Const intmaxn=2501;#defineMoD 1000000009#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************intdy[4]={1,0,-1,0};intdx[4]={0,1,0,-1};//this is donkey + +, Tiger--intvis1[1100][1100];intvis2[1100][1100];intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {        if(n==0)             Break; memset (Vis1,0,sizeof(VIS1)); memset (Vis2,0,sizeof(VIS2)); intX=read (), Y=read (), z=read (); intXx=read (), Yy=read (), zz=read (); intflag=0; intflag1=1, flag2=1; intflag11=1, flag22=1; inttt=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; }            intnowx=x,nowy=y,nowz=Z;  for(intI=0;i<2; i++)            {                if(flag11==0)                     Break; intnextx=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(intI=0;i<2; i++)            {                if(flag22==0)                     Break; intnextx=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"); }}

Hdu 4740 The donkey of Gui Zhou BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.