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)
{
    if(xx1<1||xx1>n||yy1<1||yy1>m)
        return 0;
    return 1;
}
int dfs(int x,int y,int cot)
{
    int i,j;
    int fx,fy;
    if(cot==k)
        return true;
    for(i=1; i<a[cot].x1; i++)
    {
        if(a[cot].x[0]=='R')
            fx=x,fy=y+i;
        else if(a[cot].x[0]=='L')
            fx=x,fy=y-i;
        else if(a[cot].x[0]=='U')
            fx=x-i,fy=y;
        else
            fx=x+i,fy=y;
        if(map[fx][fy])
            return 0;
        if(!judge(fx,fy))
            return 0;
    }
    if(a[cot].x[0]=='R')
    {
        for(i=a[cot].x1; i<=a[cot].x2; i++)
        {
            fy=y+i;
            fx=x;
            if(!judge(fx,fy)||map[fx][fy])
                break;
            if(dfs(fx,fy,cot+1))
                return 1;
        }
    }
    if(a[cot].x[0]=='L')
    {
        for(i=a[cot].x1; i<=a[cot].x2; i++)
        {
            fy=y-i;
            fx=x;
            if(!judge(fx,fy)||map[fx][fy])
                break;
            if(dfs(fx,fy,cot+1))
                return 1;
        }
    }
    if(a[cot].x[0]=='U')
    {
        for(i=a[cot].x1; i<=a[cot].x2; i++)
        {
            fy=y;
            fx=x-i;
            if(!judge(fx,fy)||map[fx][fy])
                break;
            if(dfs(fx,fy,cot+1))
                return 1;
        }
    }

    if(a[cot].x[0]=='D')
    {
        for(i=a[cot].x1; i<=a[cot].x2; i++)
        {
            fy=y;
            fx=x+i;
            if(!judge(fx,fy)||map[fx][fy])
                break;
            if(dfs(fx,fy,cot+1))
                return 1;
        }
    }
    return 0;
}
int main()
{
    int t,i,j,cot1;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(map,0,sizeof(map));
            for(i=1; i<=n; i++)
                for(j=1; j<=m; j++)
                    scanf("%d",&map[i][j]);
            k=0;
            while(1)
            {
                getchar();
                scanf("%d%d",&a[k].x1,&a[k].x2);
                if(a[k].x1==0&&a[k].x2==0)
                    break;
                cin>>a[k].x;
                k++;
            }
            cot1=0;
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=m; j++)
                {
                    if(map[i][j]==0)
                    {
                        if(dfs(i,j,0))
                            cot1++;
                    }
                }
            }
            printf("%d\n",cot1);
        }
    }
    return 0;
}

时间: 2024-10-15 08:03:20

hdu 1364 Illusive Chase (dfs)的相关文章

POJ 1071 &amp; HDU 1364 &amp; 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 audien

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

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

【DFS】HDU 1364 &amp;&amp; POJ 1071 Illusive Chase

数据水了... 不知道正解是什么 将TOM放在一个0上经过输入的  1 2 R 这样走 还能在图上则这个点可行(走的过程中不能走出图) 求有几个0 可行 直接dfs 完全没有别的思路 题目要求必须 走 A - B 步 所以在走A步不能遇到 1 #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cctype> #include &

hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci 问题描述 斐波那契数列的递归定义如下: Fi=???01Fi?1+Fi?2i = 0i = 1i > 1 现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积. 输入描述 有多组数据,第一行为数据组数T(T≤100,000). 对于每组数据有一个整数n,表示要判断的数字. 0≤n≤1,000,000,000 输出描述 对于每组数据,如果可以输出"Yes",否则输出"No". 输入样例 3 4 17 233 输出样例

HDU 3158 PropBot(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3158 Problem Description You have been selected to write the navigation module for PropBot. Unfortunately, the mechanical engineers have not provided a lot of flexibility in movement; indeed, the PropBot

hdu 4499 Cannon 暴力dfs搜索

Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 338 Problem Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move

Hdu 1175 连连看(DFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu 1728相比,都要考虑转弯次数,所以在判断转弯的次数上,两者可以相互借鉴. 这一点应该不难想到,在搜索前就应判断两点的值是否相等,以及两点的值中是否有0.如果不相等或其中一值为0,则可以判断是"NO". 时间虽是10000ms,但只是这样,还是超时. 后来又加了一个数组walk[][],用