ZOJ Paint the Grid Reloaded

Description

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 Aand 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

这道题真的是一道图论和搜索的经典题目,而且此问题真的很难想到。

题意:

有两种字符X O,你可以操作X字符变成O(与操作字符相邻的所有相同字符都变成O,或者O变成X),需要求的是最小操作步数。

题解:

这种我也是看别人题解才会。

将所有X和O的连通压缩成点,然后X连通块和O连通块相邻就可以连边,形成一个图,

然后以每条边为起点找到其他点最短路(长度为1)的最大值,找其中的最大值中的最小值。

为什么可以这样:

因为建图是以X的连通块和O的连通块相邻连边的,所以变一次就可以和相邻边的字符相同,然后再变一次就可以和所有距离为2的边变成一样的字符了。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 45, N = maxn*maxn;
int n, m, vis[maxn][maxn], color, d[N];
char str[maxn][maxn];
bool fig[N][N], use[N];
int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, 1, 0, -1 };
int next[N*N], to[N*N], head[N*N], pos;
int check ( int x, int y )
{
    return x < 0 || x >= n || y < 0 || y >= m;
}
void dfs ( int x, int y, int color )
{
    vis[x][y] = color;
    for ( int i = 0; i < 4; i ++ )
    {
        int nx = x+dx[i];
        int ny = y+dy[i];
        if ( check ( nx, ny ) || vis[nx][ny] || str[x][y] != str[nx][ny] )
            continue ;
        dfs ( nx, ny, color );
    }
}
void addEdge ( int u, int v )   //要用邻接表 矩阵TLE
{
    to[pos] = v;
    next[pos] = head[u];
    head[u] = pos ++;
}
void BuildEdge ( int x, int y, int nx, int ny )
{
    int p = vis[x][y], np = vis[nx][ny];
    if ( p == np || fig[p][np] )
        return ;
    fig[p][np] = fig[np][p] = true;
    addEdge ( p, np );
    addEdge ( np, p );
}
void Build ( )
{
    color = 1;
    memset ( vis, 0, sizeof ( vis ) );
    for ( int i = 0; i < n; i ++ )
    {
        for ( int j = 0; j < m; j ++ )
        {
            if ( vis[i][j] )
                continue ;
            dfs ( i, j, color );    //标记连通块
            color ++;
        }
    }
    memset ( fig, false, sizeof ( fig ) );
    memset ( head, -1, sizeof ( head ) );
    pos = 0;
    for ( int i = 0; i < n; i ++ )
    {
        for ( int j = 0; j < m; j ++ )
        {
            for ( int k = 0; k < 4; k ++ )
            {
                int nx = i+dx[k], ny = j+dy[k];
                if ( check ( nx, ny ) )
                    continue ;
                BuildEdge ( i, j, nx, ny );
                //相邻的连通块 且值不同的边建图
            }
        }
    }
}
void print ( )
{
    for ( int i = 1; i < color; i ++ )
    {
        for ( int j = 1; j < color; j ++ )
        {
            printf ( "%d ", fig[i][j] );
        }
        printf ( "\n" );
    }
}
void bfs ( int s )
{
    queue < int > q;
    q.push ( s );
    memset ( use, false, sizeof ( use ) );
    memset ( d, 0x3f, sizeof ( d ) );
    d[s] = 0;
    use[s] = true;
    while ( ! q.empty ( ) ) //最短路
    {
        int u = q.front ( );
        q.pop ( );
        for ( int i = head[u]; ~ i; i = next[i] )
        {
            int v = to[i];
            if ( use[v] )
                continue ;
            if ( d[v] > d[u]+1 )
            {
                d[v] = d[u]+1;
                use[v] = true;
                q.push ( v );
            }
        }
    }
}
void solve ( )
{
    int T;
    scanf ( "%d", &T );
    while ( T -- )
    {
        scanf ( "%d%d", &n, &m );
        for ( int i = 0; i < n; i ++ )
            scanf ( "%s", str[i] );
        Build ( );
        //print ( );
        int ans = INF;
        for ( int i = 1; i < color; i ++ )
        {
            bfs ( i );
            int mx = 0;
            for ( int j = 1; j < color; j ++ )
                if ( mx < d[j] )    //其中最大的为步数
                    mx = d[j];
            if ( ans > mx ) //找所有最大步数中最小的
                ans = mx;
        }
        printf ( "%d\n", ans );
    }
}
int main ( )
{
    solve ( );
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值