Paint the Grid Reloaded ZOJ - 3781 图论变形

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\n",mina);
118     }
119 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3675984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在某些情况下,numpy模块被重新加载(第二次导入)。这可能会导致一些问题。 当我们导入一个模块时,Python会执行模块中的所有代码,并将其存储在内存中。如果我们在同一脚本中多次导入相同的模块,那么第二次导入实际上是不必要的。这可能发生在以下情况下: 1. 模块被意外地多次导入:在编写代码时,可能会出现错误导致模块在代码中被多次导入。这可能是由于复制粘贴代码时的疏忽或逻辑错误导致的。 2. 代码处于循环中:如果模块导入的代码位于循环中,那么每次循环迭代时模块都会被重新导入。这可能会导致性能问题。 这种重新导入模块的情况可能会导致一些问题: 1. 内存浪费:每次导入模块时,Python都会将模块的代码加载到内存中,如果多次导入,会浪费额外的内存资源。 2. 命名冲突:如果模块中定义了全局变量、类或函数,多次导入可能会导致命名冲突。这可能导致未定义的行为或意外的结果。 为了避免这种情况,我们可以在代码中检查是否已经导入了模块,并只执行一次导入操作。例如,可以使用条件语句来检查模块是否已经导入,并避免重复导入。 另外,如果重复导入是由循环引起的,我们可以考虑重构代码以避免在循环中导入模块。 总之,当numpy模块被重新加载时,可能会出现一些问题,包括内存浪费和命名冲突。为了避免这些问题,我们应该确保只在必要时才导入模块,并避免在循环中导入模块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值