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".
【In】
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
【Out】
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
【SampIn】
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
【SampOut】
0 0 0 0
1 0 0 1
1 0 0 1
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".
【In】
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
【Out】
Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
【SampIn】
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
【SampOut】
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
题目大义是在已知矩阵中进行翻转,每次翻转将翻转本身及上下左右的元素(0变为1,1变为0),以期待达到矩阵全部为0的效果。求翻转次数最少的方法。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int g[30][30],f[30][30],t[30][30];//map为原本的瓷砖分布,t是改变过程中map的值,f则是最后反转的记录值
int M,N,cnt;
int mov[5][2]={0,0,0,1,0,-1,1,0,-1,0};
int x[4] = {0, 0, -1, 1};
int y[4] = { -1, 1, 0, 0};
void flip(int x,int y)//对应翻转自己和相邻的位置
{
cnt++;
f[x][y]=1;//为了控制最后的结果项,对已经确认反转的进行反转
for(int i=0;i<5;i++)
{
if(x+mov[i][0]>-1&&y+mov[i][1]>-1)
{
t[x+mov[i][0]][y+mov[i][1]]^=1;
}
}
}
bool Judge(int k)//对于第一行的每一种情况,判断是否产生最终结果,但注意此时的k是指反转的位置而非之后的具体情况
{
cnt=0;
memcpy(t,g,sizeof(t));//t是原始map的副本
for(int j=0;j<N;j++)
{
if(k&(1<<(N-1-j)))//逆字典序,从后往前反转
flip(0,j);
}
for(int i=1;i<M;i++)
for(int j=0;j<N;j++)
if(t[i-1][j]) flip(i,j);
for(int j=0;j<N;j++)
if(t[N-1][j]) return false;
return true;
}
int main()
{
scanf("%d%d",&M,&N);
int ans=M*N+1,p=-1;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
scanf("%d",&g[i][j]);
for(int i=0;i<(1<<N);i++)
if(Judge(i)&&cnt<ans)
ans=cnt,p=i;
memset(f,0,sizeof(f));
if(p>=0)
{
Judge(p);
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
printf("%d%c",f[i][j], j < N - 1 ? ' ' : '\n');
}
}
else
printf("IMPOSSIBLE");
return 0;
}
/*
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
*/