zoukankan      html  css  js  c++  java
  • Paint the Grid Reloaded ZOJ

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

    []   [Go Back]   [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


      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <algorithm>
      5 #include <math.h>
      6 #include <map>
      7 #include <queue>
      8 #include <vector>
      9 using namespace std;
     10 vector<int> c[1700];
     11 char a[60][60];
     12 int b[60][60],n,m,bcnt,vi[1700];
     13 int w[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
     14 void dfs(int r,int c1,int x)
     15 {
     16     b[r][c1]=x;
     17     int i,rr,cc;
     18     for(i=0; i<4; i++)
     19     {
     20         rr=r+w[i][0];
     21         cc=c1+w[i][1];
     22         if(rr<n&&rr>=0&&cc<m&&cc>=0&&!b[rr][cc]&&a[rr][cc]==a[r][c1])
     23         {
     24             dfs(rr,cc,x);
     25         }
     26     }
     27 }
     28 void build()
     29 {
     30     int i,j,k,rr,cc,x,y;
     31     for(i=0; i<=bcnt; i++)c[i].clear();
     32     map<pair<int,int>,int>e;
     33     e.clear();
     34     for(i=0; i<n; i++)
     35     {
     36         for(j=0; j<m; j++)
     37         {
     38             for(k=0; k<4; k++)
     39             {
     40                 rr=i+w[k][0];
     41                 cc=j+w[k][1];
     42                 //
     43                 if(rr<n&&rr>=0&&cc<m&&cc>=0&&b[rr][cc]!=b[i][j])
     44                 {
     45                     x=b[rr][cc],y=b[i][j];
     46                     if(x>y)swap(x,y);
     47                     e.insert(make_pair(make_pair(x,y),1));
     48                 }
     49             }
     50         }
     51     }
     52     for(map<pair<int,int>,int>::iterator it=e.begin();it!=e.end();it++)
     53      {
     54          x=(*it).first.first,y=(*it).first.second;
     55          c[x].push_back(y);
     56          c[y].push_back(x);
     57          //cout<<(*it).first.first<<" "<<(*it).first.second<<endl;
     58      }
     59 }
     60 void DFS()
     61 {
     62     memset(b,0,sizeof(b));
     63     int i,j;
     64     bcnt=1;
     65     for(i=0; i<n; i++)
     66     {
     67         for(j=0; j<m; j++)
     68         {
     69             if(!b[i][j])
     70                 dfs(i,j,bcnt++);
     71         }
     72     }
     73     build();
     74     /* for(i=0; i<n; i++)
     75     {
     76         for(j=0; j<m; j++)
     77         {
     78             cout<<b[i][j]<<" ";
     79         }
     80         cout<<endl;
     81     }*/
     82 }
     83 int solve(int x)
     84 {
     85     memset(vi,0,sizeof(vi));
     86     queue<pair<int,int> >q;
     87     while(!q.empty())q.pop();
     88     pair<int,int>now;
     89     q.push(make_pair(x,0));
     90     vi[x]=1;
     91     int i;
     92     now.second=0;
     93     while(!q.empty())
     94     {
     95         now=q.front();
     96         q.pop();
     97         for(i=0;i<c[now.first].size();i++)
     98         {
     99             if(!vi[c[now.first][i]])
    100             q.push(make_pair(c[now.first][i],now.second+1)),vi[c[now.first][i]]=1;
    101         }
    102     }
    103     return now.second;
    104 }
    105 int main()
    106 {
    107     int t,i,mina;
    108     scanf("%d",&t);
    109     while(t--)
    110     {
    111         scanf("%d%d",&n,&m);
    112         for(i=0; i<n; i++)scanf("%s",a[i]);
    113         DFS();
    114         mina=1666666;
    115         for(i=1;i<bcnt;i++)
    116         mina=min(mina,solve(i));
    117         printf("%d
    ",mina);
    118     }
    119 }
    View Code
  • 相关阅读:
    Apache基本设置
    主流无线传输技术GPRS与CDMA之对比
    光波分复用系统(WDM)技术要求
    IPv6报头结构以及与IPv4的比较
    网络设计师训练资料
    802.11b/11a/11g横向比较
    交换机术语
    无线局域网技术白皮书
    无线网络基础知识
    校验码
  • 原文地址:https://www.cnblogs.com/ERKE/p/3675984.html
Copyright © 2011-2022 走看看