首页 技术 正文
技术 2022年11月16日
0 收藏 404 点赞 3,219 浏览 2674 个字

Paint the Grid Reloaded

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

[Submit]   [Status]

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 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
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
using namespace std;
vector<int> c[];
char a[][];
int b[][],n,m,bcnt,vi[];
int w[][]= {{,},{-,},{,},{,-}};
void dfs(int r,int c1,int x)
{
b[r][c1]=x;
int i,rr,cc;
for(i=; i<; i++)
{
rr=r+w[i][];
cc=c1+w[i][];
if(rr<n&&rr>=&&cc<m&&cc>=&&!b[rr][cc]&&a[rr][cc]==a[r][c1])
{
dfs(rr,cc,x);
}
}
}
void build()
{
int i,j,k,rr,cc,x,y;
for(i=; i<=bcnt; i++)c[i].clear();
map<pair<int,int>,int>e;
e.clear();
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
for(k=; k<; k++)
{
rr=i+w[k][];
cc=j+w[k][];
//
if(rr<n&&rr>=&&cc<m&&cc>=&&b[rr][cc]!=b[i][j])
{
x=b[rr][cc],y=b[i][j];
if(x>y)swap(x,y);
e.insert(make_pair(make_pair(x,y),));
}
}
}
}
for(map<pair<int,int>,int>::iterator it=e.begin();it!=e.end();it++)
{
x=(*it).first.first,y=(*it).first.second;
c[x].push_back(y);
c[y].push_back(x);
//cout<<(*it).first.first<<" "<<(*it).first.second<<endl;
}
}
void DFS()
{
memset(b,,sizeof(b));
int i,j;
bcnt=;
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(!b[i][j])
dfs(i,j,bcnt++);
}
}
build();
/* for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}*/
}
int solve(int x)
{
memset(vi,,sizeof(vi));
queue<pair<int,int> >q;
while(!q.empty())q.pop();
pair<int,int>now;
q.push(make_pair(x,));
vi[x]=;
int i;
now.second=;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=;i<c[now.first].size();i++)
{
if(!vi[c[now.first][i]])
q.push(make_pair(c[now.first][i],now.second+)),vi[c[now.first][i]]=;
}
}
return now.second;
}
int main()
{
int t,i,mina;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=; i<n; i++)scanf("%s",a[i]);
DFS();
mina=;
for(i=;i<bcnt;i++)
mina=min(mina,solve(i));
printf("%d\n",mina);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856