hdu2531 Catch him(bfs)

hdu2531

题目

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

谈谈

这道题就是你不再是一个点了,有一定的范围了,处理方法就是选取一个点,每次走的时候把你的部分同时判断一下能不能走,同Bfs了。我2在了想用bool类型函数来return 2,不够仔细。

代码

#include <iostream>
#include <cassert>
#include <cstdio>
#include <queue>;
#include <cstring>

using namespace std;

struct node
{
    int x,y;
};

struct node2
{
    int x,y,t;
    node2(int xx,int yy,int tt):x(xx),y(yy),t(tt)
    {

    }
};

int dir[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
char maze[110][110];
int vis[110][110];
int n,m,num;
node defence[25];


int judge(int x,int y)
{
    int tempx=x-defence[0].x;
    int tempy=y-defence[0].y;
    bool flag=0;
    bool flag2=0;
    for(int i=1; i<num; i++)
    {
        int xx=tempx+defence[i].x;
        int yy=tempy+defence[i].y;
        if(xx>n||xx<0||yy>m||yy<0||maze[xx][yy]=='O')
        {
            flag=1;
            break;
        }
        if(maze[xx][yy]=='.')
            continue;
        if(maze[xx][yy]=='Q')
        {
            flag2=1;
        }
    }
    if(flag==1)
        return 0;
    if(flag==0&&flag2==0)
        return 1;
    if(flag==0&&flag2==1)
        return 2;
}

int Bfs()
{
    queue<node2> q;
    while(!q.empty())
        q.pop();

    node2 nd(defence[0].x,defence[0].y,0);
    vis[defence[0].x][defence[0].y]=1;
    q.push(nd);
    while(!q.empty())
    {
        nd=q.front();
        q.pop();
        if(maze[nd.x][nd.y]=='Q')
        {
            return nd.t;
        }
        int xx,yy;
        int tt=nd.t+1;
        for(int i=0; i<4; i++)
        {
            xx=nd.x+dir[i][0];
            yy=nd.y+dir[i][1];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m)
            {
                if(maze[xx][yy]=='O'||vis[xx][yy])
                    continue;
                else
                {
                    int jj=judge(xx,yy);
                    if(jj==0)
                    {
                        vis[xx][yy]=1;
                    }
                    if(jj==1)
                    {
                        node2 nd2(xx,yy,tt);
                        vis[xx][yy]=1;
                        q.push(nd2);
                    }
                    if(jj==2)
                    {
                        return tt;
                    }

                }
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d %d",&n,&m)&&(n!=0&&m!=0))
    {
        memset(vis,0,sizeof(vis));
        char ch;
        num=0;
        for(int i=1; i<=n; i++)
        {
            getchar();
            for(int j=1; j<=m; j++)
            {
                scanf("%c",&ch);
                if(ch=='D')
                {
                    defence[num].x=i;
                    defence[num].y=j;
                    num++;
                    ch='.';
                }
                maze[i][j]=ch;
            }
        }

        int key=Bfs();
        if(key==-1)
            cout<<"Impossible"<<endl;
        else
            cout<<key<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值