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
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
随机推荐
- Path Sum 解答
Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- ubuntu cenots 禁止本地登陆
auth required pam_succeed_if.so user != root
- Java菜鸟学习笔记--数组篇(三):二维数组
定义 //1.二维数组的定义 //2.二维数组的内存空间 //3.不规则数组 package me.array; public class Array2Demo{ public static void ...
- cenos 安装 phpredis 扩展
1. php -m 可以查看 php 所有的已经安装的扩展
- C# ?? 操作符示例
static int? GetNullableInt() { return null; } static string GetStringValue() { return null; } static ...
- 六步实现Spring.NET 与 NHibernate 的整合
最近刚完成一个项目,其中对数据库的架构用到的是Spring.NET 与 NHibernate相结合的产物.对于这两项技术,我自己也不是太熟悉,不过好在网上有很多关于这方面的介绍文档,在这里就不多说了. ...
- 求一组数字序列的分布情况(java)
最近需要做一个正态分布的函数图像所以要处理一段double序列 写了这个算法 先上效果图: 核心思想: 1先根据步长计算每一个区间 2循环进行判断序列中每个数属于哪个区间 3用一个数组来保存每一个区 ...
- VS2003,安装程序检测到另一个程序…
昨天在安装Visual Studio .Net 2003时,出现一个对话框 "安装程序检测到另一个程序要求计算机重新启动.必须重新启动计算机后才能安装visual studio.net系 ...
- C# Hashtable 使用说明 以及 Hashtable和HashMap的区别
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...
- 手把手教你使用Git(转)
Git使用教程 2014-10-25 14:29 by 云溪0707, 10532 阅读, ... 评论, 收藏, 编辑 Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系 ...