zoj 1019 Illusive Chase --- dfs

永远都看不清楚nx ny抓狂



#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;

int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};

struct node
{
    int a,b;
    char dir;
}op[10010];

int t,n,m,cnt,mp[105][105],ans;
char s[10];

int dfs(int x,int y,int step)
{
    int nx,ny,i,aa,bb,flag;
    if(step==cnt) return 1;
    aa=op[step].a;
    bb=op[step].b;
    for(i=aa;i<=bb;i++)
    {
        switch(op[step].dir)
        {
            case 'L':nx=x;ny=y-i;break;
            case 'R':nx=x;ny=y+i;break;
            case 'U':nx=x-i;ny=y;break;
            case 'D':nx=x+i;ny=y;
        }

        if(nx<0||nx>=n||ny<0||ny>=m) continue;
   //     printf("i:%d x;%d y:%d nx:%d ny:%d\n",i,x,y,nx,ny);
        flag=1;
        if(op[step].dir=='D')
        {
            for(int j=x;j<=nx;j++)
            {
                if(mp[j][y])
                {
                    flag=0;
                    break;
                }
            }
        }
        else if(op[step].dir=='R')
        {
            for(int j=y;j<=ny;j++)
            {
                if(mp[x][j])
                {
                    flag=0;
                    break;
                }
            }
        }

        else if(op[step].dir=='U')
        {
            for(int j=x;j>=nx;j--)
            {
                if(mp[j][y])
                {
                    flag=0;
                    break;
                }
            }
        }

        else if(op[step].dir=='L')
        {
            for(int j=y;j>=ny;j--)
            {
                if(mp[x][j])
                {
                    flag=0;
                    break;
                }
            }
        }

        if(flag)
        {
            if(dfs(nx,ny,step+1))
                return 1;
        }
    }
    return 0;
}


int main()
{
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(mp,0,sizeof mp);
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        cnt=0;
        while(scanf("%d%d",&op[cnt].a,&op[cnt].b)&&(op[cnt].a||op[cnt].b))
        {
            scanf("%s",s);
          //  printf("op:%c\n",s[0]);
            op[cnt].dir=s[0];
            cnt++;
        }
        ans=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                if(!mp[i][j])
                    if(dfs(i,j,0)){
                        ans++;
               //         printf("i:%d j:%d\n",i,j);
                    }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值