Fliptile(枚举+深搜)
来源:互联网 发布:思拓CMS 编辑:程序博客网 时间:2024/05/22 15:56
FliptileTime Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
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
Sample Input
4 41 0 0 10 1 1 00 1 1 01 0 0 1
Sample Output
0 0 0 01 0 0 11 0 0 10 0 0 0
题意:对所给的矩阵进行翻转,使其全部为0;输出各个位置翻转次数(总次数最小的那个)
解析:本来想着对每个位置翻转与不反转进行暴力枚举用dfs()实现,对所有的答案判断是否符合条件,但不知道什么原因,出错,写得很复杂很暴力
正解:对于这个问题其实只要枚举第一行的情况就可以了,对于第一行的情况确定后,其余都会确定了
#include <iostream>#include <cstdio>#include <cstring>#define maxx 16#define inf 0x3f3f3fusing namespace std;int n,m,minx=inf;int temp[maxx][maxx],tmp[maxx][maxx],map[maxx][maxx],ans[maxx][maxx];void update(int x,int y){int tx,ty;int next[5][2]={{0,0},{0,1},{1,0},{-1,0},{0,-1}};tmp[x][y]=1;for(int i=0;i<5;i++){tx=x+next[i][0];ty=y+next[i][1];temp[tx][ty]=!temp[tx][ty];}}int judge(){for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(temp[i][j]==1){return 0;}}}return 1;}void solve(int x){memset(tmp,0,sizeof(tmp));memcpy(temp,map,sizeof(map));int cnt=0;for(int i=0;i<m;i++)//从0位开始 {if((x>>i)&1)//x<<i表示x除以2的i次方,&1表示是否为奇数,奇数为1,偶数为0,总的来说就是逆序的i位是1还是0{update(1,i+1);cnt++;}}for(int i=2;i<=n;i++){for(int j=1;j<=m;j++){if(temp[i-1][j]==1){update(i,j);cnt++;}}}if(judge()&&cnt<minx){minx=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]);}}for(int i=0;i<(1<<m);i++){solve(i);}if(minx==inf){printf("IMPOSSIBLE\n");}else{for(int i=1;i<=n;i++){for(int j=1;j<m;j++){printf("%d ",ans[i][j]);}printf("%d\n",ans[i][m]);}}return 0;}
- Fliptile(枚举+深搜)
- POJ - 3279----Fliptile(枚举,遍历)
- POJ 3279 Fliptile 反转 (二进制枚举)
- Fliptile 暴力枚举(可以写成搜索。。。)
- Poj 3279 Fliptile 【枚举】
- POJ 3279 Fliptile【枚举】
- (枚举)dfs-POJ 3279Fliptile (详)
- POJ 3279 Fliptile (暴力枚举)(D)
- 【POJ】3279 Fliptile(十字变换搜索+二进制枚举)
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
- bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏(枚举)
- poj 3279 Fliptile 枚举子集
- POJ 3279 Fliptile(枚举状态)
- poj 3279 Fliptile 二进制枚举
- poj 3279 Fliptile 枚举 模拟
- poj3279 Fliptile【二进制枚举状态】
- POJ 3279 Fliptile (二进制枚举+模拟)
- Fliptile POJ3279 二进制压缩枚举 解题报告
- (编程题)5-2然后是几点
- ssh 常见面试题
- JavaScript事件基础
- 用 VMware 12 Player 安装Ubuntu 14.04.5 分区时提示“没有定义根文件系统,请回到分区菜单以修正此错误”
- codevs 1361_知识排名_模拟
- Fliptile(枚举+深搜)
- 【bzoj3224】文艺平衡树
- 物理模拟&碰撞检测 2017-2-4
- CS 20SI|Lecture 3 Linear and Logistic Regression in TensorFlow
- 利用sas软件将txt文档转为excel文档
- 数据结构之二叉查找树
- Datetime,Python(2017/02/04)
- Eclipse 向导机制扩展 -- 实现可定制的向导
- Qt入门之基础篇 ( 一 ) :Qt4及Qt5的下载与安装