文章

28

粉丝

226

获赞

192

访问

187.1k

头像
请教,不知道哪里错了,感觉思路正确的。毒气动不了人就停也考虑了。
P1124
发布于 2022年6月
阅读数 6.9k

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

const int len = 105;
// 迷宫
char gra[len][len];
// 方向
int dir[4][2] = {-1,0,1,0,0,-1,0,1};

struct node{
    int x,y;
    char v;
};

int bfs(node G, node S, int t){
    int alive = 1;
    vector<queue<node>> que(t+1, queue<node>());
    que[0].push(G);
    que[0].push(S);

    for(int i = 0;i < t;i++){
        int Gflag = 0;
        while(!que[i].empty()){
            node now = que[i].front();
            que[i].pop();

            if(now.v == 'G'){
                for(int j = 0;j < 4;j++){
                    int x = now.x + dir[j][0];
                    int y = now.y + dir[j][1];
                    if(gra[x][y] != 'X' && gra[x][y] != 'G'){
                        Gflag = 1;
                        if(gra[x][y] == 'S'){
                            alive--; // 2,4
                        }
                        gra[x][y] = 'G';
                        que[i+1].push(node{x,y,'G'});
                    }
                }
            }else{
                if(alive <= 0){
                    return 0;
                }
                if(Gflag == 0){
                    return 1;
                }
                // 如果还有存活的就移动
                for(int j = 0;j < 4;j++){
                    int x = now.x + dir[j][0];
                    int y = now.y + dir[j][1];
                    if(gra[x][y] != 'X' && gra[x][y] != 'G'){
                        gra[x][y] = 'S';
                        alive++;
                        que[i+1].push(node{x,y,'S'});
                    }
                }
            }
        }
    }
    return alive;
}

int main()
{
    int T;
    cin >> T;
    for(int k = 1;k <= T;k++){
        int n,m,t;
        cin >> n >> m >> t;
        getchar();
        memset(gra, 'X', sizeof(gra));
        node G,S;
        for(int i = 1;i <= n;i++){
            string str;
            getline(cin, str);
            for(int j = 1;j <= m;j++){
                gra[i][j] = str[j-1];
                if(str[j-1] == 'G'){
                    G.x = i;
                    G.y = j;
                    G.v = 'G';
                }else if(str[j-1] == 'S'){
                    S.x = i;
                    S.y = j;
                    S.v = 'S';
                }
            }
        }

        if(bfs(G,S,t) > 0){
            for(int i = 1;i <= n;i++){
                for(int j = 1;j <= m;j++){
                    cout << gra[i][j];
                }
                cout << endl;
            }
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}

 



登录后发布评论

1 条评论
admin
2022年6月30日 00:46

一旦气体无法扩散,人也就不会走了,可以直接break。如何判断气体无法扩散呢?只要一次扩散过程中气体的格数不再增加即可。

赞(0)
回复给: