Fliptile 暴力枚举(可以写成搜索。。。)
来源:互联网 发布:松下plc编程视频教程 编辑:程序博客网 时间:2024/06/03 13:11
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 anM × 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 withN 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
题意:给定一个0,1矩阵,要求用最少的次数将矩阵翻转为全是0。每次翻转一个值时周围4个和自己一起翻转。
解析:一看并没有发现和搜索有什么关系,然后去看了一下别人解析,发现确实和搜索没有什么很大关系。。。暴力枚举,因为我们如果一行行往下翻转的话,会发现只要上面的硬币是1,那么这个硬币就必须翻转,否则不能翻转。那这样的话,我们只要枚举出第一行所有的状态,那么下面每一行的状态我们就知道了。因为最后一行是由倒数第二行决定的,所以最后一行我们无法保证都是0,那我们最后进行检测,如果最后一行都是0,则方案可取,否则不可取。最后得出翻转最少的状态。我的第一行所有的状态和下面对应每一行的状态是用搜索写的,不用也可以。
#include<iostream>#include<cstring>using namespace std;int m,n;int sum=100000000;int a[17][17]; //用来储存开始的矩阵bool b[17][17]; //表示当前位置是否翻转,1表示翻转bool c[17][17]; //用来储存最后输出的翻转状态int judge() { //判断最后一行是否全为0for(int i=1; i<=n; i++)if((a[m][i]+b[m][i]+b[m][i-1]+b[m][i+1]+b[m-1][i])%2==1)return 0;return 1;}void dfs(int k,int num) { //再已知第一行的状态下找出下面每一行的状态if(num>=sum)return ;if(k>m) {if(judge()) {memcpy(c,b,sizeof(b));sum=num;}return ;}for(int i=1; i<=n; i++) { if((a[k-1][i]+b[k-1][i-1]+b[k-1][i]+b[k-1][i+1]+b[k-2][i])%2==1) {//判断上一行此位置现在是否为1b[k][i]=1;num++;} elseb[k][i]=0;}dfs(k+1,num);}void first_line(int k,int num) { //用来枚举第一行所有的状态if(k>n) {dfs(2,num);return ;}b[1][k]=0;first_line(k+1,num);b[1][k]=1;first_line(k+1,num+1);}int main() {cin>>m>>n;for(int i=1; i<=m; i++)for(int j=1; j<=n; j++)cin>>a[i][j];memset(b,0,sizeof(b));first_line(1,0);if(sum==100000000)cout<<"IMPOSSIBLE";elsefor(int i=1; i<=m; i++) {for(int j=1; j<n; j++)cout<<c[i][j]<<" ";cout<<c[i][n]<<endl;}}
- Fliptile 暴力枚举(可以写成搜索。。。)
- POJ 3279 Fliptile (暴力枚举)(D)
- poj3279 Fliptile dfs暴力枚举状态
- POJ 3279 Fliptile(二进制枚举暴力)
- 【POJ】3279 Fliptile(十字变换搜索+二进制枚举)
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
- Fliptile(枚举+深搜)
- POJ 3279 Fliptile(暴力)
- POJ 3279Fliptile(状态枚举+搜索)
- POJ3279 Fliptile题解 二进制枚举搜索法
- 称砝码(搜索\暴力枚举)
- POJ 3279 Fliptile 开关灯问题(状态压缩加暴力搜索)
- poj3279 Fliptile (dfs搜索)
- POJ 3279 Fliptile(状态压缩+暴力)
- POJ - 3279----Fliptile(枚举,遍历)
- POJ 3279 Fliptile 反转 (二进制枚举)
- POJ 1753 Flip Game 暴力搜索(dfs加枚举)
- POJ3279 Fliptile 【暴力】
- 二叉树
- Java300StudyNote(9)-unicode和GBK,UTF-8,UTF-16,图解各种字符集之间的关系、Java内部使用的字符集、字符集的兼容性问题、乱码问题
- angualrjs--倒计时60秒
- CodeForces
- Microsoft SQL Server学习(四)--约束
- Fliptile 暴力枚举(可以写成搜索。。。)
- Framework学习(五)应用程序启动过程
- 给定两个排好顺序的数组,快速找到两数组中相同的数值
- Python多进程协程爬虫----1
- Matlab中控制系统设计pade语句
- Combination Sum
- fastjson:实现对java.nio.ByteBuffer数据类型的支持
- (二)从JSP说起
- 最大连续子序列最大和的四种解法