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
思路:不知道为啥这道题目会放在搜索专题上,可能搜索的方法也可以解决吧。
首先我们看数据量,最大是15.状压的一个很明显的特点就是数据量小。我们如果按照暴力的方法的话,数据量就特别大,肯定会超时。那么我们就将第一行是否翻转的情况枚举一下。
假如是m列的话,我们枚举0~(2^m-1)的数字,转换成二进制,就是01串。vis[i][j]=0就是不翻转,vis[i][j]=1代表翻转。
我们枚举了第一行的翻转情况,然后从第二行开始,如果该位置的上一个位置是黑色的话,这一个位置,是必须要翻转的。翻转完成之后,如果最后一行全是白色的话,就说明第一行的翻转是可行的,记录一下最小值。
题目要求字典序最小,我们枚举的顺序正好满足了字典序最小。

这是转换成二进制串的代码,结果如图所示:

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;const int maxx=16;
int a[maxx][maxx];//原数组
int vis[maxx][maxx];//翻转数组
int ans[maxx][maxx];//答案数组
int d[][2]={{1,0},{0,1},{-1,0},{0,-1},{0,0}};//上下左右中
int n,m;bool judge(int x,int y)//判断一个点的颜色,是这个点本身的颜色+翻转的次数;如果本身是白色的话,翻转奇数次是黑色;如果本身是黑色的话,翻转偶数次是黑色。这两种情况加起来都是奇数。
{int as=a[x][y];//本身的颜色for(int i=0;i<5;i++){int tx=x+d[i][0];int ty=y+d[i][1];if(tx<0||tx>=n||ty<0||ty>=m) continue;as+=vis[tx][ty];//翻转的次数}return as&1;//判断是否为奇数
}
inline int solve()
{for(int i=1;i<n;i++)for(int j=0;j<m;j++) if(judge(i-1,j)) vis[i][j]=1;//判断上一个点是否为黑色,如果是黑色,这个点必须要翻转for(int i=0;i<m;i++) if(judge(n-1,i)) return -1;//最后一行如果有黑色的话,就说明这个情况不符合。int num=0;for(int i=0;i<n;i++) for(int j=0;j<m;j++) num+=vis[i][j];//记录一下翻转的次数return num;
}
int main()
{while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++){for(int j=0;j<m;j++) scanf("%d",&a[i][j]);}int flag=0,num;int Ans=inf;for(int i=0;i<(1<<m);i++){memset(vis,0,sizeof(vis));for(int j=0;j<m;j++) vis[0][m-j-1]=(i>>j)&1;//转换成二进制串num=solve();if(num!=-1){flag=1;if(num<Ans){Ans=num;for(int o=0;o<n;o++) {for(int p=0;p<m;p++) ans[o][p]=vis[o][p];}}}}if(!flag) cout<<"IMPOSSIBLE"<<endl;else {for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout<<ans[i][j]<<" ";cout<<endl;}}}return 0;
}

努力加油a啊

Fliptile(状压+思维)相关推荐

  1. C. Qualification Rounds(状压思维)

    C. Qualification Rounds(状压&思维) 若有解最多选两个. 选奇数肯定没有选偶数优,在奇数的基础上减少一个1最多的问题是更优的. 所以只需考虑偶数情况. 假设存在4个问题 ...

  2. 【思维题 状压dp】APC001F - XOR Tree

    可能算是道中规中矩的套路题吧-- Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...

  3. CodeForces - 1567C Carrying Conundrum(思维/状压)

    题目链接:点击查看 题目大意:规定加法中使用隔项进位,问给定的 nnn 有多少种方案可以通过 "隔项进位加法" 得到 题目分析:隔项进位意味着奇偶位置的数字互不影响,所以将奇偶位置 ...

  4. [HNOI2012]集合选数(思维构造 + 状压dp)

    problem 题目链接 solution 从最小一个数 xxx 开始,将其 2x,3x2x,3x2x,3x 放入,再将 2(2x),3(2x),2(3x),3(3x)2(2x),3(2x),2(3x ...

  5. 【思维·状压】 jzoj1434灌水(COCI2009) 纪中集训提高B组

    Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits Description 学生都很喜欢灌水,第一天只有Alice给她的每个朋友灌 ...

  6. NowCoder110E Pocky游戏 状压DP

    传送门 题意:给出$N$个数和一个长为$M$.所有数在$[1,N]$范围之内的正整数序列$a_i$,求出这$N$个数的一种排列$p_1...p_N$使得$\sum\limits_{i=2}^M |p_ ...

  7. [蓝桥杯][算法提高VIP]Sharing Chocolate(状压dp记忆化搜索)

    题目描述 每天,巧克力在它的许多形式上被全世界数百万人分享.它是一个真正普遍的糖果,实际上在世界上每个国家都能得到. 你发现唯一比吃巧克力更好的事情是把它分享给朋友.不幸的是,你的朋友非常挑剔,有着不 ...

  8. agc012E Camel and Oases(状压dp+思路题)

    这题神啊.状压dp你敢信?思维难度爆表还有一堆细节要注意???orz Visjiao 原题链接:http://agc012.contest.atcoder.jp/tasks/agc012_e 大神题解 ...

  9. [状压dp] 蒙德里安的梦想(模板题+状压dp)

    文章目录 0. 前言 1. 状压dp 模板题 0. 前言 状压 dp 就是采用二进制数保存状态,方便进行位运算操作.例如 八皇后.八数码问题也都是采用了状态压缩的思想来使用一个二进制数唯一对应集合中的 ...

最新文章

  1. Nat. Mach. Intell. | 华科同济医学院剑桥联手推出新冠预测模型!
  2. 【博客话题】我的linux心路历程
  3. 【Top】Plan (updating...)
  4. 51nod 1004 【快速幂】
  5. 谁更安全?WI-FI无线网加密方式大比拼
  6. python基本代码教程-python基础教程
  7. RabbitMQ(7)-发后即忘模型
  8. java类的加载与初始化_Java类何时以及如何加载和初始化?
  9. python课程预告_Python3编程预告
  10. html中的背景颜色渐变效果,如何CSS实现网页背景三种颜色渐变效果?
  11. PHP实现图片加文字/图案水印
  12. 使用toUpperCase toLowerCase getBytes方法实现一串字母字符的大小写转换
  13. Android 控件获取焦点
  14. Photoshop - 教您怎样对扫描文件去除底色?
  15. 密集芯片的焊接技巧:从LQFP64说起
  16. 计算机歌曲数我的一个道姑朋友,同人歌|我的一个道姑朋友
  17. 潭州学院html学习(day03)
  18. DL实现semantic segmentation
  19. 考试必备技能--自制题库
  20. SQLite 使用问题记录(一)

热门文章

  1. Python—实训day9—使用pandas进行数据预处理
  2. Android开发面试题之求一个数的N次幂手写算法
  3. Fragment切换。radiobutton加fragment切换(附件源码下载)
  4. android异步网络连接开源:Android Asynchronous Http Client
  5. iOS开发如何实现消息推送机制
  6. 5.SpringMVC
  7. React优化性能的经验教训
  8. 通过自定义配置实现插件式设计
  9. find命令--Linux命令应用大词典729个命令解读
  10. 项目中SQL语句文件保存及提取方法