题意:给你一个给你一个矩阵,有黑白两个状态。每次你可以选择一个变为其相反的状态,它周围的4个都会变成相反的状态。问你最少需要改变多少个使得矩阵中的状态全为白色,若有多个答案,输出字典序最小的
思路:白书p153
题解:这个题仍然是个反转问题,我们只需要枚举第一行(二进制)进行翻转的坐标, 然后根据当前这块上面那块是否是黑色(依据该块上面本来是什么以及周围或者自身总共反转了多少次确定)最后得出该块是否需要反转, 最后只需要特判最后一行是否合理即全为白色,并进行维护更新答案即可。
另外其实每个点最多只会被翻转一次,因为如果翻转两次和不翻转是一样的。
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Line 1: Two space-separated integers: M and N
Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with Nspace-separated integers which are 1 for black and 0 for white
Output
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=110;
int m,n;
int mp[M][M];
int s[M][M];//保存中间结果
int w[M][M];///保存最优解,s,w,记录的都是翻动操作
int e[5][2]= {0,0,0,1,1,0,-1,0,0,-1};//领接格子的坐标包括本身
int f(int x,int y)//查询(x,y)的颜色判断该点是否为黑色
{int xx=mp[x][y];for(int i=0; i<5; i++){int u=x+e[i][0],v=y+e[i][1];if(u>=0&&u<m&&v>=0&&v<n)xx+=s[u][v];}return xx%2;
}
int bfs()//求出第一行确定情况下的最小操作,不存在解的话,返回-1
{int ant=0;for(int i=1; i<m; i++)//求出从第二行开始翻转for(int j=0; j<n; j++)//上方格子是黑色,必须必须反转(i,j)号格子if(f(i-1,j))s[i][j]=1;for(int i=0; i<n; i++)//判断最后一行是否全白if(f(m-1,i))return -1;for(int i=0; i<m; i++)//统计翻转的次数for(int j=0; j<n; j++)ant+=s[i][j];return ant;
}
void solve()
{int ans=0x3f3f3f3f;for(int i=0; i<(1<<n); i++)//按字典序尝试第一行的所有可能性{memset(s,0,sizeof(s));for(int j=0; j<n; j++)//给第一行各个位置是否翻动按字典序赋值s[0][n-j-1]=i>>j&1;int num=bfs();if(num>=0&&ans>num){ans=num;memcpy(w,s,sizeof(s));//把dp数组复制给s数组(int数组的复制memcpy)}}if(ans==0x3f3f3f3f)printf("IMPOSSIBLE\n");else{for(int i=0; i<m; i++)///输出翻动操作{for(int j=0; j<n-1; j++)printf("%d ",w[i][j]);printf("%d\n",w[i][n-1]);}}
}
int main()
{while(~scanf("%d%d",&m,&n)){memset(w,0,sizeof(w));for(int i=0; i<m; i++)for(int j=0; j<n; j++)scanf("%d",&mp[i][j]);solve();}return 0;
}