HDU 4740 The Donkey of Gui Zhou

The Donkey of Gui Zhou

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4740
64-bit integer IO format: %I64d      Java class name: Main

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

2013 ACM/ICPC Asia Regional Hangzhou Online

解题:模拟什么的最难搞了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 #define pii pair<int,int>
16 using namespace std;
17 const int maxn = 1005;
18 bool vd[maxn][maxn],vt[maxn][maxn];
19 int n,dx,dy,dr,tx,ty,tr;
20 bool dstop,tstop;
21 const int dir[4][2] = {0,1,1,0,0,-1,-1,0};
22 bool ismeet() {
23     return (tx == dx && ty == dy);
24 }
25 bool isIn(int x,int y) {
26     return (x >= 0 && y >= 0 && x < n && y < n);
27 }
28 bool go() {
29     dstop = false;
30     tstop = false;
31     while(true) {
32         int nx = dx + dir[dr][0];
33         int ny = dy + dir[dr][1];
34         if(!dstop&&isIn(nx,ny) && !vd[nx][ny]) {
35             dx = nx;
36             dy = ny;
37             vd[dx][dy] = true;
38         } else if(!dstop) {
39             int ddr = (dr + 1)%4;
40             nx = dx + dir[ddr][0];
41             ny = dy + dir[ddr][1];
42             if(isIn(nx,ny) && !vd[nx][ny]) {
43                 dx = nx;
44                 dy = ny;
45                 dr = ddr;
46                 vd[dx][dy] = true;
47             } else dstop = true;
48         }
49
50         nx = tx + dir[tr][0];
51         ny = ty + dir[tr][1];
52         if(!tstop && isIn(nx,ny) && !vt[nx][ny]) {
53             tx = nx;
54             ty = ny;
55             vt[tx][ty] = true;
56         } else if(!tstop) {
57             int ttr = (tr + 3)%4;
58             nx = tx + dir[ttr][0];
59             ny = ty + dir[ttr][1];
60             if(isIn(nx,ny) && !vt[nx][ny]) {
61                 tx = nx;
62                 ty = ny;
63                 tr = ttr;
64                 vt[tx][ty] = true;
65             } else tstop = true;
66         }
67         if(ismeet()) return true;
68         if(tstop && dstop) break;
69     }
70     return false;
71 }
72 int main() {
73     while(scanf("%d",&n),n) {
74         scanf("%d %d %d",&dx,&dy,&dr);
75         scanf("%d %d %d",&tx,&ty,&tr);
76         memset(vd,false,sizeof(vd));
77         memset(vt,false,sizeof(vt));
78         vd[dx][dy] = true;
79         vt[tx][ty] = true;
80         if(tx == dx && dy == ty) {
81             printf("%d %d\n",tx,ty);
82             continue;
83         }
84         if(go()) printf("%d %d\n",tx,ty);
85         else puts("-1");
86     }
87     return 0;
88 }

时间: 2024-11-10 07:47:10

HDU 4740 The Donkey of Gui Zhou的相关文章

hdu 4740 The Donkey of Gui Zhou bfs

The Donkey of Gui Zhou Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4740 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

hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)

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)

【hdu】The Donkey of Gui Zhou(搜索)

一开始的思路是,先走驴子,之后走老虎,每次走的时候记录他们的达到该位置的步数,如果一样就可以相遇,特殊处理末尾. 但是没办法过,之后索性让他们一起走,一起修改移动的坐标,结果过了... 真心不知道错哪了.. 11662502 2014-09-16 00:43:01 Accepted 4740 31MS 8300K 2547 B G++ KinderRiven #include<cstdio> #include<cstring> #include<cstdlib> #in

hdu4740 搜索(会爆栈,需要手动开辟)

http://acm.hdu.edu.cn/showproblem.php?pid=4740 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 c

旺仔:自动提示输入框

package com.example.administrator.mytest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class Test extends AppCompatActivity { private

Android View之用户界面...

PS:Android的控件真的是很多...现在还在忙到控件...也是神了.... 学习内容: 1.Spinner下拉菜单... 2.AutoComplete TextView自动完成文本框... 1.Spinner下拉菜单....   下拉菜单,这个是非常熟悉的...这个大部分应用基本是有的...比较常用,相对而言,挺简单的,就是有些地方不那么容易理解而已...Spinner这个东西需要使用到适配器...那什么是适配器呢?到底是干什么用的呢?就是将我们保存的数据更好的显示在View上...这就是

HDU 5343 MZL&#39;s Circle Zhou

MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 534364-bit integer IO format: %I64d      Java class name: Main MZL's Circle Zhou is good at solving some counting problems. One day, he comes

hdu 4609 3-idiots 【FFT快(gui)速傅立叶变换】

FFT实现起来挺复杂的,开始用vector,结果发现空间超了,换成数组还是超,删掉了几个后又超时了 sin cos 函数调用次数太多了,改成乘法,还是超时 最后把FFT里的除法运算和模运算优化了一下,终于过了,排的老后面 坑,3843MS,一看时间最少的只有671MS,我都怀疑这是不是同一个算法..为毛差距这么大 #pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include

HDU 4870 Rating(概率、期望、推公式) &amp;&amp; ZOJ 3415 Zhou Yu

其实zoj 3415不是应该叫Yu Zhou吗...碰到ZOJ 3415之后用了第二个参考网址的方法去求通项,然后这次碰到4870不会搞.参考了chanme的,然后重新把周瑜跟排名都反复推导(不是推倒)四五次才上来写这份有抄袭嫌疑的题解... 这2题很类似,多校的rating相当于强化版,不过原理都一样.好像是可以用高斯消元做,但我不会.默默推公式了. 公式推导参考http://www.cnblogs.com/chanme/p/3861766.html#2993306 http://www.cn