hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)
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 (,) , the down-right cell is (N-,N-) and the cell below the up-left cell is (,)..... A × 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 = . ( <= N <= , <= 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 - instead.
-
题意:
在一个N*N的方格里,有一只驴和一只虎,两者以相同的速度(一格一格地走)同时开始走。走法是:往东南西北某一个初始方向走,两者都不能重复走自己走过的路,但是对方走过的路自己可以走,如果遇到墙壁或者自己走过的路,则驴向右转,虎向左转,如果还不能继续往前走,就停在原地不动。如果驴和老虎能同一时间在一个格子里面相遇,则输出坐标,否则输出-1 。
分析:
深搜。
1)当两者坐标相同时,相遇;
2)两者同时在不同地方都停下时,肯定不相遇;
3)如果有一个停下了,记录当前坐标,否则往初始方向继续遍历。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n;
int flag;
int p,q;
int dirx[]={,,,-};
int diry[]={,,-,};
int vis1[N][N],vis2[N][N];
void dfs(int a,int b,int c,int x,int y,int z){
vis1[a][b]=;
vis2[x][y]=;
if(flag==){
return;
}
if(a==x && b==y){
flag=;
printf("%d %d\n",a,b);
return;
} if(p && q){
flag=;
printf("-1\n");
return;
}
int aa,bb,xx,yy; if(p){
aa=a;
bb=b;
}else{
aa=a+dirx[c];
bb=b+diry[c];
if(aa< || aa>=n || bb< || bb>=n || vis1[aa][bb]==){
c=(c+)%;
aa=a+dirx[c];
bb=b+diry[c];
if(aa< || aa>=n || bb< || bb>=n || vis1[aa][bb]==){
p=;
aa=a;
bb=b;
}
}
} if(q){
xx=x;
yy=y;
}else{
xx=x+dirx[z];
yy=y+diry[z];
if(xx< || xx>=n || yy< || yy>=n || vis2[xx][yy]==){
z=(z-+)%;
xx=x+dirx[z];
yy=y+diry[z];
if(xx< || xx>=n || yy< || yy>=n || vis2[xx][yy]==){
q=;
xx=x;
yy=y;
}
}
}
dfs(aa,bb,c,xx,yy,z);
}
int main()
{
while(scanf("%d",&n)==){
if(n==){
break;
}
memset(vis1,,sizeof(vis1));
memset(vis2,,sizeof(vis2));
int a,b,c,x,y,z;
scanf("%d%d%d",&a,&b,&c);
scanf("%d%d%d",&x,&y,&z);
if(a==x && b==y){
printf("%d %d\n",a,b);
continue;
}
flag=;
p=q=;
dfs(a,b,c,x,y,z); }
return ;
}
hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)的更多相关文章
- 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/showproble ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- hdu 4740 The Donkey of Gui Zhou
1.扯犊子超多if else 判断的代码,华丽丽的TLE. #include<stdio.h> #include<string.h> #define N 1010 int ma ...
- HDU 4740 The Donkey of Gui Zhou (模拟)
由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视. 不是写模拟的料...................... 反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站 ...
- The Donkey of Gui Zhou
Problem Description There was no donkey in the province of Gui Zhou, China. A trouble maker shipped ...
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- Vijos P1114 FBI树【DFS模拟,二叉树入门】
描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- Android Studio参考在线文章
This article is From :http://www.android-studio.org/index.php/docs/guide Gradle使用手册(一):为什么要用Gradle? ...
- knowledges address
http://www.zhukun.net/archives/5794
- Mysql 添加用户和数据库授权
注:我的运行环境是widnows xp professional + MySQL5.0 一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY ...
- jQuery.extend()、jQuery.fn.extend()扩展方法示例详解
jQuery自定义了jQuery.extend()和jQuery.fn.extend()方法.其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方 ...
- android scrollview 简单的使用
以前写的Scrollview ,通常都是与Listview结合使用,不过因复杂可能新手不太懂,网上有许多文章,这里就不贴那个了DEMO了. 写了个简单的供大家参考:这样比较好理解(需要复杂的可以Q我 ...
- hpux操作系统的关机与重新启动命令
关机 shutdown -hy 0 重新启动: shutdown -ry 0
- Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 2
Prepare 10g Database for OGG Create GGS and GGS_MON Database Users SQL> create tablespace ggs_tbs ...
- 基于angularJS和requireJS的前端架构
1.概要描述 1.1.angularJS描述:angularJS是可以用来构建WEB应用的,WEB应用中的一种端对端的完整解决方案.通过开发者呈现一个更高层次的抽象来简化应用的开发.最适合的就是用它来 ...
- RMAN-format变量及configuration配置项
一.FORMAT字符串替代变量使用FORMAT参数时可使用的各种替换变量,如下:%c:备份片的拷贝数(从1开始编号):%d:数据库名称:%D:位于该月中的天数 (DD):%M:位于该年中的月份 (MM ...
- linux上大量tcp端口处于TIME_WAIT的问题
今天发现网站突然无法访问,登陆webserver已经无法连接mysql,转战mysql一看,发现竟然有三千多个TIME_WAIT连接,擦,被SYN攻击了?百度&&google,发现通过 ...