【DFS】HDU 1364 && 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 <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
inline int read()
{
    char ch=' ';
    int ans=0;
    while(ch<'0' || ch>'9')ch=getchar();
    while(ch<='9' && ch>='0')
    {
        ans=ans*10+ch-'0';
        ch=getchar();
    }
    return ans;
}
//inline void print(LL x){printf(LLD, x);puts("");}
bool mp[105][105];
PP step[1005];
int d, m, n;
bool solve(int r, int c,int x)
{
    if(r>=m || c>=n)
        return false;
    if(r<0 || c<0)
        return false;
    if(x==d)
        if(!mp[r][c])
            return true;
        else return false;
    int a=step[x].second.first;
    int b=step[x].second.second;
    //bool flag=0;
    if(step[x].first==0)
    {
        for(int i=c-1; i>=c-a; i--)
        {
            if(i<0)
                return false;
            if(mp[r][i])
                return false;
        }
        for(int i=c-a; i>=c-b; i--)
        {
            if(solve(r,i,x+1))
                return true;
        }
    }
    if(step[x].first==1)
    {
        for(int i=c+1; i<=c+a; i++)
        {
            if(i>=n)
                return false;
            if(mp[r][i])
                return false;
        }
        for(int i=c+a; i<=c+b; i++)
        {
            if(solve(r,i,x+1))
                return true;
        }
    }
    if(step[x].first==2)
    {
        for(int i=r-1; i>=r-a; i--)
        {
            if(i<0)
                return false;
            if(mp[i][c])
                return false;
        }
        for(int i=r-a; i>=r-b; i--)
        {
            if(solve(i,c,x+1))
                return true;
        }
    }
    if(step[x].first==3)
    {
        for(int i=r+1; i<=r+a; i++)
        {
            if(i>=m)
                return false;
            if(mp[i][c])
                return false;
        }
        for(int i=r+a; i<=r+b; i++)
        {
            if(solve(i,c,x+1))
                return true;
        }
    }
    return false;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t=read();
    while(t--)
    {
        m=read(), n=read();
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                mp[i][j]=read();
        d=0;
        while(true)
        {
            int a=read(), b=read();
            if(a==0 && b==0)
                break;
            char op[2];
            int c;
            scanf("%s", op);
            if(op[0]=='L')
                c=0;
            else if(op[0]=='R')
                c=1;
            else if(op[0]=='U')
                c=2;
            else if(op[0]=='D')
                c=3;
            step[d++]=make_pair(c, make_pair(a, b));
        }
        int ans=0;
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                if(!mp[i][j])
                    if(solve(i, j,0))
                        ans++;
        printf("%d
", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wgwyanfs/p/7060796.html