Fliptile(枚舉+DFS)

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

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

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

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 }
相關文章
相關標籤/搜索