1. 程式人生 > >[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子遊戲

[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子遊戲

pla 而且 solution raw tails aps amount 字符 discuss

1647: [Usaco2007 Open]Fliptile 翻格子遊戲

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 702 Solved: 281
[Submit][Status][Discuss]

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

約翰知道,那些高智力又快樂的奶牛產奶量特別高.所以他做了一個翻瓦片的益智遊戲來娛樂奶牛.在一個M×N(1≤M,N≤15)的骨架上,每一個格子裏都有一個可以翻轉的瓦片.瓦片的一面是黑色的,而另一面是白色的.對一個瓦片進行翻轉,可以使黑變白,也可以使白變黑.然而,奶牛們的蹄子是如此的巨大而且笨拙,所以她們翻轉一個瓦片的時候,與之有公共邊的相鄰瓦片也都被翻轉了.那麽,這些奶牛們最少需要多少次翻轉,使所有的瓦片都變成白面向上呢?如杲可以做到,輸出字典序最小的結果(將結果當成字符串處理).如果不能做到,輸出“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

第1行輸入M和N,之後M行N列,輸入遊戲開始時的瓦片狀態.0表示白面向上,1表示黑面向上.

Output

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

輸出M行,每行N個用空格隔開的整數,表示對應的格子進行了多少次翻轉.

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

OUTPUT DETAILS:

After flipping at row 2 column 1, the board will look like:
0 0 0 1
1 0 1 0
1 1 1 0
1 0 0 1

After flipping at row 2 column 4, the board will look like:
0 0 0 0
1 0 0 1
1 1 1 1
1 0 0 1

After flipping at row 3 column 1, the board will look like:
0 0 0 0
0 0 0 1
0 0 1 1
0 0 0 1

After flipping at row 3 column 4, the board will look like:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Another solution might be:
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
but this solution is lexicographically higher than the solution above.

第一眼看到數據範圍可能很多人會想到狀壓 $DP$ , 然而看起來似乎是最大 $15 \times 15 = 225$ 的狀壓量根本無法枚舉.

但是實際上我們可以推知, 根據上一行的黑白情況我們可以推知下一行的翻轉情況. 因為在上一行已經確定的情況下下一行必須保證在黑色瓦片正下方進行翻轉. 所以我們的所有情況都可以從第一行的黑白情況推知. 這時枚舉量就從 $2^{k^2}$ 降到了 $2^k$ . 然後我們根據第一行枚舉得出的黑白情況計算該種情況是否可以得出解. 如果得出解的話更新答案, 無解忽略即可.

枚舉中途可以選擇進行剪枝, 保存最低翻轉數, 當已使用的翻轉數大於這個值時停止繼續求值.

參考代碼

GitHub

技術分享
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <iostream>
  5 #include <algorithm>
  6 
  7 const int MAXN=16;
  8 
  9 int n;
 10 int m;
 11 int sum;
 12 int flip;
 13 int minfl;
 14 int black;
 15 bool ans[MAXN][MAXN];
 16 bool raw[MAXN][MAXN];
 17 bool tmp[MAXN][MAXN];
 18 bool data[MAXN][MAXN];
 19 
 20 void Change(int,int);
 21 void Initialize();
 22 bool Check(int);
 23 bool Compare();
 24 
 25 int main(){
 26     freopen("fliptile.in","r",stdin);
 27     freopen("fliptile.out","w",stdout);
 28     bool solve=false;
 29     Initialize();
 30     for(int i=0;i<(1<<n);i++){
 31         memset(tmp,0,sizeof(tmp));
 32         memcpy(data,raw,sizeof(raw));
 33         flip=0;
 34         black=sum;
 35         if(Check(i)){
 36             memcpy(ans,tmp,sizeof(tmp));
 37             minfl=flip;
 38             solve=true;
 39         }
 40     }
 41     if(solve){
 42         for(int i=1;i<=n;i++){
 43             for(int j=1;j<=m;j++){
 44                 printf("%d ",ans[i][j]?1:0);
 45             }
 46             putchar(\n);
 47         }
 48     }
 49     else
 50         puts("IMPOSSIBLE");
 51     return 0;
 52 }
 53 
 54 inline void Change(int x,int y){
 55     tmp[x][y]=true;
 56     data[x][y]=!data[x][y];
 57     black+=data[x][y]?1:-1;
 58     if(x>1){
 59         data[x-1][y]=!data[x-1][y];
 60         black+=data[x-1][y]?1:-1;
 61     }
 62     if(x<n){
 63         data[x+1][y]=!data[x+1][y];
 64         black+=data[x+1][y]?1:-1;
 65     }
 66     if(y>1){
 67         data[x][y-1]=!data[x][y-1];
 68         black+=data[x][y-1]?1:-1;
 69     }
 70     if(y<m){
 71         data[x][y+1]=!data[x][y+1];
 72         black+=data[x][y+1]?1:-1;
 73     }
 74 }
 75 
 76 void Initialize(){
 77     scanf("%d%d",&n,&m);
 78     int tmp=0;
 79     minfl=0x7FFFFFFF;
 80     for(int i=1;i<=n;i++){
 81         for(int j=1;j<=m;j++){
 82             scanf("%d",&tmp);
 83             raw[i][j]=tmp;
 84             sum+=tmp;
 85         }
 86     }
 87 }
 88 
 89 bool Check(int x){
 90     for(int i=0;i<n;i++){
 91         if(x&(1<<i)){
 92             Change(1,i+1);
 93             flip++;
 94             if(flip>=minfl)
 95                 return false;
 96         }
 97     }
 98     for(int i=2;i<=n;i++){
 99         for(int j=1;j<=m;j++){
100             if(data[i-1][j]){
101                 Change(i,j);
102                 flip++;
103                 if(flip>=minfl)
104                     return false;
105             }
106         }
107     }
108     if(black>0)
109         return false;
110     else
111         return true;
112 }
Backup

以及日常凸包圖包

技術分享

[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子遊戲