POJ3276——暴力——Fliptile
http://poj.org/problem?id=3279
/* 枚举第一行所有的状态,接下来的行都已经确定了,字典序最小输出 */ /************************************************ * Author :Powatr * Created Time :2015-8-9 13:18:38 * File Name :D.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int n, m; char s[400]; char ss[400]; int maze[20][20]; int mp[20][20]; int path[20][20]; int f_path[20][20]; int solve(int cnt) { int ans = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) mp[i][j] = maze[i][j]; for(int i = 1; i <= m; i++){ if(cnt&(1<<(i-1))){ ans++; path[1][i] ^= 1; mp[1][i] ^= 1; mp[1][i-1] ^= 1; mp[1][i+1] ^= 1; mp[2][i] ^= 1; } } for(int i = 2; i <= n;i++){ for(int j = 1; j <= m; j++){ if(mp[i-1][j] == 1){ ans++; path[i][j] ^= 1; mp[i-1][j] ^= 1; mp[i][j] ^= 1; mp[i][j-1] ^= 1; mp[i][j+1] ^= 1; mp[i+1][j] ^= 1; } } } for(int j = 1; j <= m; j++) if(mp[n][j]) return -1; return ans; } int main(){ while(~scanf("%d%d", &n, &m)){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++) scanf("%d", &maze[i][j]); } memset(f_path, 0 , sizeof(f_path)); for(int i = 1; i <= n*m ; i++) ss[i] = '1'; int min1 = n*m + 1; for(int i = 0; i <= (1<<n) - 1; i++){ int res = 0; memset(path, 0, sizeof(path)); memset(mp, 0, sizeof(mp)); int temp = solve(i); if(temp == -1) continue; if(temp <= min1) { min1 = temp; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ s[++res] = (char)(path[i][j] + '0'); } } if(strcmp(s+1, ss+1) < 0){ strcpy(ss+1, s+1); } } } if(min1 == n*m + 1) printf("IMPOSSIBLE\n"); else { for(int i = 1; i <= n*m; i++){ printf("%c%c", ss[i], i % m == 0 ? '\n' : ' '); } } } return 0; }