hdu 3345 War Chess(BFS+模拟)

大概半年前做过一次这道题,当时一直wa而且想不明白为什么会wa。

今天看见了又做了一次,第一次提交选错了语言ce,换了c++后AC,然后开始怀疑为什么之前写的一直是wa。

简单模拟+BFS。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 105
int vis[N][N];
int m,n,k;
int sx,sy;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char map[N][N];
struct node
{
    int x,y;
    int cost;
    friend bool operator<(node a,node b)
    {
        return a.cost<b.cost;
    };
};
int test(int x,int y)
{
    if(x<1||x>m||y<1||y>n) return 0;
    return 1;
}
int fun(int x,int y)
{
    if(test(x-1,y)&&map[x-1][y]=='E') return 1;
    if(test(x,y-1)&&map[x][y-1]=='E') return 1;
    if(test(x+1,y)&&map[x+1][y]=='E') return 1;
    if(test(x,y+1)&&map[x][y+1]=='E') return 1;
    return 0;
}
int judge(int x,int y)
{
    if(!test(x,y)) return 0;
    if(vis[x][y]==1) return 0;
    if(map[x][y]=='#') return 0;
    if(map[x][y]=='E') return 0;
    return 1;
}
void BFS()
{
    priority_queue<node>q;
    node cur,next;
    cur.x=sx;cur.y=sy;cur.cost=k;;vis[sx][sy]=1;
    //if(fun(sx,sy)) cur.cost=0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(cur.cost==0) continue;
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            next.cost=cur.cost;
            if(judge(next.x,next.y))
            {
                vis[next.x][next.y]=1;
                if(map[next.x][next.y]=='.'&&next.cost>=1) next.cost-=1;
                else if(map[next.x][next.y]=='.') continue;
                else if(map[next.x][next.y]=='T'&&next.cost>=2) next.cost-=2;
                else if(map[next.x][next.y]=='T') continue;
                else if(map[next.x][next.y]=='R'&&next.cost>=3) next.cost-=3;
                else if(map[next.x][next.y]=='R') continue;
                else if(map[next.x][next.y]=='P'&&next.cost>=1) next.cost-=1;
                else if(map[next.x][next.y]=='P') continue;
                if(fun(next.x,next.y)) next.cost=0;
                if(map[next.x][next.y]!='P') map[next.x][next.y]='*';
                q.push(next);
            }
        }
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&m,&n,&k);
        getchar();
        for(int i=1;i<=m;i++)
        {
            gets(map[i]+1);
            for(int j=1;j<=n;j++)
                if(map[i][j]=='Y') sx=i,sy=j;
        }
        memset(vis,0,sizeof(vis));
        BFS();
        for(int i=1;i<=m;i++) puts(map[i]+1);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值