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

题意:给你一个给你一个矩阵,有黑白两个状态。每次你可以选择一个变为其相反的状态,它周围的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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310024.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

新基建火了,开源云计算渠道能做什么?

导语对于开源云计算厂商而言&#xff0c;如果希望在抢滩新基建上构建差异化竞争优势&#xff0c;具备高超的售前技能、售后体验&#xff0c;并拥有创新的技术服务能力与解决方案构建能力是实有必要的。巧了&#xff0c;这些都与渠道构建息息相关。开源云计算厂商在此前的渠道激…

python socket编程之双方相互通信简单实例_Python socket实现的简单通信功能示例

套接字(socket)是计算机网络数据结构&#xff0c;在任何类型的通信开始之前&#xff0c;网络应用程序必须创建套接字&#xff0c;可以将其比作电话的插孔&#xff0c;没有它将无法进行通信常用的地址家族AF_UNIX&#xff1a;基于文件&#xff0c;实现同一主机不同进程之间的通信…

[C++STL]常用拷贝和替换算法

代码如下: #include <iostream> #include <algorithm> #include <vector> #include <ctime> using namespace std;void myPrint(int val) {cout << val << " "; }void test01() {vector<int>v1;for (int i 0; i < 10…

Jin Ge Jin Qu hao UVA - 12563 (劲歌金曲)01背包,求装入的东西最多(相同多时价值大)

题目&#xff1a;白书p274 题意: KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<t-1) ? 最终输出num1 和…

ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记

任务16&#xff1a;oauth2 oidc 实现 client部分实现 client 之前启动一下上一节的 server&#xff0c;启动之前需要清除一些代码注释 Program 的 MigrateDbContextpublic static void Main(string[] args) {BuildWebHost(args)//.MigrateDbContext<ApplicationDbContext&g…

java中vi是什么意思_java中的public void是什么意思?

java中的public void是什么意思&#xff1f;发布时间&#xff1a;2020-05-23 11:21:22来源&#xff1a;亿速云阅读&#xff1a;255作者&#xff1a;Leahjava中的public void是什么意思&#xff1f;相信很多新手都不太了解&#xff0c;今天小编为了让大家更加了解public void的意…

[C++STL]常用算术生成算法

代码如下: #include <iostream> #include <vector> #include <numeric> using namespace std;void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}int total accumulate(v.begin(), v.end(), 0);cout << "total …

前端异步对象的原理与使用方法

源宝导读&#xff1a;现今互联网的WEB网站&#xff0c;几乎没有不用到JS异步技术的&#xff0c;虽然大家经常用到&#xff0c;但Javascript提供的异步机制如何才能真正用好呢&#xff0c;可能很多开发小伙伴还有点含糊&#xff0c;本文将从常见的开发场景出发&#xff0c;系统的…

Millenium Leapcow POJ - 2111 (千禧年跳牛)(贪心找最长路径,记忆化)

题意&#xff1a; 给你一个矩阵&#xff0c;问你按照象棋马的走法&#xff0c;下一步比上一步的数大&#xff0c;问长度最长的序列是多长&#xff0c;然后输出序列。如果有多个最长序列输出字典序最小的那个。类似滑雪&#xff0c;找出最长路径&#xff0c;多个答案 输出字典序…

java1.8的stream_JDK1.8新特性(一):stream

搜索热词一.什么是stream&#xff1f;1.概述Java 8 API添加了一个新的抽象称为流Stream&#xff0c;可以让你以一种声明的方式处理数据。这种风格将要处理的元素集合看作一种流&#xff0c; 流在管道中传输&#xff0c; 并且可以在管道的节点上进行处理&#xff0c; 比如筛选&a…

[C++STL]常用集合算法

代码如下: #include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std;class myPrint { public:void operator()(int val){cout << val << " ";} };void test01() {vector<int&g…

php给html传值,PHP传值到不同页面的三种常见方式及php和html之间传值问题_PHP

在项目开发中经常见到不同页面之间传值在web工作中&#xff0c;本篇文章给大家列出了三种常见的方式。接触PHP也有几个月了&#xff0c;本文总结一下这段日子中&#xff0c;在编程过程里常用的3种不同页面传值方法&#xff0c;希望可以给大家参考。有什么意见也希望大家一起讨论…

Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

题意&#xff1a;给一个字符串S&#xff0c; 求出所有前缀pre&#xff0c;使得这个前缀也正好是S的后缀。 输出所有前缀的结束位置。 就是求前缀和后缀相同的那个子串的长度 然后从小到大输出,主要利用next数组求解。 例如 “ababcababababcabab”&#xff0c; 以下这些前缀…

2020 年了,WPF 还有前途吗?

2020年了&#xff0c;微软的技术也不断更新了, .Net Core 3.1、.Net Framework 4.8以及今年11月推出的.NET 5...win10平台也普及了很多&#xff0c;WPF可以在上面大展身手&#xff0c;可性能和内存占用还是不行&#xff0c;但是WPF强大的UI能力很吸引人。WPF已经凉了吗? 学WPF…

[C++STL]常用查找算法

代码如下: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std;void test01() {vector<int>v;for (int i 0; i < 10; i){v.push_back(i);}vector<int>::iterator it find(v.begin(…

php asp 语法,ASP 语法

在我们的 ASP 教程中&#xff0c;每个实例都提供隐藏的 ASP 源代码。这样会使您更容易理解它们的工作原理。向浏览器写输出ASP 文件通常包含 HTML 标签&#xff0c;就像 HTML 文件。然而&#xff0c;ASP 文件也能包含服务器脚本&#xff0c;这些脚本被分隔符 包围起来。服务器脚…

#10003. 「一本通 1.1 例 4」加工生产调度(贪心)

加工生产调度 题目描述 某工厂收到了n个产品的订单&#xff0c;这n个产品分别在A、B两个车间加工&#xff0c;并且必须先在A车间加工后才可以到B车间加工。 某个产品i在A、B两车间加工的时间分别为Ai、Bi。询问怎样安排这n个产品的加工顺序&#xff0c;才能使总的加工时间最短…

不要把异常当做业务逻辑,这性能可能你无法承受

一&#xff1a;背景1. 讲故事在项目中摸爬滚打几年&#xff0c;应该或多或少的见过有人把异常当做业务逻辑处理的情况(┬&#xff3f;┬)&#xff0c;比如说判断一个数字是否为整数,就想当然的用try catch包起来&#xff0c;再进行 int.Parse&#xff0c;如果抛异常就说明不是整…

[设计模式]开闭原则

开闭原则: 对扩展开放&#xff0c;对修改关闭。 增加功能是提过增加代码来实现的&#xff0c;而不是去修改源代码。 代码如下: #include <iostream> #include <string> using namespace std;class Caculaor { public:Caculaor(int a,int b,string c):a(a),b(b…

#10010 「一本通 1.1 练习 6」糖果传递 (数学+贪心)

题目描述 原题来自&#xff1a;HAOI 2008 有 n个小朋友坐成一圈&#xff0c;每人有 ai 颗糖果。每人只能给左右两人传递糖果。每人每次传递一颗糖果的代价为 1 。求使所有人获得均等糖果的最小代价。 输入格式 第一行有一个整数 &#xff0c;n表示小朋友个数&#xff1b; …