题意:给你一个给你一个矩阵,有黑白两个状态。每次你可以选择一个变为其相反的状态,它周围的4个都会变成相反的状态。问你最少需要改变多少个使得矩阵中的状态全为白色,若有多个答案,输出字典序最小的

思路:白书p153

题解:这个题仍然是个反转问题,我们只需要枚举第一行(二进制)进行翻转的坐标, 然后根据当前这块上面那块是否是黑色(依据该块上面本来是什么以及周围或者自身总共反转了多少次确定)最后得出该块是否需要反转, 最后只需要特判最后一行是否合理即全为白色,并进行维护更新答案即可。

另外其实每个点最多只会被翻转一次,因为如果翻转两次和不翻转是一样的。

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 Nspace-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
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=110;
int m,n;
int mp[M][M];
int s[M][M];//保存中间结果
int w[M][M];///保存最优解,s,w,记录的都是翻动操作
int e[5][2]= {0,0,0,1,1,0,-1,0,0,-1};//领接格子的坐标包括本身
int f(int x,int y)//查询(x,y)的颜色判断该点是否为黑色
{int xx=mp[x][y];for(int i=0; i<5; i++){int u=x+e[i][0],v=y+e[i][1];if(u>=0&&u<m&&v>=0&&v<n)xx+=s[u][v];}return xx%2;
}
int bfs()//求出第一行确定情况下的最小操作,不存在解的话,返回-1
{int ant=0;for(int i=1; i<m; i++)//求出从第二行开始翻转for(int j=0; j<n; j++)//上方格子是黑色,必须必须反转(i,j)号格子if(f(i-1,j))s[i][j]=1;for(int i=0; i<n; i++)//判断最后一行是否全白if(f(m-1,i))return -1;for(int i=0; i<m; i++)//统计翻转的次数for(int j=0; j<n; j++)ant+=s[i][j];return ant;
}
void solve()
{int ans=0x3f3f3f3f;for(int i=0; i<(1<<n); i++)//按字典序尝试第一行的所有可能性{memset(s,0,sizeof(s));for(int j=0; j<n; j++)//给第一行各个位置是否翻动按字典序赋值s[0][n-j-1]=i>>j&1;int num=bfs();if(num>=0&&ans>num){ans=num;memcpy(w,s,sizeof(s));//把dp数组复制给s数组(int数组的复制memcpy)}}if(ans==0x3f3f3f3f)printf("IMPOSSIBLE\n");else{for(int i=0; i<m; i++)///输出翻动操作{for(int j=0; j<n-1; j++)printf("%d ",w[i][j]);printf("%d\n",w[i][n-1]);}}
}
int main()
{while(~scanf("%d%d",&m,&n)){memset(w,0,sizeof(w));for(int i=0; i<m; i++)for(int j=0; j<n; j++)scanf("%d",&mp[i][j]);solve();}return 0;
}

Fliptile POJ - 3279 (翻转)(二进制+对第一行暴力遍历翻转的位置)相关推荐

  1. D - Fliptile POJ - 3279(翻转问题)

    D - Fliptile POJ - 3279 题意: 给一个m*n的01矩阵,对某一块砖踩一脚,1->0 || 0->1, 求将整个举证全部变为0最少踩几次 典型的翻转问题,此类问题的特 ...

  2. [kuangbin带你飞]专题一 简单搜索D - Fliptile(POJ 3279)

    题目大意 给一个N行M列的矩阵,值分别为0和1,每次你可以选择将一个变成相反状态,同时,它周围的四个数也会变为相反状态. 问:最少翻转多少次,可以将所有值都变成0 多个解,输出翻转次数最少的(若有次数 ...

  3. 算法 Tricks(四)—— 获取一个数二进制形式第一个不为 0 的位置

    int n = ...; int flag = 1; while ((flag & n) == 0) flag <<= 1;// & 运算时,其实判断的是二者的二进制形式:

  4. POJ 3279 Fliptile

    传送门:http://poj.org/problem?id=3279 Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  5. 黑白翻转棋算法java实现_黑白翻转棋算法java实现

    翻转游戏是在4*4的正方形里进行的,每个小正方形放有拥有黑白两面的棋子.每一轮你翻转3-5个棋子,把它从白变黑或从黑变白. 翻转的规则如下: 1. 选择任意一个棋子. 2. 翻转选择的棋子和与它相临的 ...

  6. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  7. POJ - 3279 Fliptile(状态压缩+位运算+暴力)

    题目链接:点击查看 题目大意:给出一个n*m的01矩阵,为了好描述,我们设0和1是两个相反的状态,我们的目标是要将整个矩阵全部变成1,现在我们可以将某一个点(x,y)更改为相反的状态,不过相应的该点周 ...

  8. 凡人修c传(四)翻牌子(POJ - 3279 - Fliptile)(思维+dfs)

    本萌新初来乍到,不知代码的险恶,曾看过一篇帖子,大佬说他一天就打了一题,十分崩溃,底下评论芸芸,另一位大佬说,没事,可能将来你一天看一个题解都未必能看懂. 当我打完这题时,我觉得大佬说的对...... ...

  9. 凡人修c传(四)翻牌子(POJ - 3279 - Fliptile每日一水)

    本萌新初来乍到,不知代码的险恶,曾看过一篇帖子,大佬说他一天就打了一题,十分崩溃,底下评论芸芸,另一位大佬说,没事,可能将来你一天看一个题解都未必能看懂. 当我打完这题时,我觉得大佬说的对...... ...

最新文章

  1. ATMEGA328实验电路板
  2. Leetcode 120. 三角形最小路径和 (每日一题 20210927)
  3. ios 后台唤醒应用_IOS开发之----详解在IOS后台执行
  4. 系统架构师学习笔记-数据通信与计算机网络(一)
  5. 移动页面自适应手机屏幕宽度HTML5开发
  6. NSString常用方法
  7. HDU 4334 5-sum
  8. [转载] Python中NumPy简介及使用举例
  9. Cesium:加载本地高程/地形数据
  10. 微软 补丁 服务器,2003到2016各版服务器操作系统ms17-010补丁直链地址
  11. Snap7 在西门子PLC的使用
  12. 《Head First 设计模式》(三):装饰者模式
  13. 互联网入口,一个正在消失的“黑洞”
  14. 多张图片合成一张jpg工具_如何将两张图片合成一张图片?
  15. web漏洞类型概述(owasp top10笔记)
  16. 2个鸡蛋,100层楼问题
  17. MATLAB生成一段音乐《小星星》
  18. 第三方支付为什么会兴起
  19. 4.19@深圳 | 品牌出海正当时,如何更有方向感?
  20. Peregrine半导体推出DOCSIS 3.1标准RF开关IC

热门文章

  1. Android Studio之编译提示\app\src\main\res\values\colors.xml:1:1 Error:前言有不允许的内容
  2. Android之Android Studio三种方式导入外部资源 以及 报错处理
  3. es mysql 同步插件_[es和数据库怎么同步]mysql与elasticsearch实时同步常用插件及优缺点对比(ES与关系型数据库同步)...
  4. 导出jar插件_Fluttify输出的Flutter插件工程详解
  5. ​怎么用藏头诗向女友表白......
  6. 豆瓣9.6分!这部BBC的纪录片太让人震撼!
  7. 男女共厕,考验男生心理素质的时候到了......
  8. 白话AI:看懂深度学习真的那么难吗?初中数学,就用10分钟
  9. Java学习笔记之 IO包 字符流
  10. mysql 1054 42s22_MySQL ERROR 1054(42S22)