poj 3279 Fliptile
来源:互联网 发布:语音识别算法 编辑:程序博客网 时间:2024/06/05 14:49
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
题意,翻转格子,翻转之后相邻的4个格子也会一起翻转。问你一种最小的翻转次数的翻转情况。如果答案不唯1输出字典序最小的。
照这个数据来看答案是唯1的。
我们不难想的是暴力,但是不可能去暴力所有的格子翻转或者不翻转。实际上我们只需要枚举第一行的翻转状态就可以。
然后后面一行的状态就会被上一行的状态唯一确定了。想一想是不是。最后检测一下最后一行的状态是不是全为0就可以了。
如果是,说明是答案的一种,更新答案即可。
字典序最小的话,我们枚举的时候从000000...到111111....枚举就可以了。
#include <cstdio>#include <queue>#include <cstring>using namespace std;const int MAXN=10+7;const int inf=1e9;int tu[MAXN][MAXN];int state[MAXN][MAXN];//中间翻转状态int ans[MAXN][MAXN];//最终结果int temp[MAXN][MAXN];//每一次枚举中间结果int dx[5] = {0,0,0,-1,1};int dy[5] = {0,1,-1,0,0};int n,m;int Ans;bool isOk(){ for(int j = 0 ;j < m; ++j)if(state[n-1][j])return 0; return 1;}void updata(int i,int j){ temp[i][j] = 1; for(int k = 0 ;k < 5; ++k) { int nx = i + dx[k]; int ny = j + dy[k]; if(nx >= 0 && nx < n && ny >= 0 && ny < m )state[nx][ny] = !state[nx][ny]; }}void check(int key){ memcpy(state,tu,sizeof(tu)); memset(temp,0,sizeof(temp)); int cnt = 0 ; //赋值第一行的翻转状态 for(int j = 0 ;j < m; ++j) { if(key & (1<<(m-1-j))) { updata(0,j); cnt++; } } //从第二行消灭1 for(int i = 1 ; i < n ; ++i) for(int j = 0 ; j< m ; ++j) { if(state[i-1][j]) { cnt++; updata(i , j); } } //检测并更新答案 if(isOk() && cnt < Ans) { Ans=cnt; memcpy(ans,temp,sizeof(temp)); }}int main(){ while(~scanf("%d%d",&n,&m)) { memset(ans,0,sizeof(ans)); for(int i = 0; i < n; ++i) for(int j = 0 ;j < m ; ++j)scanf("%d",&tu[i][j]); Ans = inf ; for(int key = 0 ; key < (1<<m) ; ++key)check(key); if(Ans == inf) { puts("IMPOSSIBLE"); continue; } for(int i = 0 ;i< n ; ++i) { for(int j = 0 ;j < m ; ++j) { if(j)printf(" "); printf("%d",ans[i][j]); } puts(""); } } return 0;}
- POJ-3279-Fliptile
- POJ 3279 Fliptile
- poj 3279 Fliptile
- POJ 3279 Fliptile
- poj 3279 Fliptile
- POJ 3279 Fliptile
- poj 3279 Fliptile
- Poj 3279 Fliptile 【枚举】
- POJ 3279 Fliptile
- POJ 3279 Fliptile
- POJ 3279 Fliptile
- POJ - 3279 Fliptile(DFS)
- poj 3279 Fliptile
- POJ 3279 Fliptile
- poj-3279 Fliptile
- POJ 3279 - Fliptile
- poj 3279 Fliptile(dfs)
- POJ 3279 Fliptile(反转)
- exit与return区别
- 我為什麼鼓勵工程師寫 blog
- String(byte[] bytes, Charset charset) 和 getBytes() 使用
- nohup &
- webstorm添加vue模板支持
- poj 3279 Fliptile
- 单独安装pgAdmin的小问题和解决
- 软编码Flv 到Mp4 容器(四) fmp4 总览和基础讲解
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- android Activity生命周期总结
- Unity Mecanim动画的实现(八):Animation视图
- ButterKnife初体验与认知JakeWharton
- 生成窗口最大值数组
- Keil uVision4教程