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

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

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。

解:每个棋子翻一下,那么和他相连的相同的都会翻转 ,那么比如 OX ,第1个O翻转一下,他就和和他距离为1的X相同,

比如 OXO,第一个 O翻1下 和他距离为1的X相同,翻2下,和他距离为2的O相同 

那么 OOX中第1个和第二个O距离为多少?显然是0,因为他们是连在1起的,同时翻转

我们可以先dfs进行缩点,所有相连的记为同一个点 ,然后通过bfs为每两个相邻的点建边,长度为1

最后我们对每个点进行spfa ,找出该点到所有点长度的最大值 , 从所有的最大值中找1个最小值就是我们要求的翻转次数

比如  

OXO                 1 2 3             1号点   2  4     2号点   1 3 5        3号点     2  6                

XOX    缩点      4 5 6    建边    4号点  1 5 7   5号点   2 4 6 8      6号点  3 5 9       spfa

OXO                7 8 9               7号点  4 8      8号点   5 7 9         9号点 6 8



0 1 2 1 2 3 2 3 4
1 0 1 2 1 2 3 2 3
2 1 0 3 2 1 4 3 2
1 2 3 0 1 2 1 2 3
2 1 2 1 0 1 2 1 2
3 2 1 2 1 0 3 2 1
2 3 4 1 2 3 0 1 2
3 2 3 2 1 2 1 0 1
4 3 2 3 2 1 2 1 0

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<algorithm>
#include<map>d
#define inf 0x3f3f3f3f
#define ll long long
#define maxx 5000005
using namespace std;
int n,m;
char aa[50][50];//原图
int a[50][50];//缩点后的图
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int go(int x,int y) //判断是否越界
{
    if(0<=x&&x<n&&0<=y&&y<m)
        return 1;
    return 0;
}
int cnt;
void dfs(int x,int y,char c) //dfs缩点
{
    int xx,yy;
    a[x][y]=cnt;
    for(int i=0;i<4;i++)
    {
        xx=x+dir[i][0];
        yy=y+dir[i][1];
        if(go(xx,yy)&&!a[xx][yy]&&aa[xx][yy]==c)
            dfs(xx,yy,c);
    }
}
int tot;
int ans;
struct node//邻接表
{
    int en,next;
}e[10000];
int p[10000];
void add(int x,int y)
{
    e[tot].en=y;
    e[tot].next=p[x];
    p[x]=tot++;
}
void init() //清零
{
    tot=0;
    cnt=1;
    memset(a,0,sizeof(a));
    memset(p,-1,sizeof(p));
    ans=inf;
}
struct no
{
    int x,y;
};
bool v[50][50]; 
bool cc[10000];//边是否已经建过
void bfs(int x,int y,int u) //最短路建图
{
    memset(v,0,sizeof(v));
    memset(cc,0,sizeof(cc));
    no st,ed;
    queue<no>q;
    st.x=x;
    st.y=y;
    v[x][y]=1;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            ed.x=st.x+dir[i][0];
            ed.y=st.y+dir[i][1];
            if(!go(ed.x,ed.y))
                continue;
            if(cc[a[ed.x][ed.y]]) //如果这条边已经建过
                continue;
            if(v[ed.x][ed.y])
                continue;
            v[ed.x][ed.y]=1;
            if(a[ed.x][ed.y]==u) //如果是同一个点,那么放入队列继续搜索临近的点
            {
                q.push(ed);
            }
            else
            {
                cc[a[ed.x][ed.y]]=1;
                add(u,a[ed.x][ed.y]); //不同的点,建边,标记
            }
        }
    }
}
int d[10000];//距离
bool vis[10000];
void spfa(int xx)
{
   memset(d,inf,sizeof(d));
   memset(vis,0,sizeof(vis));
    d[xx]=0; 
   queue<int>q;
   q.push(xx);
   while(!q.empty())
   {
       int x=q.front();
       q.pop();
       if(vis[x]) continue;
       vis[x]=1;
    for(int i=p[x];i+1;i=e[i].next)
   {
       int y=e[i].en;
       if(d[y]>d[x]+1)
       {
           d[y]=d[x]+1;
            q.push(y);
       }
   }
   }
   int uu=0;
   for(int i=1;i<cnt;i++) //选取最大值
    uu=max(uu,d[i]);
   ans=min(uu,ans);//选最小值
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",aa[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(!a[i][j])  //如果还没被标记,那么是新的点
                   {
                    a[i][j]=cnt;
                    dfs(i,j,aa[i][j]);
                    cnt++;
                  }
        int uu=1;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(a[i][j]==uu) //我们 bfs每个点,找到和他临近的点
                {
                    bfs(i,j,uu);
                    uu++;
                }
            }
         for(int i=1;i<cnt;i++)
            spfa(i);
        printf("%d\n",ans);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值