ZOJ 3781 Paint the Grid Reloaded 缩点+bfs

Paint the Grid Reloaded

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Leo has a grid with N rows and M columns. All cells are painted with either black or white initially.

Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B.

Leo wants to paint the grid with the same color. He can make it done in multiple steps. At each step Leo can choose a cell and flip the color (from black to white or from white to black) of all cells connected to it. Leo wants to know the minimum number of steps he needs to make all cells in the same color.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N and M (1 <= NM <= 40). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the initial color of the cells.

Output

For each test case, output the minimum steps needed to make all cells in the same color.

Sample Input
2
2 2
OX
OX
3 3
XOX
OXO
XOX
Sample Output
1
2
Hint

For the second sample, one optimal solution is:

Step 1. flip (2, 2)

XOX
OOO
XOX

Step 2. flip (1, 2)

XXX
XXX
XXX



相当于每次从一个联通块只能到达相邻的不同颜色的联通块,由于棋盘是二维的,对于每个联通块跑一遍bfs就可以找到离他最远的联通块要几次操作才能得到,为了加速bfs要缩点。水题..


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int N = 40 + 10;
char g[N][N];
int n, m;
int vis[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

set<int>Map[1600 + 10];
struct Edge{int to, nxt;}e[(1610 * 4) << 1];
int head[1610];
int tot;
void initGraph(){
    tot = 0;
    memset(head, 0, sizeof head);
}
void add(int u, int v){
    e[++tot].to = v; e[tot].nxt = head[u]; head[u] = tot;
    e[++tot].to = u; e[tot].nxt = head[v]; head[v] = tot;
}
int sz;
void dfs(int i, int j, char t){
    int x, y;
    for(int k = 0; k < 4; k++){
        x = i + dx[k]; y = j + dy[k];
        int u = sz, v = vis[x][y];
        if (v && g[x][y] != t && !Map[u].count(v)){
            Map[u].insert(v); Map[v].insert(u);
            add(u, v);
        }else if (!v && g[x][y] == t){
            vis[x][y] = sz;
            dfs(x, y, t);
        }
    }
}

int d[1600 + 10];
int bfs(int k){
    int re = 0;
    queue<int>q;
    int x, y;
    memset(d, 0, sizeof d);
    q.push(k);
    d[k] = 1;
    while(!q.empty()){
        x = q.front(); q.pop();
        for(int i = head[x]; i; i = e[i].nxt){
            y = e[i].to;
            if (d[y]) continue;
            q.push(y);
            d[y] = d[x]+1;
            re = max(re, d[y] - 1);
        }
    }
    return re;
}

int main()
{
//    freopen("data.in", "r", stdin);
//    freopen("data.out", "w", stdout);
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++){
            scanf("%s", g[i]+1);
        }
        for(int i = 1; i <= n; i++){
            g[i][0] = g[i][m+1] = '#';
        }
        for(int i = 1; i <= m; i++){
            g[0][i] = g[n+1][i] = '#';
        }
        sz = 0;
        memset(vis, 0, sizeof vis);
        for(int i = 0; i < 1610; i++) Map[i].clear();
        initGraph();
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if (vis[i][j]) continue;
                vis[i][j] = ++sz;
                dfs(i, j, g[i][j]);
            }
        }
        int ans = sz;
        for(int i = 1; i <= sz; i++){
            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、付费专栏及课程。

余额充值