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 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

/**
hdu4740 搜索(会爆栈,需要手动开辟)
题目大意:有一头驴和一只虎,二者在一个n*n的棋盘里乱跑,判断是否有一个时间点二者跑到同一个格子上。至于他们跑到方式,还是看题吧
解题思路:两次dfs用两个vector分别存驴和虎在i时间所处的位置点,找vector1的i和vector2的i里面的坐标第一重合的就行了
*/
#pragma comment(linker, "/STACK:10240000000000,10240000000000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector <pair<int,int> > vec,vec1;
bool flag[1005][1005];
int n;

void dfs(int x,int y,int r,int num)///驴向左
{
    flag[x][y]=1;
    vec.push_back(make_pair(x,y));
    if(r==1)
    {
        if(flag[x+1][y]==0&&x+1<=n)
        {
            dfs(x+1,y,r,num++);
        }
        else
        {
            if(flag[x][y-1]==0&&y-1>0)
            {
                dfs(x,y-1,2,num++);
            }
        }
    }
    else if(r==2)
    {
        if(flag[x][y-1]==0&&y-1>0)
        {
            dfs(x,y-1,r,num++);
        }
        else
        {
            if(flag[x-1][y]==0&&x-1>0)
            {
                dfs(x-1,y,3,num++);
            }
        }
    }
    else if(r==3)
    {
        if(flag[x-1][y]==0&&x-1>0)
        {
            dfs(x-1,y,r,num++);
        }
        else
        {
            if(flag[x][y+1]==0&&y+1<=n)
            {
                dfs(x,y+1,0,num++);
            }
        }
    }
    else
    {
        if(flag[x][y+1]==0&&y+1<=n)
        {
            dfs(x,y+1,r,num++);
        }
        else
        {
            if(flag[x+1][y]==0&&x+1<=n)
            {
                dfs(x+1,y,1,num++);
            }
        }
    }
}

void dfs1(int x,int y,int r,int num)///虎向右
{
    flag[x][y]=1;
    vec1.push_back(make_pair(x,y));
    if(r==1)
    {
        if(flag[x+1][y]==0&&x+1<=n)
        {
            dfs1(x+1,y,r,num++);
        }
        else
        {
            if(flag[x][y+1]==0&&y+1<=n)
            {
                dfs1(x,y+1,0,num++);
            }
        }
    }
    else if(r==2)
    {
        if(flag[x][y-1]==0&&y-1>0)
        {
            dfs1(x,y-1,r,num++);
        }
        else
        {
            if(flag[x+1][y]==0&&x+1<=n)
            {
                dfs1(x+1,y,1,num++);
            }
        }
    }
    else if(r==3)
    {
        if(flag[x-1][y]==0&&x-1>0)
        {
            dfs1(x-1,y,r,num++);
        }
        else
        {
            if(flag[x][y-1]==0&&y-1>0)
            {
                dfs1(x,y-1,2,num++);
            }
        }
    }
    else
    {
        if(flag[x][y+1]==0&&y+1<=n)
        {
            dfs1(x,y+1,r,num++);
        }
        else
        {
            if(flag[x-1][y]==0&&x-1>0)
            {
                dfs1(x-1,y,3,num++);
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        int x,y,r;
        scanf("%d%d%d",&x,&y,&r);
        memset(flag,0,sizeof(flag));
        vec.clear();
        dfs(x+1,y+1,r,0);
        int m=vec.size();
        /*for(int i=0;i<m;i++)
        {
            printf("%d %d\n",vec[i].first,vec[i].second);
        }*/
        scanf("%d%d%d",&x,&y,&r);
        memset(flag,0,sizeof(flag));
        vec1.clear();
        dfs1(x+1,y+1,r,0);
        int m1=vec1.size();
        /*for(int i=0;i<m1;i++)
        {
            printf("%d %d\n",vec1[i].first,vec1[i].second);
        }*/
        bool cnt=0;
        for(int i=0; i<m&&i<m1&&cnt==0; i++)
        {
            if(vec[i].first==vec1[i].first&&vec[i].second==vec1[i].second)
            {
                x=vec[i].first-1;
                y=vec[i].second-1;
                cnt=1;
            }
        }
        if(cnt==0)
        {
            if(m<m1)
            {
                for(int i=m; i<m1; i++)
                {
                    if(vec[m-1].first==vec1[i].first&&vec[m-1].second==vec1[i].second)
                    {
                        x=vec1[i].first-1;
                        y=vec1[i].second-1;
                        cnt=1;
                    }
                }
            }
            else
            {
                for(int i=m1; i<m; i++)
                {
                    if(vec[i].first==vec1[m1-1].first&&vec[i].second==vec1[m1-1].second)
                    {
                        x=vec[i].first-1;
                        y=vec[i].second-1;
                        cnt=1;
                    }
                }
            }
        }
        if(cnt==0)
            printf("-1\n");
        else
            printf("%d %d\n",x,y);
    }
    return 0;
}
时间: 2024-10-09 23:22:25

hdu4740 搜索(会爆栈,需要手动开辟)的相关文章

爆栈三部曲:数据库开发大系技术栈 (300多技术点)

前言 这个数据库技术栈是我写的“爆栈三部曲”的最后一部 ;-) 最近我写过  .NET技术大系概览 (迄今为止最全的.NET技术栈) ,相信很多网友感叹掌握的.NET技术远没有这个技术栈里面所描述的多. 然后我还写 Web前端开发大系概览 (前端开发技术栈) ,包含大约180个技术点,做前端的都会觉得前端开发包含的技术相对繁多. 什么叫全栈(full stack)?简单地说就是万金油,web前端.后台.数据库.桌面应用等都能搞. 爆栈(stack overflow)来得更多些,包括但不仅限于:

hdu 1272 小希的迷宫(并查集/附爆栈的原因)

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33194    Accepted Submission(s): 10214 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该

不要搜索,出栈序列统计

1627: 出栈序列统计 时间限制: 1 Sec  内存限制: 128 MB 题目描述 栈是常用的一种数据结构,有n令元素在栈顶端一侧等待进栈,栈顶端另一侧是出栈序列.你已经知道栈的操作有两·种:push和pop,前者是将一个元素进栈,后者是将栈顶元素弹出.现在要使用这两种操作,由一个操作序列可以得到一系列的输出序列.请你编程求出对于给定的n,计算并输出由操作数序列1,2,-,n,经过一系列操作可能得到的输出序列总数. 输入 一个整数n(1<=n<=15) 输出 一个整数,即可能输出序列的总数

爆栈之前端工程化技术小结备案

ps,前些天一90后同事说,要我多写些blog,因为你的实力要show给别人知道,就要怎样怎样的.. 同时建议写个工程化架构来简化工作等等.有时觉得有点对,又觉得有点可笑,有觉得有点无奈... 由于本人是重后端技术,所以对前端技术了解并不深入. 由于很多都是原始pc的web,而且大多都是轻前端项目.所以这里先说下以前用到过的前端js技术大多是: jquery,extjs,easyui,ko,bootstrap,dorado等等,当然还有各种各样的,如日历,上传,富编辑器,报表等等js插件. 但现

爆栈之一般IT项目管理相关基本概念杂谈(备案)

PS,说到IT(PM)项目管理,还要提下职场两点小领会: 1.选择比努力重要: 2.位置比能力重要. 由于现在IT项目管理一般已下面三种角色划分: 1.产品经理,以产品为导向---对商业模式较为欠缺: 2.项目经理,以项目为导向---通晓一般模式,有一定体会: 3.技术经理,以技术为导向---有较深度和广度积累: 只是大部分公司都没有分那么清晰,大都是身兼多个角色,美其名曰:项目经理.技术总监.部门经理等等,当然也是根据实际情况的侧重不同而已~ IT项目管理,其实也是两个关键: 1.IT,即针对

会爆栈,dfs传参,只能传1个~~RMQLCA在线

#include<cstdio> #include<cstring> #include<algorithm> #pragma comment(linker, "/STACK:1024000000, 1024000000") #include<queue> using namespace std; const int M = 400015; struct Edge{ int v, next; } edge[M << 1]; in

记vue+leaflet的一次canvas渲染爆栈

背景: 在地图上绘制大量的circleMarker,leaflet能选择使用canvas来渲染,比起默认的svg渲染来说在大量绘制的情况下会更加流畅.但当触发其中某一个circleMarker的tooltip或popup时,浏览器报错"Uncaught RangeError: Maximum call stack size exceeded": 解决过程: 1.写了个测试代码来复现问题: 1 <!DOCTYPE html> 2 <html> 3 <head

hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现

http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:构造长度为n的字符序列,使得>=4的子串只出现一次 其实最长只能构造出来26^4+4-1= 456979 的序列,大于该数的都是不可能的.构造方法,就是那种欧拉回路的序列,此题DFS会爆栈,手动扩展栈也可以AC...... 递归形式的开始WA了,没有细调就换非递归了,后来又想了想,虽然自己电脑上运行不了,但是先把长度按小的来,然后调试代码,然后在扩大,AC了,当时错在MOD,递归的MOD应该是26

ACM第一阶段学习内容

一.知识目录 字符串处理 ................................................................. 3 1.KMP 算法 ............................................................ 3 2.扩展 KMP ............................................................ 6 3.Manacher 最长回文子串 .......