天天看點

Fliptile(枚舉+DFS)Problem DescriptionInputOutputSampleInputSampleOutput

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

SampleInput

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1      

SampleOutput

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

這題也是kuangbin搜尋專題裡面的一題,題意就是一個n*m的矩陣,0代表燈關,1代表燈開,按其中一個按鈕,自身以及上下左右都會變成相反狀态,就是和我們玩的關燈遊戲一樣,問你最少按幾次就能全部關掉,多種情況的話輸出字典序最小的。
這題目可以用遞推的思路,從最上面開始操作,下一行的操作都會由上一行得出,最後我們判斷最後一行是否都為0就行了。

而對于第一行來說,最壞一共有2^m種情況,我們隻需要枚舉每一種情況即可,然後若是有多組,輸出字典序小值就好。
其他的話就沒什麼解釋的啦,代碼裡面我加了注釋,直接看代碼吧=7=
代碼:      
1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 int mp[20][20],tp[20][20],s[20][20];
 45 int n,m;
 46 int fx[5][2] = {{0,0},{0,1},{0,-1},{1,0},{-1,0}};
 47 
 48 int fun(int x,int y){    //判斷x、y旁邊的5個位置的顔色得出x、y位置的顔色
 49     int temp = mp[x][y];
 50     for(int i = 0; i < 5; i++){
 51         int next_x = x+fx[i][0];
 52         int next_y = y+fx[i][1];
 53 
 54         if(next_x < 1 || next_x > n || next_y < 1 || next_y > m)
 55             continue;
 56         temp += tp[next_x][next_y];
 57     }
 58     return temp%2;
 59 }
 60 
 61 int dfs(){
 62     for(int i = 2; i <= n; i++)
 63         for(int j = 1; j <= m; j++)
 64             if(fun(i-1,j))    //上一行位置燈開狀态,此位置就必須開燈使上一行熄滅
 65                 tp[i][j] = 1;
 66 
 67     for(int i = 1; i <= m; i++)  //最後一行全部為0,直接結束
 68         if(fun(n,i))
 69             return -1;
 70 
 71     int res = 0;
 72     for(int i = 1; i <= n; i++)
 73         for(int j = 1; j <= m; j++)  //得出大小,因為後面會比較字典序
 74             res += tp[i][j];
 75     return res;
 76 }
 77 
 78 int main(){
 79     ios_base::sync_with_stdio(false);
 80     cin.tie(0);
 81     cout.tie(0);
 82     while(cin>>n>>m){
 83         for(int i = 1; i <= n; i++)
 84             for(int j = 1; j <= m; j++)
 85                 cin>>mp[i][j];
 86 
 87         int flag = 0;
 88         int ans = INF;
 89         for(int i = 0; i < 1<<m; i++){    //枚舉第一行的所有狀态
 90             memset(tp,0,sizeof(tp));
 91 
 92             for(int j = 1; j <= m; j++)
 93                 tp[1][m-j+1] = i>>(j-1) & 1;
 94             int cnt = dfs();
 95             if(cnt >= 0 && cnt < ans){  //得出字典序最小的
 96                 flag = 1;
 97                 ans = cnt;
 98                 memcpy(s,tp,sizeof(tp));
 99             }
100         }
101         if(!flag)
102             cout<<"IMPOSSIBLE"<<endl;
103         else{
104             for(int i = 1;i <= n;i ++){
105                 for(int j = 1;j <= m;j ++){
106                     if(j != 1)
107                         cout<<" ";
108                     cout<<s[i][j];
109                 }
110                 cout<<endl;
111             }
112         }
113     }
114     return 0;
115 }      

繼續閱讀