您的位置:首页 > 产品设计 > UI/UE

HDU4740 The Donkey of Gui Zhou 暴力模拟

2015-07-25 21:00 483 查看
问题描述:

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.

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.



输入:

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)

输出:

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.


样例输入:

2

0 0 0

0 1 2

4

0 1 0

3 2 0

0

样例输出:

-1

1 3

大意:2个人开始走,碰到边界或者自己走过的点就转向,如果转向后还不能走就停下来,问2个人能不能相遇。相遇输出坐标不能则输入-1

思路:直接模拟就行。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int fangxiang[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool a[1005][1005][2], f1,f2;
int main()
{
    int n,r1,c1,d1,r2,c2,d2,r,c;
    while(cin>>n)
    {
        if(n==0) break;
        cin>>r1>>c1>>d1>>r2>>c2>>d2;
        memset(a,false,sizeof(a));
        f1=true;f2=true;
        while(f1||f2)
        {
            if(r1==r2&&c1==c2) break;
            if(f1)
            {
                a[r1][c1][0]=true;
                r=r1+fangxiang[d1][0];
                c=c1+fangxiang[d1][1];
                if(r<0 || r==n || c<0 || c==n || a[r][c][0])
                {
                    d1++;
                    if(d1==4) d1=0;
                    r=r1+fangxiang[d1][0];
                    c=c1+fangxiang[d1][1];
                    if(r<0 || r==n || c<0 || c==n || a[r][c][0])
                        f1=false;
                    else
                        {r1=r;c1=c;}
                }
                else
                    {r1=r;c1=c;}
            }
            if(f2)
            {
                a[r2][c2][1]=true;
                r=r2+fangxiang[d2][0];
                c=c2+fangxiang[d2][1];
                if(r<0||r==n||c<0||c==n||a[r][c][1])
                {
                    d2--;
                    if(d2<0) d2=3;
                    r=r2+fangxiang[d2][0];
                    c=c2+fangxiang[d2][1];
                    if(r<0||r==n||c<0||c==n||a[r][c][1])
                        f2=false;
                    else
                        {r2=r;c2=c;}
                }
                else
                    {r2=r;c2=c;}
            }
        }
        if(r1==r2&&c1==c2)
            cout<<r1<<" "<<c1<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: