Fliptile
来源:互联网 发布:淘宝模板自己怎么设计 编辑:程序博客网 时间:2024/06/15 03:03
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
- 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
- JAVASE基础-day04(Java语言基础)
- spring MVC执行流程
- Python的字符串分片
- Android编程权威指南学习笔记1
- python+selenium中遇到的难题及解决方法
- Fliptile
- QT学习笔记
- apache kafka监控系列-KafkaOffsetMonitor
- Fiddler操作具体实践之断点调试
- 极限单车算法
- 美的殉道者——关于傅雷的断想
- Homework #1 for MAFS 5220
- android.graphics包中的一些类的使用
- Android自定义开关