POJ 3279 Fliptile(翻格子)

POJ 3279 Fliptile(翻格子)

Time Limit: 2000MS    Memory Limit: 65536K

 

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".

农夫John知晓了一只智商不堪忧的快乐奶牛产出更多。于是他用广场砖弄了个M × N (1 ≤ M ≤ 15; 1 ≤ N ≤ 15)的网格,一面黑一面白,用来玩智力游戏。

如你所想,白的翻过来是黑的,黑的翻过来是白的。如果奶牛翻转瓷砖,使所有白面朝上,则会得到奖励。然而牛蹄子大,翻一块砖时会联动翻转相邻的砖(与被翻转的砖共享边缘)。翻面很累牛,因此他们想尽可能少翻点。

帮助奶牛们确定最少的翻转次数,还有最少需要翻动的位置。如果存在多解,则选取输出字符串字典序最小的解法。如果无解,则输出一行 "IMPOSSIBLE"
CN

 

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

第1行:两个以空格分隔的整数:M和N
第2..M+1行:第i+1行是从左到右地描述第i行网格的颜色,有N个由空格分隔的整数,1表示黑,0表示白。
CN

 

Output - 输出

Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

第1..M行:每行有N个由空格分隔的整数,每个数表示当前位置需要翻转的次数。
CN

 

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

 

题解

  题目的最少翻转次数不明觉厉,字典序的描述也是感觉有点问题,曾让我误以为策略是需要从左往右一列一列地搜索。主要还是道考(cai)策略的题目…………
  看着样例感觉翻转没有顺序的区别,那就从下往上地看(一开始看列看发现输出不对),一行一行地看,从左往右,从上往下。
  因为感觉没有顺序差别,所以翻转最多一次,翻两次回到原点。
  下面的行可以为上面的行做最终决定,依次往下,就看最后一行是否为0即可判断是否成立。
  唯一没法决定的在第一行,因此需要枚举第一行的开场情况,枚举第一行翻的位置与翻的次数(二进制)。

 

一开始也是对这个策略将信将疑,然后强迫症发作找到这个游戏,玩了几把后就差不多有自信写了…… 

(Flash好像加载不出来,这里直接放链接好了)http://s1.4399.com:8080/4399swf/upload_swf/ftp/20080527/1.swf

 

代码 C++

 1 #include <cstdio>
 2 #include <cstring>
 3 int row[15], rowBack[15], ts[15], bit[15], m, n;
 4 void reRow(int y, int x){
 5     if (y < 0 || x < 0 || y >= m || x >= n) return;
 6     row[y] ^= bit[x];
 7 }
 8 void opt(){
 9     int i, j;
10     for (i = 0; i < m; ++i){
11         printf("%d", ts[i] & bit[0]);
12         for (j = 1; j < n; ++j) printf(" %d", (ts[i] & bit[j]) >> j);
13         puts("");
14     }
15 }
16 int main(){
17     int i, j, k, ed;
18     for (i = 0, j = 1; i < 15; ++i, j <<= 1) bit[i] = j;
19     scanf("%d%d", &m, &n);
20     for (i = 0; i < m; ++i){
21         for (j = 0; j < n; ++j){
22             scanf("%d", &k);
23             if (k) rowBack[i] ^= bit[j];
24         }
25     }
26 
27     for (i = 0, ed = 1 << n; i < ed; ++i){
28         memcpy(row, rowBack, sizeof rowBack);
29         memset(ts, 0, sizeof ts); ts[0] = i;
30         for (j = 0; j < n; ++j){
31             if (i & bit[j]){
32                 reRow(0, j); reRow(0, j - 1); reRow(0, j + 1); reRow(1, j);
33             }
34         }
35         for (j = 1; j < m; ++j){
36             for (k = 0; k < n; ++k){
37                 if (row[j - 1] & bit[k]){
38                     reRow(j - 1, k); reRow(j + 1, k); reRow(j, k); reRow(j, k - 1); reRow(j, k + 1);
39                     ts[j] ^= bit[k];
40                 }
41             }
42         }
43         if (!row[m - 1]) break;
44     }
45     if (row[m - 1]) puts("IMPOSSIBLE");
46     else opt();
47     return 0;
48 }

 

posted @ 2017-01-17 21:22  Simon_X  阅读(319)  评论(0编辑  收藏  举报