Fliptile
来源:互联网 发布:大和号战列舰模型淘宝 编辑:程序博客网 时间:2024/06/04 18:14
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".
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
4 41 0 0 10 1 1 00 1 1 01 0 0 1
0 0 0 01 0 0 11 0 0 10 0 0 0
枚举第一行的翻转所有翻转情况然后逐行向下更新,如果上一行是1的话,那么下面一行肯定要翻转,因为只有下面一行能影响上面一行。最后判断一下,最后一行是不是都是0,如果都是,则维护最小的翻转次数。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int maxn = 25;const int INF = 0x3f3f3f3f;int n,m,res;int Map[maxn][maxn],vist[maxn][maxn];int temp[maxn][maxn],ans[maxn][maxn],tmp[maxn][maxn];int dx[5] = {0,0,0,1,-1};int dy[5] = {0,1,-1,0,0};void filp(int x,int y){ int nx,ny; tmp[x][y] = 1; for(int i = 0; i < 5; i++){ nx = x+dx[i]; ny = y+dy[i]; temp[nx][ny] = !temp[nx][ny]; }}bool judge(){ for(int i = 1; i <= m; i++) if(temp[n][i]) return false; return true;}void solve(int x){ memcpy(temp,Map,sizeof(Map)); memset(tmp,0,sizeof(tmp)); int cnt = 0; for(int i = 0; i < m; i++){ if((x>>i) & 1){ filp(1,i+1); cnt++; } } for(int i = 2; i <= n; i++){ for(int j = 1; j <= m; j++){ if(temp[i-1][j] == 1){ filp(i,j); cnt++; } } } if(judge() && cnt < res){ res = cnt; memcpy(ans,tmp,sizeof(tmp)); }}int main(){ scanf("%d %d",&n,&m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d",&Map[i][j]); res = INF; int End = 1<<m; for(int i = 0; i < End; i++) solve(i); if(res == INF) puts("IMPOSSIBLE"); else{ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(j == m) printf("%d\n",ans[i][j]); else printf("%d ",ans[i][j]); } } } return 0;}
- Fliptile
- Fliptile
- Fliptile
- Fliptile
- Fliptile
- Fliptile
- POJ3279 Fliptile
- POJ3279 Fliptile
- POJ3279-Fliptile
- poj3279 Fliptile
- Fliptile POJ
- poj3279 Fliptile
- Fliptile POJ
- Fliptile POJ
- POJ3279-Fliptile
- POJ3279-Fliptile
- POJ3279 Fliptile
- poj3279 Fliptile
- Linux中exec命令相关
- 无网络情况ubuntu虚拟机与win7以samba方式共享文件(hostonly仅主机模式)
- Associating Neural Word Embeddings With Deep Image Representations Using Fisher Vector源码解析
- jQuery选择器之基本筛选选择器
- 剑指offer刷题总结
- Fliptile
- 高斯基函数线性组合回归练习——sklearn库高斯过程回归
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED解决方法
- EM算法的应用--高斯混合模型学习
- Java基础-网络编程之IP和端口号
- Windows脚本对最后修改时间超过24小时的文件进行处理
- Java学习笔记:接口和抽象类 之间的探讨
- jQuery鼠标事件
- 关于字节序Big Endian和Little Endian