poj3279(反转问题)

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代表黑,0代表白;
题解:(1)首先明确一点,每块瓷砖最多踩一次即可,已经如果再踩一次,就跟没踩一样了。
(2)如果第一行已经确定怎么哪个要踩,哪个不要踩的话,其他行也会随之确定。(因为第一行踩完以后,第二行能踩的瓷砖只能是同列的第一行的瓷块是黑色的,如果踩其他列的,第一行就会有白的变成黑的了,但我们已经假设第一行确定了的,不能再回第一行踩了)。那么对于接下来的行对哪列是要踩的就要看上一行同列是黑色还是白色了,黑色就踩,白色就不睬。
(3)踩完最后一列,如果最后一列是全白的就说明该踩法可以使所有瓷块都变成白的了,因为按(2)所说,每踩一行要把上一行全变成白的,所以最后一行是白的就说明全是白的了。
(4)那么第一行的踩法有多少种呢,对于每一个瓷砖有踩和不睬两种方案,所以就有2的n次方中了。
以下是完整代码
ac代码

#include<iostream>       
#include<cstdlib>      
#include<cstdio> 
#include<cstring>      
#include<cmath>           
#include<string>      
#include<cstdlib>      
#include<iomanip>      
#include<vector>      
#include<list>      
#include<map>      
#include<queue>    
#include<algorithm>
using namespace std;
const int N = 100000;
int you[16][16];
int zhong[16][16];
int yuan[16][16];
int n,m,ant=N,sum;
int s1[4] = { 0,0,1,-1 }, s2[4] = { 1,-1,0,0 };
int kkk(int x, int y)//该函数用来判断该(x,y)处的瓷块是不是黑色的,那这块瓷块会受到谁的影响呢?本身和它的四周,如果这五个地方有奇数个被踩了,那么它就会变成相反面,偶数个的话,就不会变。
{
	int dx, dy;
	int g = zhong[x][y] + yuan[x][y];
	for (int i = 0; i < 4; i++)
	{
		dx = x + s1[i];
		dy = y + s2[i];
		if (dx >=0 && dy >=0 && dx < n&&dy < m)
			g += zhong[dx][dy];
	}
	return g % 2;
}
int asd(int ask)
{
	for (int j = 1; j < n; j++)//接下来踩第2--n-1行的
	{
		for (int k = 0; k < m; k++)
		{
			if (kkk(j - 1, k))//判断上一行同列的瓷块是不是黑的,是的话就踩
			{
				zhong[j][k] = 1; ask++;
			}
		}
	}
	for (int i = 0; i < m; i++)//判断最后一行是不是全白的,如果有一个是黑的就返回N。
		if (kkk(n - 1, i))return N;
	return ask;
}
void yi(int x)
{
	for (int i = 0; i < n; i++)
	{
		if ((x >> i) & 1)	//把一行踩了,记得第一行踩的次数也要加上去
		{
			zhong[0][i] = 1;
			sum++;
		}
	}
	sum=asd (sum);
	
}
int main()
{
	while (cin >> n >> m)
	{
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				cin >> yuan[i][j];
		for (int i = 0; i < (1 << m); i++)
		{
			memset(zhong, 0, sizeof(zhong));
			sum = 0;
			yi(i);
		
			if (ant > sum)//选更少次数的复制给要输出的数组,那么最终要输出的数组就是踩最少次数的
			{
				ant = sum;
				memcpy(you, zhong, sizeof(zhong));
			}
		}
		if (ant == N)cout << "IMPOSSIBLE" << endl;
		else {
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < m; j++)
				{
					cout << you[i][j];
						if (j < m - 1)cout << " ";
						else cout << endl;
				}
			}
		}
	}
	
}

猜你喜欢

转载自blog.csdn.net/weixin_43965698/article/details/87880961