ZOJ 3781 - Paint the Grid Reloaded(缩点dfs+bfs)

题目:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268

题意:

一个n*m的矩阵,上面O表示白色,X表示黑色,改变一个格子的颜色会连同其相连的格子颜色改变,求出最少需要多少步才能是整个方格都变成同一个颜色。

思路;

将颜色相连通的格子用dfs缩成一个点,并将相邻的不同颜色的连同块连边,形成一个无向图。

枚举每一个连同块进行bfs,求出走完整个图需要的最少步数即为答案。

AC.

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;
const int INF = 0x3f3f3f3f;
int n, m;
char a[50][50];
int num[1800], vis[1800];
vector<int> g[1800];
int dx[]={0, 0, -1, 1}, dy[]={1, -1, 0, 0};

void init()
{
    memset(vis, 0, sizeof(vis));
    memset(num, -1, sizeof(num));
    for(int i = 0; i <= n*m; ++i) {
        g[i].clear();
    }
}
void dfs(int x, int y, char c, int p)
{
    for(int i = 0; i < 4; ++i) {
        int nx = x+dx[i], ny = y+dy[i];
        if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
         if(a[nx][ny] == c) {
            if(num[nx*m+ny] == -1) {
                num[nx*m+ny] = p;
                dfs(nx, ny, c, p);
            }
        }
        else if(num[nx*m+ny] != -1){
            int tmp = num[nx*m+ny];
            g[p].push_back(tmp);
            g[tmp].push_back(p);
        }
    }
}
struct node {
    int x, sp;
//    node(int xx, int ss) {
//        x = xx; sp = ss;
//    }
};
int bfs(int s)
{
    queue<node> que;
    node no;
    que.push((node){s, 0});
    vis[s] = 1;

    int res = 0;
    while(!que.empty()) {
        no = que.front(); que.pop();
        //printf("%d %d\n", no.x, no.sp);
        //res = no.sp;
        res = max(res, no.sp);


        for(int i = 0; i < g[no.x].size(); ++i) {
            int cnt = g[no.x][i];
            if(vis[cnt] == 0) {
                vis[cnt] = 1;
                que.push((node){cnt, no.sp+1});
            }
        }
        //printf("res = %d\n", res);
    }
    //printf("res = %d\n", res);
    return res;
}
int main()
{
    //freopen("in", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; ++i) {
            scanf("%s", a[i]);
        }

        int cnt = 0;
        init();

        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if(num[i*m+j] == -1) {
                    num[i*m+j] = cnt;
                    dfs(i, j, a[i][j], cnt);
                    cnt++;
                }
            }
        }
//        printf("%d\n", cnt);
//        for(int i = 0; i < g[1].size(); ++i) {
//            printf("%d ", g[1][i]);
//        }
//        printf("\n");

        int ans = INF;
        for(int i = 0; i < cnt; ++i) {
            memset(vis, 0, sizeof(vis));
            ans = min(ans, bfs(i));
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值