The Donkey of Gui Zhou

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 389    Accepted Submission(s): 153

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
 
Source
 
Recommend
liuyiding
 


感想
:现在才发现当时自己把题目读复杂了,怪不得自己搞了半天最后还是WA了。题意是王道,题意理解错了都是扯淡。好在我看见这个模拟水题之后想到了以前做的那两个兔子的模拟,和吉吉说了下,吉吉后来拿了一血,虽然不早,但毕竟是一血。

 题目大意:是说两个动物驴子和老虎在一个方格里面走,然后会给你他们的初始位置,初始方向。如果不能向前走越界或者自己已经访问过了,可以转向。驴子顺时针转,老虎逆时针转。如果转向一次之后还是不不能走,那么它以后都不能走了。主要当时曲解的题意,就是两个快要碰到的时候会转向,比如驴子在(0,0),老虎在(0,1).驴子向右走老虎向左走。开始不是说两个见着很害怕么,我以为两个这时候就会转向,实际上题目没这层意思。他们可以不改变方向继续往前走,除非同时到达同一个地点,其他都不是问题。

 题目地址:The Donkey of Gui Zhou

AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; int dir[4][2]= //往东南西北四个方向
{
{0,1},{1,0},{0,-1},{-1,0}
};
int visidon[1005][1005];
int visitig[1005][1005]; int main()
{
int n,i,j;
int donx,dony,tigx,tigy,pdon,ptig;
while(scanf("%d",&n)&&n)
{
memset(visidon,0,sizeof(visidon));
memset(visitig,0,sizeof(visitig));
scanf("%d%d%d",&donx,&dony,&pdon); //驴子的坐标与方向
scanf("%d%d%d",&tigx,&tigy,&ptig); //老虎的坐标与方向
visidon[donx][dony]=1;
visitig[tigx][tigy]=1;
int flag=0;
int fla1=0,fla2=0;//代表驴子和老虎不能转向
if(donx==tigx&&dony==tigy) //开始就在一起,直接输出
{
cout<<donx<<" "<<dony<<endl;
continue;
}
else
{
while(1)
{
if(fla1&&fla2)
{
break;
}
int cx1,cy1,cx2,cy2;
cx1=donx,cy1=dony,cx2=tigx,cy2=tigy;
if(!fla1) //驴子还可以走
{
cx1=donx+dir[pdon][0];
cy1=dony+dir[pdon][1];
}
if(!fla2) //老虎还可以走
{
cx2=tigx+dir[ptig][0];
cy2=tigy+dir[ptig][1];
} if(!fla1) //驴子还可以走
{
if(cx1>=0&&cx1<n&&cy1>=0&&cy1<n&&!visidon[cx1][cy1]) //可以沿着方向走
{
donx=donx+dir[pdon][0];
dony=dony+dir[pdon][1];
visidon[donx][dony]=1;
//cout<<"驴子:"<<donx<<" "<<dony<<endl;
}
else //转了一次方向
{
pdon=(pdon+1+4)%4;
cx1=donx+dir[pdon][0];
cy1=dony+dir[pdon][1];
if(cx1>=0&&cx1<n&&cy1>=0&&cy1<n&&!visidon[cx1][cy1]) //可以沿着方向走
{
donx=donx+dir[pdon][0];
dony=dony+dir[pdon][1];
visidon[donx][dony]=1;
//cout<<"驴子:"<<donx<<" "<<dony<<endl;
}
else
fla1=1;
//转了一次方向还是不能走,那就停下来
}
} if(!fla2) //老虎还可以走
{
if(cx2>=0&&cx2<n&&cy2>=0&&cy2<n&&!visitig[cx2][cy2])
{
tigx=tigx+dir[ptig][0];
tigy=tigy+dir[ptig][1];
visitig[tigx][tigy]=1;
//cout<<"老虎:"<<tigx<<" "<<tigy<<endl;
}
else
{
ptig=(ptig-1+4)%4;
cx2=tigx+dir[ptig][0];
cy2=tigy+dir[ptig][1];
if(cx2>=0&&cx2<n&&cy2>=0&&cy2<n&&!visitig[cx2][cy2])
{
tigx=tigx+dir[ptig][0];
tigy=tigy+dir[ptig][1];
visitig[tigx][tigy]=1;
//cout<<"老虎:"<<tigx<<" "<<tigy<<endl;
}
else
fla2=1;
}
}
if(donx==tigx&&dony==tigy) //说明撞在一起
{
flag=1;
break;
}
}
if(!flag)
puts("-1");
else
{
printf("%d %d\n",donx,dony);
}
}
}
return 0;
}

2013杭州网络赛C题HDU 4640(模拟)的更多相关文章

  1. 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  4. HDU 4741 Save Labman No.004 (2013杭州网络赛1004题,求三维空间异面直线的距离及最近点)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 4739 Zhuge Liang's Mines (2013杭州网络赛1002题)

    Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  7. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

随机推荐

  1. jquery 插件 validate 学习

    jquery是十分方便的对于现在来说. 首先应该明白一个问题: <p> <label  for="password">Password</label& ...

  2. SqlDataAdapter的方法之一Fill (DataSet dataset, String datatable)解释

    一.SqlDataAdapter的方法之一Fill (DataSet dataset, String datatable)解释:根据datatable名填充Dataset.myda.Fill(ds, ...

  3. BZOJ 1625: [Usaco2007 Dec]宝石手镯( dp )

    最裸的01背包.... --------------------------------------------------------------------- #include<cstdio ...

  4. 什么是weblogic?安装步骤详解

    weblogic,就是用于java开发的web服务器. tomcat熟悉吧,跟tomcat一个作用,是比tomcat更具优势的web服务器. 安装:(转载) 1.提供安装文件网盘下载:链接处2.安装过 ...

  5. Inlay技术要求

    物理特性: 项目 要求内容 备考 基准值 公差 INLAY尺寸 A(长) 480mm ±0.5mm B(宽) 380mm ±0.5mm 线圈位置 C(天地位置) 16.05mm ±0.2mm D(左右 ...

  6. QT5程序发布dll依赖

    QT5 发布程序,太庞大了,QT4下,基本的也就20M左右 platforms目录是 对应X:\Qt\Qt5.3.2\5.3\mingw482_32\plugins\platforms 下的qmini ...

  7. android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK

    android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK ADB server didn ...

  8. hdu1395-2^x mod n = 1

    http://acm.hdu.edu.cn/showproblem.php?pid=1395 原理为 a ^ b % n == d ; >>>>>>  (( a % ...

  9. tomcat的webappclassloader中一个奇怪的异常信息

    假设一个应用抛出大量的Class not found信息,一般你会怀疑包冲突.但是tomcat的webappclassloader却有这种问题: 假设一个应用公布出现故障, webappclasslo ...

  10. Linux通过网卡驱动程序和版本号的信息

    检查卡制造商和信号 查看基本信息:lspci 查看详情:lspci -vvv   # 3小作文v 查看卡信息:lspci | grep Ethernet 查看网卡驱动 查看网卡驱动信息:lspci - ...