Fliptile POJ 3279(开关问题)

来源:互联网 发布:104cm肛塞淘宝 编辑:程序博客网 时间:2024/04/23 23:15

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 41 0 0 10 1 1 00 1 1 01 0 0 1

Sample Output

0 0 0 01 0 0 11 0 0 10 0 0 0

Source

USACO 2007 Open Silver

题目大意:

给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另一个颜色,要求翻转最少的点,使得变为全白色的矩阵,输出这个标记了翻转点的矩阵,如果有多个最优解,输出逆字典序最小的那个矩阵,若没有解,输出IMPOSSIBLE。

解题思路:

由于一个点翻转两次则返回原来的状态,所以最优解每个点最多翻转一次,但是2^(M*N)过大,所以2^N枚举第一行的所有翻转方式(逆字典序枚举),确定一种方式之后第二行也就随之确定了,因为如果第一行处理后没有翻回白色的点:(i,j),必须在第二行(i+1,j)翻回,否则将无法返回。反之第二行其他的点都处理为不翻转,要不然上一行的点会翻回黑色而无法改变。第二行ok后同理解决第三行,以此类推。处理到最后一行如果不是全白就输出IMPOSSIBLE。否则更新结果。

P.S.

1.逆字典序输出!!!就是第一行要倒着枚举!!坑啊!

2.不一定是15*15的,可能更大。


#include<stdio.h>#include<string.h>int t[30][30], tem[30][30], m[30][30];int M,N,dir[5][2] = { 0,0,1,0,0,1,-1,0,0,-1 };int get(int x, int y)//获得x,y点的颜色{int c = t[x][y];for (int i = 0; i < 5; i++){int x1 = x + dir[i][0], y1 = y + dir[i][1];c += tem[x1][y1];}return c % 2;}int cal()//计算2行及之后的,有解返回翻点数,无解返回-1{for (int i = 2; i <= M; i++)for (int j = 1; j <= N; j++)if (get(i - 1, j) == 1)tem[i][j] = 1;for (int i = 1; i <= N; i++)if (get(M, i))return -1;int res = 0;for (int i = 1; i <= M; i++)for (int j = 1; j <= N; j++)res += tem[i][j];return res;}int main(){int min = -1;//次数>0可以这样初始化scanf("%d%d", &M, &N);for (int i = 1; i <= M; i++)for (int j = 1; j <= N; j++)scanf("%d", &t[i][j]);for (int i = 0; i < (1 << N); i++)//枚举第一行{memset(tem, 0, sizeof(tem));for (int j = 1; j <= N; j++)tem[1][j] = (i >> (j - 1)) & 1;int num = cal();if (num >= 0 && (min<0 || min>num)){min = num;memcpy(m, tem, sizeof(tem));}}if (min == -1)printf("IMPOSSIBLE\n");else{for (int i = 1; i <= M; i++)for (int j = 1; j <= N; j++)printf("%d%c", m[i][j], j == N ? '\n' : ' ');}}



0 0