Fliptile POJ - 3279 (开关问题)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16483 | Accepted: 6017 |
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 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 题意:只含有0和1的格子,每次都可以反转其中一个,但反转的时候,这个格子的上下左右的四个格子也被反转了,就是一次反转像“十”一样的五个格子,求出最小步数完成时的每个格子的翻转次数,最小步数的解有多个时,输出字典序最小的一组。不存在的话就输出IMPOSSIBLE
思路:一个格子翻转两次会恢复原状,所以多次翻转的多余的。这道题目我们可以先翻转第一行,如果第一行翻转好了再翻转第二行,最后一行应该是不用翻转的,因为翻转势必会使得前一行已经翻转好的重新变1,所以最后要判断一下最后一行是不是都是0,如果有1的话那就意味着不存在可行的操作方法
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
const int dx[]={-,,,,};
const int dy[]={,-,,,};
int M,N,tile[maxn][maxn];
int opt[maxn][maxn]; //保存最优解
int flip[maxn][maxn]; //保存中间结果
//查询(x,y)的颜色
int get(int x,int y)
{
int c=tile[x][y];
for(int d=;d<;d++)
{
int nx=x+dx[d],ny=y+dy[d];
if(<=nx && nx<M && <=ny && ny<N)
c+=flip[nx][ny];
}
return c%;
}
//求出第一行确定情况下的最小操作次数
//不存在解就返回-1
int calc()
{
//求出从第二行开始的翻转方法
for(int i=;i<M;i++)
for(int j=;j<N;j++)
if(get(i-,j)!=) //(i-1,j)是黑色格子就必须要翻转
flip[i][j]=;
//判断最后一行是否是全白
for(int j=;j<N;j++)
if(get(M-,j)!=)
return -;
//统计翻转的次数
int res=;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
res+=flip[i][j];
return res;
} void solve()
{
int res=-;
//按照字典序尝试第一行的所有可能性
for(int i=;i<(<<N);i++)
{
memset(flip,,sizeof(flip));
for(int j=;j<N;j++)
flip[][N-j-]=i>>j&;
int num=calc();
if(num>= && (res< || res>num))
{
res=num;
memcpy(opt,flip,sizeof(flip));
} }
if(res<)
printf("IMPOSSIBLE\n");
else
{
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(j==)
printf("%d",opt[i][j]);
else
printf(" %d",opt[i][j]);
}
printf("\n");
}
}
}
int main()
{
cin>>M>>N;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
{
cin>>tile[i][j];
}
solve();
return ;
}
Fliptile POJ - 3279 (开关问题)的更多相关文章
- Enum:Fliptile(POJ 3279)
Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一 ...
- poj 3279(开关问题)(待完成)
传送门:Problem 3279 #include<iostream> #include<cstdio> #include<cstring> using names ...
- Fliptile(POJ 3279)
原题如下: Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16494 Accepted: 6025 D ...
- kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279
题目链接:https://vjudge.net/problem/POJ-3279 题意:格子有两面,1表示黑色格子,0表示白色格子,奶牛每次可以踩一个格子,踩到的格子和它周围的上下左右格子都会翻面,也 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
随机推荐
- mac-profile
Mac 中定义与Linux一样的profile.d 首先Mac是没有profile.d的 在/etc/profile文件中添加 for sh in /etc/profile.d/*sh; do [ - ...
- ASP.NET MVC CheckBoxFor为什么会生成hidden input控件
自己开发的公众号,可以领取淘宝内部优惠券 @Html.CheckBoxFor(m => m.Bool) 使用CheckBoxFor方法得到的html代码会是下面这个样子 <input ch ...
- html标签补充
<body>内常用标签 1.<div>和<span> <div></div> : <div>只是一个块级元素,并无实际的意义.主 ...
- agc016B - Colorful Hats(智商题)
题意 题目链接 有$n$个人,每个人有一种颜色,第$i$个人说除了我之外有$a_i$种不同的颜色,问是否存在一组合法解 Sol 700分的题就这么神仙了么..好难啊... 先说结论吧 设$mx, mn ...
- 海海DRM视频保护解密流程分析
环境及工具 手机 :小米手机 MI 2A 系统版本: Android 4.1.1 工具 : IDA pro 6.6 .C32Asm .VS2005 一:第一次打开加密视频会出现如下验证: ...
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
本文转自:http://www.trinea.cn/android/preloaddatacache/ 本文主要介绍一个支持自动向前或向后获取新数据的缓存的使用及功能.Android图片内存缓存可见I ...
- Tomcat中配置JAVA_HOME
一般来说我们在使用Tomcat时配置JAVA_HOME都是采用的系统环境变量直接配置,下面我提供一种新的配置思路. 这里我们使用的是免安装版的apache-tomcat-7.0.35,首先我们安装好j ...
- NOIP2018初赛 解题报告
前言 \(NOIP2018\)初赛已经结束了,接下来就要准备复赛了. 不过,在此之前,还是先为初赛写一篇解题报告吧. 单项选择题 送分题.(虽然我还是做错了)可以考虑将它们全部转化为\(10\)进制, ...
- NBU基本常用命令
Veritas常用命令: 1. 查看当有运行的任务 bpdbjobs –report | grep Active 2. 停止任务 bpdbjobs –cancel PID (包括主任务和子任务) 3. ...
- iOS 3DTouch 的开发套路大全
今天休息,先来无事就自己看了一下,关于3d touch,看完后自己有动手尝试了一下,感觉也并不是很难, 经过大量的搜索,目前为止,发现有三种实现方式: 第一种:info文件中添加字段 这种方式,最为直 ...