Fliptile
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 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
Source
题目类型:搜索
算法分析:枚举第一行反转的所用情况,然后按照第一行的情况更新之后行,最后第M行若出现’1’,则表示这种情况不成立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0, 0}; const int dy[] = {0, 0, -1, 1, 0}; int g[166][166], res[166][166], temp[166][166], tempg[166][166], row, col, ans; string ress; void rev (int x, int y) { for (int i = 0; i < 5; i++) { int tx = x + dx[i], ty = y + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1) tempg[tx][ty] = !tempg[tx][ty]; } } int SS () { for (int i = 1; i < row; i++) { for (int j = 0; j < col; j++) { if (tempg[i-1][j]) { rev (i, j); ans++; temp[i][j] = 1; } } } for (int i = 0; i < col; i++) if (tempg[row-1][i]) return -1; return ans; } string TT () { string res; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) res += (char) (temp[i][j] + '0'); return res; } int dfs () { int minval = INF; for (int i = 0; i < (1 << col); i++) { memset (temp, 0, sizeof (temp)); for (int j = 0; j < col; j++) temp[0][col-1-j] = (i >> j) & 1; for (int j = 0; j < row; j++) for (int k = 0; k < col; k++) tempg[j][k] = g[j][k]; ans = 0; for (int j = 0; j < col; j++) if (temp[0][j]) { rev (0, j); ans++; } int tt = SS (); if (tt != -1) { string ss = TT (); if (minval > tt || (minval == tt && ss < ress)) { minval = tt, ress = ss; memcpy (res, temp, sizeof (temp)); } } } if (minval < INF) return minval; else return -1; } int main() { // CFF; while (scanf ("%d %d", &row, &col) != EOF) { for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) scanf ("%d", &g[i][j]); int tt = dfs (); if (tt == -1) puts ("IMPOSSIBLE"); else { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (j == 0) printf ("%d", res[i][j]); else printf (" %d", res[i][j]); } puts (""); } } } return 0; } |
- « 上一篇:hdu1495