hdu4740 search (will explode stack, need to open manually)

Source: Internet
Author: User

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

Problem Descriptionthere 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. 
Inputthere 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)
Outputfor Each test case, if the donkey and the Tiger would meet in a cell, print the coordinate of the cell where they me ET first time. If they would never meet, print-1 instead.
Sample Input
20 0 00 1 240 1 03 2 00

Sample Output
-11 3

Source2013 ACM/ICPC Asia Regional Hangzhou Online
/**hdu4740 search (will explode stack, need to open up manually) The topic: There is a donkey and a tiger, both in a n*n chess board run, to determine whether there is a point in time and the two ran to the same lattice. As for them to run to the way, or to read the question of the idea: two times Dfs with two vectors to save the donkey and the tiger in the position I time, find Vector1 I and Vector2 i inside coordinates first coincident on the line */#pragma comment (linker, "/ stack:10240000000000,10240000000000 ") #include <stdio.h> #include <string.h> #include <algorithm># Include <iostream> #include <vector>using namespace Std;vector <pair<int,int> > Vec,vec1;bool    Flag[1005][1005];int n;void dfs (int x,int y,int r,int num)//donkey left {flag[x][y]=1;    Vec.push_back (Make_pair (x, y));        if (r==1) {if (flag[x+1][y]==0&&x+1<=n) {DFS (x+1,y,r,num++);            } else {if (flag[x][y-1]==0&&y-1>0) {DFS (x,y-1,2,num++); }}} and else if (r==2) {if (flag[x][y-1]==0&&y-1>0) {DFS (x,y-1        , r,num++);            } else {if (flag[x-1][y]==0&&x-1>0) {    DFS (x-1,y,3,num++);         }}} and else if (r==3) {if (flag[x-1][y]==0&&x-1>0) {DFS (x-1,y,r,num++); } else {if (flag[x][y+1]==0&&y+1<=n) {DFS (x,y+1,0,n            um++);        }}}} else {if (flag[x][y+1]==0&&y+1<=n) {DFS (x,y+1,r,num++);            } else {if (flag[x+1][y]==0&&x+1<=n) {DFS (x+1,y,1,num++);    }}}}void dfs1 (int x,int y,int r,int num)///Tiger right {flag[x][y]=1;    Vec1.push_back (Make_pair (x, y));        if (r==1) {if (flag[x+1][y]==0&&x+1<=n) {DFS1 (x+1,y,r,num++);            } else {if (flag[x][y+1]==0&&y+1<=n) {DFS1 (x,y+1,0,num++);  }}} and else if (r==2) {if (flag[x][y-1]==0&&y-1>0) {          DFS1 (x,y-1,r,num++);            } else {if (flag[x+1][y]==0&&x+1<=n) {DFS1 (x+1,y,1,num++); }}} and else if (r==3) {if (flag[x-1][y]==0&&x-1>0) {DFS1 (x-1        , y,r,num++);            } else {if (flag[x][y-1]==0&&y-1>0) {DFS1 (x,y-1,2,num++); }}} and else {if (flag[x][y+1]==0&&y+1<=n) {DFS1 (x,y+1,r,num+        +);            } else {if (flag[x-1][y]==0&&x-1>0) {DFS1 (x-1,y,3,num++);        }}}}int Main () {while (~SCANF ("%d", &n)) {if (n==0) break;        int x,y,r;        scanf ("%d%d%d", &x,&y,&r);        memset (flag,0,sizeof (flag));        Vec.clear ();        DFS (x+1,y+1,r,0);        int m=vec.size (); /*for (int i=0;i<m;i++) {PrinTF ("%d%d\n", vec[i].first,vec[i].second);        }*/scanf ("%d%d%d", &x,&y,&r);        memset (flag,0,sizeof (flag));        Vec1.clear ();        DFS1 (x+1,y+1,r,0);        int m1=vec1.size ();        /*for (int i=0;i<m1;i++) {printf ("%d%d\n", vec1[i].first,vec1[i].second);        }*/bool cnt=0; for (int i=0; i<m&&i<m1&&cnt==0; i++) {if (Vec[i].first==vec1[i].first&&vec                [I].second==vec1[i].second) {x=vec[i].first-1;                Y=vec[i].second-1;            Cnt=1;                }} if (cnt==0) {if (M&LT;M1) {for (int i=m; i<m1; i++)                    {if (Vec[m-1].first==vec1[i].first&&vec[m-1].second==vec1[i].second)                        {x=vec1[i].first-1;                        Y=vec1[i].second-1;                    Cnt=1;       }         }} else {for (int i=m1; i<m; i++) {                        if (Vec[i].first==vec1[m1-1].first&&vec[i].second==vec1[m1-1].second) {                        x=vec[i].first-1;                        Y=vec[i].second-1;                    Cnt=1;        }}}} if (cnt==0) printf (" -1\n");    else printf ("%d%d\n", x, y); } return 0;}


hdu4740 search (will explode stack, need to open manually)

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.