hdu-3345-War Chess(bfd,条件众多)

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


一开始用队列老是超内存,后来改用数组做了


#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct node{
    int x,y,mv;
}Tnode[1000015];
int r,c,mv,t;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
char mat[105][105]; int vis[105][105];

bool isE(const int &x, const int &y){
    int newx,newy;
    for(int i=0; i<4; i++){
        newx = x + dir[i][0];
        newy = y + dir[i][1];
        if(newx<0 || newx>r-1 || newy<0 ||newy>c-1) continue;
        if(mat[newx][newy]=='E') return true;
    }
    return false;
}
int getMv(int mv, int x, int y){
    if(mat[x][y]=='R') mv-=3;
    else if(mat[x][y]=='T') mv-=2;
    else mv-=1;
    if(mv>0 && isE(x,y)) mv=0;
    return mv;
}
void bfs(node s){
    memset(vis,-1,sizeof(vis));
    Tnode[0]=s;
    int start=0, end = 1;
    node head,next;
    vis[s.x][s.y] = s.mv;
    while(start<end){
        head = Tnode[start++];
        if(head.mv<1) continue;
        for(int i=0; i<4; i++){
            next.x = head.x + dir[i][0];
            next.y = head.y + dir[i][1];
            if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1) continue;
            if(mat[next.x][next.y]=='Y' || mat[next.x][next.y]=='#' || mat[next.x][next.y]=='E')
                continue;
            int tepmv = getMv(head.mv, next.x, next.y);
            if(tepmv>vis[next.x][next.y]){
                next.mv = tepmv;
                vis[next.x][next.y] = tepmv;
                if(mat[next.x][next.y]!='P') mat[next.x][next.y]='*';
                Tnode[end++] = next;
            }
        }
    }
}
int main()
{
    node s;
    cin>>t;
    while(t--){
        cin>>r>>c>>mv;
        memset(mat,'\0',sizeof(mat));
        for(int i=0; i<r; i++)
            for(int j=0; j<c; j++){
                cin>>mat[i][j];
                if(mat[i][j]=='Y'){
                    s.x = i;
                    s.y = j;
                    s.mv = mv;
                }
            }
        bfs(s);
        for(int i=0; i<r; i++)
            cout<<mat[i]<<endl;
        cout<<endl;
    }
    return 0;
}


贴上一开始的版本

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct node{
    int x,y,mv;
};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
char mat[100][100]; bool vis[100][100];
int newx,newy;
bool isE(int x, int y){
    for(int i=0; i<4; i++){
        newx = x + dir[i][0];
        newy = y + dir[i][1];
        if(mat[newx][newy]=='E') return true;
    }
    return false;
}
queue<node> Q;
int Q_size;
void bfs(node s, int r, int c){
    memset(vis,false,sizeof(vis));
    Q.push(s);
    node head,next;
    vis[s.x][s.y] = true;;
    while(!Q.empty()){
        Q_size = Q.size();
        while(Q_size--){
            head = Q.front();
            Q.pop();
            if(head.mv<1) continue;
            for(int i=0; i<4; i++){
                next.x = head.x + dir[i][0];
                next.y = head.y + dir[i][1];
                if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1) continue;
                if(mat[next.x][next.y]=='T' && head.mv<2) continue;
                if(mat[next.x][next.y]=='R' && head.mv<3) continue;
                if(mat[next.x][next.y]=='#' || mat[next.x][next.y]=='E') continue;
                if(mat[next.x][next.y]=='.' || mat[next.x][next.y]=='P'){
                    if(vis[next.x][next.y]) next.mv = max(head.mv-1,next.mv);
                    else next.mv = head.mv-1;
                }else if(mat[next.x][next.y]=='R'){
                    if(vis[next.x][next.y]) next.mv = max(head.mv-3,next.mv);
                    else next.mv = head.mv-3;
                }else if(mat[next.x][next.y]=='T'){
                    if(vis[next.x][next.y]) next.mv = max(head.mv-2,next.mv);
                    else next.mv = head.mv-2;
                }
                if(isE(next.x,next.y))
                    next.mv = 0;
                if(mat[next.x][next.y]=='.' || mat[next.x][next.y]=='T' || mat[next.x][next.y]=='R')
                    mat[next.x][next.y] = '*';
                Q.push(next);
                vis[next.x][next.y] = true;
            }
        }
    }
}
int main()
{
    int r,c,mv,t;
    cin>>t;
    node s;
    while(t--){
        cin>>r>>c>>mv;
        memset(mat,'\0',sizeof(mat));
        for(int i=0; i<r; i++)
            for(int j=0; j<c; j++){
                cin>>mat[i][j];
                if(mat[i][j]=='Y'){
                    s.x = i;
                    s.y = j;
                    s.mv = mv;
                }
            }
        bfs(s,r,c);
        for(int i=0; i<r; i++){
            cout<<mat[i];
            cout<<endl;
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值