ZOJ - 3781 Paint the Grid Reloaded (dfs construction, bfs find the minimum value in the longest path)

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

题意:目的把所有的点变成一个颜色,整个图只有两种颜色,每次只能改变一块颜色相同的点,问最小次数把整个图颜色变一样。

先dfs把颜色一样的建成一个点,然后不相同颜色相邻的连通,重新建图。这样一来就是一个新的图,你每次从一个点出发,找到那个最长的路,而这个最长路其实就是翻转次数,模拟一下。

还要注意,建图的话,数组要开大一点,要不会出错,因为你所过去是n*n的点都要看一遍

#include<stdio.h>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
char mapp[50][50];
int vis[50][50];
struct node
{
    int v;
    int nextt;
}e[50000];
struct node1
{
    int x;
    int step;
};
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
int r,k,cnt;
int n,m;
int first[2000];
int book[2000];
void add(int u,int v)
{
    e[r].v=v;
    e[r].nextt=first[u];
    first[u]=r++;
}
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return 1;
    return 0;
}
void dfs(int x,int y,int cntt)
{
    for(int i=0;i<4;i++)
    {
        int dxx=x+dx[i];
        int dyy=y+dy[i];
        if(judge(dxx,dyy))
        {
            if(mapp[dxx][dyy]==mapp[x][y]&&vis[dxx][dyy]==-1)
            {
                vis[dxx][dyy]=cntt;
                dfs(dxx,dyy,cntt);
            }
            else if(mapp[dxx][dyy]!=mapp[x][y]&&vis[dxx][dyy]!=-1)
            {
                add(cntt,vis[dxx][dyy]);
                add(vis[dxx][dyy],cntt);
            }
        }
    }
}
int bfs(int i)
{
    queue<node1>q;
    while(!q.empty())
        q.pop();
    memset(book,0,sizeof(book));
    node1 st,ed;
    st.x=i;
    st.step=0;
    q.push(st);
    book[i]=1;
    int maxx=-1;
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        maxx=max(maxx,st.step);
        ed.step=st.step+1;
        for(int j=first[st.x];j!=-1;j=e[j].nextt)
        {
            int v=e[j].v;
            if(!book[v])
            {
                book[v]=1;
                ed.x=v;
                q.push(ed);
            }
        }
    }
    return maxx;
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0; i<n; i++)
                scanf("%s",mapp[i]);
            r=0;
            cnt=0;
            memset(vis,-1,sizeof(vis));
            memset(first,-1,sizeof(first));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(vis[i][j]==-1)
                    {
                        vis[i][j]=cnt++;
                        dfs(i,j,cnt-1);
                    }
                }
            }
            int minn=inf,maxx;
            for(int i=0;i<cnt;i++)
            {
                maxx=bfs(i);
                minn=min(minn,maxx);
            }
            printf("%d\n",minn);
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324847901&siteId=291194637