POJ3279 Fliptile —— 状态压缩 + 模拟
2017-09-06 11:01
323 查看
题目链接:http://poj.org/problem?id=3279
Fliptile
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.
Sample Input
Sample Output
Source
USACO 2007 Open Silver
题解:
1.枚举第一行的所有状态。
2.从第二行开始,如果当前瓦片的头上瓦片为1,那么需要翻转当前瓦片。因为在这一行之内,只有当前瓦片能够使头上的瓦片变为0,然后一直递推到最后一行。
3.分析:一开始想到方法是枚举,但是数据太大,计算机要算多少年真的不好估计。先假设枚举法是可行的, 相对于枚举法, 上述方法是有明显的优势的:上述方法从第二行开始,每翻转一块瓦片都是有针对性的、有目的性的;而枚举法则是盲目地翻转,如:假如当前瓦片为0,却还是硬要翻转该瓦片,那显然是行不通的。所以上述方法相对来说更“智能”。
4.注意:s&(1<<i)的值要么等于0,要么等于(1<<i);而不是0或1。应注意!!
5.此题的弱化版,可使用枚举法。POJ1753:http://blog.csdn.net/dolfamingo/article/details/54944929
代码如下:
Fliptile
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11771 | Accepted: 4360 |
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
USACO 2007 Open Silver
题解:
1.枚举第一行的所有状态。
2.从第二行开始,如果当前瓦片的头上瓦片为1,那么需要翻转当前瓦片。因为在这一行之内,只有当前瓦片能够使头上的瓦片变为0,然后一直递推到最后一行。
3.分析:一开始想到方法是枚举,但是数据太大,计算机要算多少年真的不好估计。先假设枚举法是可行的, 相对于枚举法, 上述方法是有明显的优势的:上述方法从第二行开始,每翻转一块瓦片都是有针对性的、有目的性的;而枚举法则是盲目地翻转,如:假如当前瓦片为0,却还是硬要翻转该瓦片,那显然是行不通的。所以上述方法相对来说更“智能”。
4.注意:s&(1<<i)的值要么等于0,要么等于(1<<i);而不是0或1。应注意!!
5.此题的弱化版,可使用枚举法。POJ1753:http://blog.csdn.net/dolfamingo/article/details/54944929
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <set> #define ms(a,b) memset((a),(b),sizeof((a))) using namespace std; typedef long long LL; const int INF = 2e9; const LL LNF = 9e18; const int MOD = 1e9+7; const int MAXN = 15+10; int M[MAXN][MAXN], subM[MAXN][MAXN]; int op[MAXN][MAXN], ans_op[MAXN][MAXN]; int n, m; void press(int x, int y) { op[x][y] = 1; subM[x][y] = !subM[x][y]; if(x-1>=0) subM[x-1][y] = !subM[x-1][y]; if(x+1<n) subM[x+1][y] = !subM[x+1][y]; if(y-1>=0) subM[x][y-1] = !subM[x][y-1]; if(y+1<m) subM[x][y+1] = !subM[x][y+1]; } bool allZero() { for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) if(subM[i][j]) return false; return true; } void solve() { int ans_step = INF; for(int s = 0; s<(1<<m); s++) { ms(op, 0); memcpy(subM, M, sizeof(subM)); int step = 0; for(int i = 0; i<m; i++) if(s&(1<<i)) press(0, i), step++; for(int i = 1; i<n; i++) for(int j = 0; j<m; j++) if(subM[i-1][j]) press(i, j), step++; if(allZero() && ans_step>step ) { ans_step = step; memcpy(ans_op, op, sizeof(ans_op)); } } if(ans_step==INF) puts("IMPOSSIBLE"); else { for(int i = 0; i<n; i++){ for(int j = 0; j<m; j++) printf("%d ",ans_op[i][j]); putchar('\n'); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) scanf("%d",&M[i][j]); solve(); } return 0; }
相关文章推荐
- poj3279 Fliptile【二进制枚举状态】
- 状态压缩(2)+模拟枚举(2)--poj3279(能力题)
- poj3279 Fliptile dfs暴力枚举状态
- Fliptile POJ - 3279 (状态压缩+枚举) && Flip Game POJ - 1753(状态压缩+bfs暴力)
- [Usaco2007 Open]Fliptile 翻格子游戏 状态压缩
- POJ 3279 Fliptile(状态压缩+暴力)
- csu 1536 Bit String Reordering(模拟 bfs+状态压缩)
- 【BZOJ 1647】[Usaco2007 Open]Fliptile 翻格子游戏 模拟、搜索
- codeforces 425B Sereja and Table(状态压缩,也可以数组模拟)
- poj3279 Fliptile 开关问题
- poj 2414 Phylogenetic Trees Inherited 完全二叉树 状态压缩位运算模拟集合操作 动态规划
- poj3279 子集枚举+状态压缩暴力
- 【POJ3279】Fliptile(开关问题)
- POJ3279 Fliptile题解 二进制枚举搜索法
- God of War - HDU 2809(状态压缩+模拟)
- POJ 1753 Flip Game BFS+位压缩状态
- 【状态压缩枚举】poj 1753 Flip Game
- ZOJ-3697 Bad-written Number 恶心模拟-状态压缩-动态规划
- poj3279 Fliptile(反转(开关问题))
- JOJ 2474:Tile My Corridor 平铺棋盘 状态压缩解法