文章

17

粉丝

166

获赞

6

访问

136.2k

头像
flood fill bfs
P1564 中国科学院大学2021年机试题
发布于2021年2月24日 14:32
阅读数 6.3k

总的来说就是搜索到@,利用bfs将相邻的所有@变为* ,直到全部变为*

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;

const int N = 110;

int d[N][N];
char g[N][N];
int n, m;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

void bfs(int sx, int sy)
{
    queue<PII> q;
    d[sx][sy] = 0;
    g[sx][sy] = '*';
    q.push({sx, sy});
    
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i = 0; i < 8; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];
            if(g[x][y] == '@' && d[x][y] == -1)
            {
                d[x][y] = d[t.first][t.second] + 1;
                g[x][y] = '*';
                q.push({x, y});
            }
        }
    }
}

int main()
{
    while(cin >> n >> m)
    {
        if(n == 0 && m == 0) break;
        memset(d, -1, sizeof d);
        int ans = 0;
        fo...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发