hdu2102(WA代码)

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;

int n,m,limit;
int map[2][13][13];
int flag[2][13][13];
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
int f_s,x_s,y_s;
int f_e,x_e,y_e;
struct node
{
    int f;
    int x,y;
    int step;
};

int Judge(int f,int x,int y)
{
    if(x<0 || x>=n || y<0 || y>=m)	return -1;
    if(map[f][x][y]==-1)			return -1;
    if(flag[f][x][y])				return -1;
    return map[f][x][y];
}
int BFS()
{
    queue<node>q;
    node cur,next;
    int i;
    int t,f_t;

    memset(flag,0,sizeof(flag));
    cur.f=f_s;
    cur.x=x_s;
    cur.y=y_s;
    cur.step=0;
    flag[cur.f][cur.x][cur.y]=1;
    q.push(cur);

    while(!q.empty())
    {
        cur=q.front();
        q.pop();

        if(cur.step>limit)    break;
        if(cur.f==f_e && cur.x==x_e && cur.y==y_e)    return cur.step;

        for(i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];

            t=Judge(cur.f,next.x,next.y);
            if(t==-1)    continue;
            if(t==0)
            {
                next.f=cur.f;
                next.step=cur.step+1;
                q.push(next);
                flag[next.f][next.x][next.y]=1;
            }
            else if(t==1)
            {
                next.f=cur.f;
                next.step=cur.step+1;
                q.push(next);
                flag[next.f][next.x][next.y]=1;

                if(cur.f==0)    f_t=1;
                else            f_t=0;
                if(Judge(f_t,next.x,next.y)==0)
                {
                    next.f=f_t;
                    next.step=cur.step+1;
                    q.push(next);
                    flag[next.f][next.x][next.y]=1;
                }
            }
        }
    }

    return -1;
}

int main()
{
    int T;
    int f,i,l;
    int ans;
    char str[22];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&limit);
        for(f=0;f<2;f++)
        {
            for(i=0;i<n;i++)
            {
                scanf("%s",str);
                for(l=0;str[l];l++)
                {
                    if(str[l]=='S')		{f_s=f;x_s=i;y_s=l;}
                    else if(str[l]=='P'){f_e=f;x_e=i;y_e=l;}


                    if(str[l]=='S')		map[f][i][l]=0;
                    else if(str[l]=='P')map[f][i][l]=0;
                    else if(str[l]=='.')map[f][i][l]=0;
                    else if(str[l]=='#')map[f][i][l]=1;
                    else if(str[l]=='*')map[f][i][l]=-1;
                }
            }
        }
        for(i=0;i<n;i++)
        {
            for(l=0;l<m;l++)
            {
                if(map[0][i][l]==1 && map[1][i][l]==1)			map[0][i][l]=map[1][i][l]=-1;
                else if(map[0][i][l]==1 && map[1][i][l]==-1)	map[0][i][l]=map[1][i][l]=-1;
                else if(map[0][i][l]==-1 && map[1][i][l]==1)	map[0][i][l]=map[1][i][l]=-1;
            }
        }

        ans=BFS();
        if(ans==-1 || ans>limit)	printf("NO\n");
        else						printf("YES\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值