您的位置:首页 > 理论基础 > 计算机网络

HDU 网络赛(杭州赛区)1003 (13.09.15)

2013-09-16 14:20 253 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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)
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

2
0 0 0
0 1 2
4
0 1 0
3 2 0
0

 

[align=left]Sample Output[/align]

-1
1 3

题意:

给出一片森林

在这片森林里, 驴和虎从没见过对方, 因此一见面就吓尿了, 撒腿就跑

0 1 2 3代表四个方向, 没有特殊情况就按初始方向直直跑

不能继续向前了, 驴是只考虑向右, 而虎是只考虑向左

注意, 驴和虎都不能跑自己曾经跑过的地块!

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int lgrid[1234][1234];
int hgrid[1234][1234];

int t;
int success;
int lunremove;
int hunremove;
int ansx, ansy;

int main() {
while(scanf("%d", &t) != EOF && t) {
success = 0;
lunremove = 1;
hunremove = 1;
memset(lgrid, 0, sizeof(lgrid));
memset(hgrid, 0, sizeof(hgrid));
int ltx, lty, htx, hty;
int ltd, htd;
scanf("%d %d %d", <x, <y, <d);
scanf("%d %d %d", &htx, &hty, &htd);
while(lunremove == 1 || hunremove == 1) {
if(ltx == htx && lty == hty) {
success = 1;
ansx = ltx;
ansy = lty;
break;
}
lgrid[ltx][lty] = 1;
hgrid[htx][hty] = 1;
if(ltd == 2 && lunremove == 1) {
if(lty > 0 && lgrid[ltx][lty-1] != 1)
lty = lty - 1;
else if(ltx > 0 && lgrid[ltx-1][lty] != 1) {
ltx = ltx - 1;
ltd = 3;
}
else
lunremove = 0;
}
else if(ltd == 1 && lunremove == 1) {
if(ltx < t-1 && lgrid[ltx+1][lty] != 1)
ltx = ltx + 1;
else if(lty > 0 && lgrid[ltx][lty-1] != 1) {
lty = lty - 1;
ltd = 2;
}
else
lunremove = 0;
}
else if(ltd == 0 && lunremove == 1) {
if(lty < t-1 && lgrid[ltx][lty+1] != 1)
lty = lty + 1;
else if(ltx < t-1 && lgrid[ltx+1][lty] != 1) {
ltx = ltx + 1;
ltd = 1;
}
else
lunremove = 0;
}
else if(ltd == 3 && lunremove == 1) {
if(ltx > 0 && lgrid[ltx-1][lty] != 1)
ltx = ltx - 1;
else if(lty < t-1 && lgrid[ltx][lty+1] != 1) {
lty = lty + 1;
ltd = 0;
}
else
lunremove = 0;
}
if(htd == 2 && hunremove == 1) {
if(hty > 0 && hgrid[htx][hty-1] != 1)
hty = hty - 1;
else if(htx < t-1 && hgrid[htx+1][hty] != 1) {
htx = htx + 1;
htd = 1;
}
else
hunremove = 0;
}
else if(htd == 1 && hunremove == 1) {
if(htx < t-1 && hgrid[htx+1][hty] != 1)
htx = htx + 1;
else if(hty < t-1 && hgrid[htx][hty+1] != 1) {
hty = hty + 1;
htd = 0;
}
else
hunremove = 0;
}
else if(htd == 0 && hunremove == 1) {
if(hty < t-1 && hgrid[htx][hty+1] != 1)
hty = hty + 1;
else if(htx > 0 && hgrid[htx-1][hty] != 1) {
htx = htx - 1;
htd = 3;
}
else
hunremove = 0;
}
else if(htd == 3 && hunremove == 1) {
if(htx > 0 && hgrid[htx-1][hty] != 1)
htx = htx - 1;
else if(hty > 0 && hgrid[htx][hty-1] != 1) {
hty = hty - 1;
htd = 2;
}
else
hunremove = 0;
}
}
if(success == 1)
printf("%d %d\n", ansx, ansy);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: