Test instructions
N*m block area, each with positive and negative sides, the two sides are white and black.
When you flip an area, the four areas adjacent to it are reversed at the same time.
Ask for a minimum number of flips to turn all areas into white output its flip scheme
Ideas:
Enumerates the flip state of the first row
If you already know the state of the first row, then the status of the next few lines is determined-(if the upper area of the area is black, the area must be reversed to make the area above white)
Then compare and record the minimum number of rollover outputs to
Code
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 20;const int INF = 0x3f3f3f3f;int MAZE[MAXN][MAXN], Tmaze[maxn][maxn];int ANS[MAXN][MAXN], Min_ans[maxn][maxn];int n,m;void deal (int x, int y) {Tmaze[x][y] ^= 1; Tmaze[x+1][y] ^= 1; Tmaze[x-1][y] ^= 1; TMAZE[X][Y+1] ^= 1; Tmaze[x][y-1] ^= 1;} void Deal1 (int s) {int cnt = m; for (int i = 0; i < m; i++) {ans[1][cnt--] = (s>>i) &1; }}bool Deal2 () {for (int i = 1; I <= m; i++) {if (ans[1][i] = = 1) {deal (1,i); }} for (int i = 2, I <= N; i++) {for (int j = 1; j <= M; j + +) {if (tmaze[i-1][j] = = 1) { ANS[I][J] = 1; Deal (I,J); }}} BOOL flag = TRUE; for (int i = 1; I <= m; i++) {if (tmaze[n][i]! = 0) {flag = false; }} return flag;} void Init () {for (int i = 1; I <= n; i++) {for (int j = 1; J < = m; J + +) {scanf ("%d", &maze[i][j]); }}}void Solve () {bool flag; BOOL Flag2 = false; int min_ = INF; memset (Min_ans, 0, sizeof (Min_ans)); for (int s = 0; s < (1<<m); s++) {memset (ans, 0, sizeof (ans)); memcpy (Tmaze, Maze, sizeof (maze)); Deal1 (s);//Gets the first line state to put in ans[1][i] flag = Deal2 ();//Flip if (flag) {////) on the first line and output Flag2 = true; int tmp = 0; for (int i = 1, i <= N; i++) {for (int j = 1; j <= M; j + +) {if (ans[i][j] = = 1) tmp++ ; }} if (TMP < Min_) {memcpy (Min_ans, ans, sizeof (ans)); Min_ = tmp; }}} if (!flag2) printf ("impossible\n"); else{for (int i = 1; I <= N, i++) {for (int j = 1; j <= M; j + +) {if (J! = 1) printf (" "); printf ("%d", min_ans[i][j]); } printf ("\ n"); }}}int Main () {scanf ("%d%d", &n,&m); Init (); Solve (); return 0;}
Although the problem has been done at least 3, but still do not remember how to do .... It was a ruthless t_t game. Be battered into a horse
POJ 3279 Filptile (usaco. Open Silver)