Fliptile
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 anM × 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".
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
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
翻格子游戏,最终要全部变成0,要求输出翻转的方法。
方法是:
枚举翻转的第一行,比如样例中的,需要从 0000 到 1111,有了第一行,可以写出翻转后的结果,根据这个(由于只能用二行去改变第一行),可以以此列举出后面翻转后的结果,根据翻转的结果推出改翻转的二维数组,最后检查最后一行是否全部为0.
#include <iostream>
using namespace std;
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define inf 0x3f3f3f3f
int row,col,cnt;
int maximum = inf;
int m[16][16];
int t[16][16],ans[16][16],big[16][16];
void change( int r, int c )
{
t[r][c] = !t[r][c];
if( r != 0 )
t[r-1][c] = !t[r-1][c];
if( r != row )
t[r+1][c] = !t[r+1][c];
if( c != 1 )
t[r][c-1] = !t[r][c-1];
if( c != col )
t[r][c+1] = !t[r][c+1];
}
bool judge( int x )
{
cnt = 0;
memcpy(t,m,sizeof(m));
memset(ans,0,sizeof(ans));
for( int i = col ; i >= 1 ; i-- )
{
if( x>>(col-i) & 1 == 1 )
{
change(1,i);
cnt++;
ans[1][i] = 1;
}
}
for( int i = 2 ; i <= row; i++ )
{
for( int j = 1 ; j <= col ; j++ )
if( t[i-1][j] == 1 )
{
change(i,j);
cnt++;
ans[i][j] = 1;
}
}
for( int i = 1 ; i <= col ; i++ ) //检查最后一行
{
if( t[row][i] == 1 )
return false;
}
return true;
}
int main()
{
while( scanf("%d %d",&row,&col) == 2 && row && col )
{
bool flag = false;
maximum = inf;
for( int i = 1; i <= row ; i++ )
for( int j = 1; j <= col ; j++ )
scanf("%d",&m[i][j]);
for( int i = 0 ; i < (1<<col) ; i++ )
{
if( judge(i) && cnt < maximum )
{
maximum = cnt;
memcpy(big,ans,sizeof(ans));
flag = true;
}
}
if( flag )
for( int i = 1 ; i <= row; i++ )
{
for( int j = 1 ; j <= col ; j++ )
{
if( j != 1 )
printf(" ");
printf("%d",big[i][j]);
}
printf("\n");
}
else printf("IMPOSSIBLE\n");
}
return 0;
}
/*
1 10
1 0 0 1 1 1 1 1 1 1
1 10
1 1 0 1 0 1 1 1 1 1
2 5
1 0 1 0 1
1 1 1 1 1
2 5
1 0 1 0 1
1 0 1 1 1
2 5
1 0 1 0 1
1 1 0 1 1
2 5
1 0 1 0 1
0 1 1 1 1
3 4
1 1 1 1
0 0 1 1
1 1 1 1
3 4
1 1 1 1
0 0 1 1
0 1 1 1
*/