POJ - 3279 Fliptile 【状态压缩、dfs】
作者:互联网
题目简述
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".
输入
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
分析
\(\large\texttt{Ⅰ.如何使全部瓷砖变白}\)
对于第\(i\)行,我们只需要考虑前i-1行的黑白情况。也就是说,对于第i行j列的瓷砖,若其为黑色,我们只需要反转第i+1行第j列的瓷砖即可。
以此类推,当第\(i\)行的每列元素都进行“是否反转本格瓷砖”的操作时,一定能保证第\(i-1\)行一定能变为白色。
故而前\(n-1\)行一定可以变为白色。但若第n行出现黑色瓷砖,此时没有第n+1行,无法翻转来使其变白。
简而言之,若第n行出现黑色瓷砖,则无法使全部瓷砖变白。
\(\large\texttt{Ⅱ.如何判断第i-1行第j列的元素的颜色}\)
假设当前位置为\((i,j)\),我们定义\(\texttt{temp[i][j]}\)为当前格的翻转次数。
这里有一个小技巧,若(i,j)位置瓷砖颜色(白色为0,黑色为1)+前后左右和自己的反转次数为偶数,则第(i,j)位置一定为白色,否则为黑色。
上述结论是基于我们逐行顺序反转瓷砖的情况下得出的,可以证明。
\(\large\texttt{Ⅲ.如何处理第一行}\)
由前文分析,我们对[2,n]行的处理均取决于前一行,故第一行的反转情况影响整个n行的结果。
而我们易知,对于第一行的m个元素,相互反转所形成的组合方案为\(2^m\)个。如果单纯枚举\(2^m\)个方案,显然会超出空间。
故我们对方案进行二进制状态压缩,用i表示每一种方案(\(0\leq i \leq (1<<m)-1\)),将第一行每一列的反转次数\(\texttt{temp[1][j]}\)由十进制转为二进制,具体如下所示:
for(int i=0;i<=(1<<m)-1;i++){//枚举每一种情况,共2^m种
for(int j=0;j<=m-1;j++)
temp[1][m-j]=(i>>j)&1;//十进制转为二进制
每一种枚举方案统计一次反转次数,得出最小的反转次数所对应的方案。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 20
#define inf 0x3f3f3f3f
int temp[N][N];
int m,n,Ans=inf;
const int dir[5][2]={{1,0},{-1,0},{0,0},{0,1},{0,-1}};
int nums[N][N],new_temp[N][N];
int ip(){
int x=0,w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return w?-x:x;
}
int check(int x,int y){
int ans=nums[x][y];
for(int i=0;i<5;i++){
int nx=x+dir[i][0],ny=y+dir[i][1];
if(1<=nx&&nx<=n&&1<=ny&&ny<=m)
ans+=temp[nx][ny];
}
return ans&1;//若为奇数,该点为黑色
}
int dfs(){
int anss=0;
for(int i=2;i<=n;i++)
for(int j=1;j<=m;j++)
if(check(i-1,j)) temp[i][j]=1;//若上一格为黑色,则本格需要反转
for(int j=1;j<=m;j++)
if(check(n,j)) return -1;//若最后一行出现黑色,而没有下一行来翻转其颜色,故无法全白
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(temp[i][j]) anss++;
return anss;
}
void enumerate(){//枚举第一行的2^m种情况
for(int i=0;i<=(1<<m)-1;i++){//枚举每一种情况,共2^m种
memset(temp,0,sizeof(temp));
for(int j=0;j<=m-1;j++)
temp[1][m-j]=(i>>j)&1;//十进制转为二进制
int ans=dfs();
//if(ans==-1) continue;
if(ans<Ans&&ans!=-1) {
Ans=ans;//更新最小反转次数
memcpy(new_temp,temp,sizeof(temp));
}
}
}
int main(){
n=ip(),m=ip();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
nums[i][j]=ip();
enumerate();
if(Ans==inf) printf("IMPOSSIBLE");
else{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
printf("%d%c",new_temp[i][j],j==m?'\n':' ');
}
return 0;
}
标签:ch,int,反转,Fliptile,dfs,瓷砖,POJ,they,white 来源: https://www.cnblogs.com/MrWangnacl/p/16435037.html