2013杭州網路賽C題HDU 4640(模擬)
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.
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)
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).驢子向右走老虎向左走。開始不是說兩個見著很害怕麼,我以為兩個這時候就會轉向,實際上題目沒這層意思。他們可以不改變方向繼續往前走,除非同時到達同一個地點,其他都不是問題。
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杭州網路賽D題HDU 4741(計算幾何 解三元一次方程組)
- 2014北京網路賽1007||hdu5038 模擬
- 模擬賽雜題
- 2013成都網路賽1004題HDU 4731Minimum palindrome (思維題目)
- 10.6 模擬賽(NOIP 模擬賽 #9)
- 模擬賽
- HDU 5319 Painter (模擬 腦洞題)AI
- 2013成都網路賽 兩個簡單題
- 10.31 模擬賽題解
- 【題解】「CSP模擬賽」雨天 rainAI
- 5.4 模擬賽
- 8.5 模擬賽
- 8.2 模擬賽
- 9.2 模擬賽
- 模擬賽 2
- 2014鞍山網路賽 E題||hdu 5001 概率dp
- 2013長沙網路賽H題Hypersphere (蛋疼的題目 神似邀請賽A題)
- HDU 4288-Coder(模擬)
- 2013長沙網路賽 E題(水題 有點小bug)
- hdu5452 || 瀋陽網路賽1003題 最近公共祖先問題
- 模擬比賽-14屆研究生組C++省賽C++
- Cisco Packet Tracer Student(思科網路模擬器)模擬搭建VLAN網路
- 瀋陽網路賽I-Lattice's basics in digital electronics【模擬】Git
- Codeforces模擬賽,題解及體會
- Record - 提高/NOIP模擬賽做題記錄
- 7月模擬賽
- 10.7 模擬賽
- NZOJ 模擬賽5
- CSP模擬賽#34
- 10.13 模擬賽
- NOIP模擬賽2
- CSP模擬賽 #39
- CSP模擬賽 #42
- 11.3 模擬賽
- NOIP模擬賽 #4
- 9.12 模擬賽
- YT15-HDU-字串的模擬字串
- HDU 4496D-City2013通化邀請賽D題(並查集 需要壓縮路徑)並查集