开关问题。状态压缩入门
题意:有一个n*m的棋盘,0表示白色,1表示黑色。每次可以翻转当前位置,它的上下左右四个位置也会被相应翻转。问最少翻转多少次会使所有棋面显示为白色,并给出需要翻转的位置,0表示不翻转,1表示翻转。
思路:可以利用第一层的 2^n 种状态来进行枚举。依次翻转到最后一行,如果最后一行均为白色,那么这种第一层状态可以达到效果。记录下来,最后取最少翻转次数的结果输出。
Description
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 N space-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
知识补充: 1.运算符优先级
2.C++ memcpy()函数用法
memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度;
例:
char a[100], b[50];
memcpy(b, a,sizeof(b)); //注意如用sizeof(a),会造成b的内存地址溢出。
strcpy就只能拷贝字符串了,它遇到'\0'就结束拷贝;例:
char a[100], b[50];
strcpy(a,b);
memcpy指的是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
3.位运算符
^ 异或 。相同位为0,不同位为1
&按位与 。都为1,才为1,不然为0
| 按位或 。都为0,才为0,不然为1
(n>>k)&1表示取出整数n在二进制表示下的第k位
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int dirx[5]={0,1,-1,0,0};
int diry[5]={0,0,0,1,-1};
const int maxn=16;
const int inf=0x3f3f3f;
int maze[maxn][maxn];
int vis[maxn][maxn];
int answer[maxn][maxn];
int n,m;
bool judge(int x,int y){
//num最先表示这一个元素是白色还是黑色
//然后判断它周围四边翻转的次数,举个例子,如果它本身是黑色,
//它的上面翻转一次,右面翻转一次,左面和下面没有翻转的话,它还是黑色。
//换句话来说,如果它是黑色,翻转奇数次,它会变成白色,翻转偶数次会变成黑色。
int num=maze[x][y];
for(int i=0;i<5;i++){
int xx=x+dirx[i];
int yy=y+diry[i];{
if(xx>=0&&xx<n&&yy>=0&&yy<m)
num+=vis[xx][yy];
}
}
return num&1;//等同于num%2==1
}
int del(){
for(int i=1;i<n;i++){
//从第二行开始枚举
for(int j=0;j<m;j++){
if(judge(i-1,j)){
vis[i][j]=1;
}
}
}
for(int i=0;i<m;i++){
if(judge(n-1,i))return -1;
//最后一行如果不全是白色,说明这一状态不符合
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
ans+=vis[i][j];
}
return ans;
}
void solve(){
int res=-1;
for(int i=0;i<1<<m;i++){
memset(vis,0,sizeof(vis));
for(int j=0;j<m;j++){
vis[0][j]=i>>j&1;
//枚举第一行的状态
//表示取出整数i在二进制表示下的第j位
}
int ans=del();
if(ans>=0&&(ans<res||res<0)){
res=ans;
memcpy(answer,vis,sizeof(vis));
}
}
if(res<0)printf("IMPOSSIBLE\n");
else
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(j==m-1)
printf("%d\n",answer[i][j]);
else
printf("%d ",answer[i][j]);
}
}
}
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
scanf("%d",&maze[i][j]);
}
solve();
}
return 0;
}