WebService之CXF註解之三(Service介面實作類別)

來源:互聯網
上載者:User

標籤:style   blog   class   code   ext   color   

Paint the Grid ReloadedTime 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 cellC 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 <= N, M <= 40). Then N lines follow. Each line contains a string withN 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
22 2OXOX3 3XOXOXOXOX
Sample Output
12
Hint

For the second sample, one optimal solution is:

Step 1. flip (2, 2)

XOXOOOXOX

Step 2. flip (1, 2)

XXXXXXXXX



題意:

給你一個染色的 N×M 的格子,你每次可以選擇一個連通塊,將其顏色取反。問最後將整個格子變為同色的最小步數。


思路:

其實就是一個最短路,將連通塊縮點然後不同種的連通塊之間連邊,那麼將整個格子變為同色的最小步數就等於一個點到地圖上最遠點的距離了。


代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:1024000000,1024000000")#define maxn 45#define MAXN 2700005#define OO (1<<31)-1#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,cnt,lev;char mp[maxn][maxn];int num[maxn][maxn];int dx[]={0,0,-1,1};int dy[]={-1,1,0,0};bool vis[maxn][maxn],app[1605][1605];int p[1700];struct Node{    int v;    int next;}edge[MAXN];void addedge(int u,int v){    cnt++;    edge[cnt].v=v;    edge[cnt].next=p[u];    p[u]=cnt;}bool isok(int x,int y){    if(x<1||x>n||y<1||y>m) return false ;    return true ;}void dfs(int x,int y){    num[x][y]=lev;    int nx,ny,i;    for(i=0;i<4;i++)    {        nx=x+dx[i]; ny=y+dy[i];        if(isok(nx,ny)&&!vis[nx][ny]&&mp[nx][ny]==mp[x][y])        {            vis[nx][ny]=1;            dfs(nx,ny);        }    }}void presolve(){    memset(p,0,sizeof(p));    int i,j,k,t,nx,ny;    lev=0;    memset(vis,0,sizeof(vis));    for(i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            if(!vis[i][j])            {                lev++;                vis[i][j]=1;                dfs(i,j);            }        }    }    cnt=0;    memset(app,0,sizeof(app));    for(i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            for(k=0;k<4;k++)            {                nx=i+dx[k]; ny=j+dy[k];                if(isok(nx,ny)&&num[nx][ny]!=num[i][j]&&!app[num[nx][ny]][num[i][j]])                {                    app[num[nx][ny]][num[i][j]]=1;                    addedge(num[nx][ny],num[i][j]);                }            }        }    }}struct fuck{    int dis;    int num;}t,f;bool VVV[2700];queue<fuck> Q;int bfs(int now){    memset(VVV,0,sizeof(VVV));    t.dis=0;    t.num=now;    VVV[now]=1;    while(!Q.empty()) Q.pop();    Q.push(t);    int maxs=0;    while(!Q.empty())    {        t=Q.front();Q.pop();        if(t.dis>ans) return INF;        for(int i=p[t.num];i!=0;i=edge[i].next)        {            int to=edge[i].v;            if(!VVV[to])            {                VVV[to]=1;                f.num=to;                f.dis=t.dis+1;                maxs=max(maxs,f.dis);                Q.push(f);            }        }    }    return maxs;}int main(){    int i,j,t,u,v,w;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)        {            scanf("%s",mp[i]+1);        }        presolve();        ans=INF;        for(int i=1;i<=lev;i++)        {            ans=min(ans,bfs(i));        }        printf("%d\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.