POJ 1071 & HDU 1364 & ZOJ 1019 Illusive Chase(DFS)

题目链接:

POJ:http://poj.org/problem?id=1071

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1364

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=19

Description

Tom the robocat is presented in a Robotics Exhibition for an enthusiastic audience of youngsters, placed around an m * n field. Tom which is turned off initially is placed in some arbitrary point in the field by a volunteer from the audience. At time zero of
the show, Tom is turned on by a remote control. Poor Tom is shown a holographic illusion of Jerry in a short distance such that a direct path between them is either vertical or horizontal. There may be obstacles in the field, but the illusion is always placed
such that in the direct path between Tom and the illusion, there would be no obstacles. Tom tries to reach Jerry, but as soon as he gets there, the illusion changes its place and the chase goes on. Let‘s call each chase in one direction (up, down, left, and
right), a chase trip. Each trip starts from where the last illusion was deemed and ends where the next illusion is deemed out. After a number of chase trips, the holographic illusion no more shows up, and poor Tom wonders what to do next. At this time, he
is signaled that for sure, if he returns to where he started the chase, a real Jerry is sleeping and he can catch it.

To simplify the problem, we can consider the field as a grid of squares. Some of the squares are occupied with obstacles. At any instant, Tom is in some unoccupied square of the grid and so is Jerry, such that the direct path between them is either horizontal
or vertical. It‘s assumed that each time Tom is shown an illusion; he can reach it by moving only in one of the four directions, without bumping into an obstacle. Tom moves into an adjacent square of the grid by taking one and only one step.

The problem is that Tom‘s logging mechanism is a bit fuzzy, thus the number of steps he has taken in each chase trip is logged as an interval of integers, e.g. 2 to 5 steps to the left. Now is your turn to send a program to Tom‘s memory to help him go back.
But to ease your task in this contest, your program should only count all possible places that he might have started the chase from.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains two integers m and n, which are the number of rows and columns of the
grid respectively (1 <= m, n <= 100). Next, there are m lines, each containing n integers which are either 0 or 1, indicating whether the corresponding cell of the grid is empty (0) or occupied by an obstacle (1). After description of the field, there is a
sequence of lines, each corresponding to a chase trip of Tom (in order). Each line contains two positive integers which together specify the range of steps Tom has taken (inclusive), followed by a single upper-case character indicating the direction of the
chase trip, which is one of the four cases of R (for right), L (for left), U (for up), and D (for down). (Note that these directions are relative to the field and are not directions to which Tom turns). This part of the test case is terminated by a line containing
exactly two zeros.

Output

For each test case, there should be a single line, containing an integer indicating the number of cells that Tom might have started the chase from.

Sample Input

2
6 6
0 0 0 0 0 0
0 0 0 1 1 0
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 1 0 1
0 0 0 0 0 1
1 2 R
1 2 D
1 1 R
0 0
3 4
0 0 0 0
0 0 0 0
0 0 0 0
1 2 R
3 7 U
0 0

Sample Output

10
0

Source

Tehran 2001

题意:

给出一个m*n的地图,给出机器人的一系列动作,机器人可以从任意的一点为起点,求机器人可能的起点个数;

每个动作由一个范围和一个字母组成,字母表示机器人走的方向,范围表示走的步长.

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct
{
    int l, r;
    char dir;
} node;
node op[100017];
int mapp[117][117];
int n, m;
int flag = 0;
int step;
int movee(int &x, int &y, char dir)
{
    if(dir == 'U')
    {
        if(x-1>=0 && mapp[x-1][y] == 0)
        {
            x--;
            return 1;
        }
        return 0;
    }
    else if(dir == 'D')
    {
        if(x+1 < m && mapp[x+1][y] == 0)
        {
            x++;
            return 1;
        }
        return 0;
    }
    else if(dir == 'L')
    {
        if(y-1>=0 && mapp[x][y-1] == 0)
        {
            y--;
            return 1;
        }
        return 0;
    }
    else if(dir == 'R')
    {
        if(y+1<n && mapp[x][y+1] == 0)
        {
            y++;
            return 1;
        }
        return 0;
    }
}

void DFS(int x, int y, int k)
{
    if(mapp[x][y] == 1)
        return;//起始位置不能是1;
    if(x<0 || x>=m || y<0 || y>=n)
        return ;//不在图内;
    if(k == step)
    {
        flag = 1;
        return ;
    }
    int i;
    for(i  = 1; i < op[k].l; i++)
    {
        if(!movee(x,y,op[k].dir))
            return ;
    }
    for(; i <= op[k].r; i++)
    {
        if(movee(x,y,op[k].dir))
        {
            DFS(x,y,k+1);
            if(flag)
                return ;
        }
        else
            break;
    }
    return ;
}

int main()
{
    int t;
    scanf("%d",&t);
    int a, b;
    char tt;
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                scanf("%d",&mapp[i][j]);
            }
        }
        step = 0;
        while(1)
        {
            scanf("%d%d",&a,&b);
            if(a==0 && b==0)
                break;
            getchar();
            scanf("%c",&tt);
            op[step].l = a;
            op[step].r = b;
            op[step++].dir = tt;
        }
        int ans = 0;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                flag = 0;
                DFS(i,j,0);
                if(flag)
                    ans+=1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-27 07:20:40

POJ 1071 & HDU 1364 & ZOJ 1019 Illusive Chase(DFS)的相关文章

hdu 1364 Illusive Chase (dfs)

# include <stdio.h> # include <string.h> # include <algorithm> # include <iostream> using namespace std; struct node { int x1; int x2; char x[5]; }; struct node a[100010]; int map[110][110]; int n,m,k; bool judge (int xx1,int yy1)

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ

POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater

水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct P1 { int Num; int Hua; } Play1[30]; struct P2 { int Num; int Hua; } Play2[30]; bool cmp1(const P1&a,const P1&b

uva 10129 poj 1386 hdu 1116 zoj 2016 play on words

//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点至多只有两个(分别为入点和出点) 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #inclu

hdu Illusive Chase 1364 dfs

Illusive Chase Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 174    Accepted Submission(s): 74 Problem Description Tom the robocat is presented in a Robotics Exhibition for an enthusiastic au

POJ1607 &amp; HDU 1330 &amp; ZOJ 1216 Deck(数学题)

题目链接: POJ  1607 : http://poj.org/problem?id=1607 HDU 1330 :http://acm.hdu.edu.cn/showproblem.php?pid=1330 ZOJ  1216 : Description A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU 1986 &amp; ZOJ 2989 Encoding(模拟)

题目链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1986 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1988 HDU 1987 & ZOJ 2990 和这题刚好相反,也是比较容易模拟: Chip and Dale have devised an encryption method to hide their (written) text messages

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符