编程介的小学生 2017-11-19 12:02 采纳率: 20.5%
浏览 661
已结题

The Donkey of Gui Zhou

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条回答 默认 最新

  • $(X) 2018-07-17 14:57
    关注
    /*from:   https://blog.csdn.net/u013451221/article/details/39304105*/
    #include<cstdio>
    
    #include<cstring>
    
    #include<cstdlib>
    
    #include<iostream>
    
    #include<string>
    
    #include<vector>
    
    #include<stack>
    
    #include<set>
    
    #include<algorithm>
    
    #include<map>
    
    #include<sstream>
    
    #include<cmath>
    
    #include<queue>
    
    #define INF (1 << 30)
    
    #define eps (1e-10)
    
    #define _PI acos(-1.0)
    
    using namespace std;
    
    /*=========================================
    
    =========================================*/
    
    #define MAXD 1000 + 10
    
    int n;
    
    int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
    
    int aa , bb;
    
    int vis1[MAXD][MAXD],vis2[MAXD][MAXD];
    
    bool solve(int x1,int y1,int d1,int x2,int y2,int d2){
    
        memset(vis1,0,sizeof(vis1));
    
        memset(vis2,0,sizeof(vis2));
    
        vis1[x1][y1] = 1;
    
        vis2[x2][y2] = 1;
    
        int _x1 ,_y1;
    
        int _x2 ,_y2;
    
        while(aa || bb){
    
            if(x1 == x2 && y1 ==y2){
    
                printf("%d %d\n",x1,y1);
    
                return true;
    
            }
    
            if(aa){
    
                _x1 = x1 + dir[d1][0];
    
                _y1 = y1 + dir[d1][1];
    
                if(_x1 >= 0 && _x1 < n && _y1 >= 0 && _y1 < n && !vis1[_x1][_y1]){
    
                    vis1[_x1][_y1] = 1;
    
                }
    
                else{
    
                    d1 = (d1 + 1) % 4;
    
                    _x1 = x1 + dir[d1][0];
    
                    _y1 = y1 + dir[d1][1];
    
                    if(_x1 >= 0 && _x1 < n && _y1 >= 0 && _y1 < n && !vis1[_x1][_y1]){
    
                        vis1[_x1][_y1] = 1;
    
                    }
    
                    else{
    
                        aa = 0;
    
                    }
    
                }
    
            }
    
            if(bb){
    
                _x2 = x2 + dir[d2][0];
    
                _y2 = y2 + dir[d2][1];
    
                if(_x2 >= 0 && _x2 < n && _y2 >= 0 && _y2 < n && !vis2[_x2][_y2]){
    
                    vis2[_x2][_y2] = 1;
    
                }
    
                else{
    
                    d2 = (d2 + 3) % 4;
    
                    _x2 = x2 + dir[d2][0];
    
                    _y2 = y2 + dir[d2][1];
    
                    if(_x2 >= 0 && _x2 < n && _y2 >= 0 && _y2 < n && !vis2[_x2][_y2]){
    
                        vis2[_x2][_y2] = 1;
    
                    }
    
                    else{
    
                        bb = 0;
    
                    }
    
                }
    
            }
    
            if(aa) {x1 = _x1; y1 = _y1;}
    
            if(bb) {x2 = _x2; y2 = _y2;}
    
        }
    
        return false;
    
    }
    
    int main(){
    
        while(scanf("%d",&n) && n){
    
            int x1,y1,x2,y2,d1,d2;
    
            aa = 1; bb = 1;
    
            scanf("%d%d%d",&x1,&y1,&d1);
    
            scanf("%d%d%d",&x2,&y2,&d2);
    
            if(!solve(x1,y1,d1,x2,y2,d2))
    
               printf("-1\n");
    
        }
    
        return 0;
    
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改