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),问你最少踩哪些砖块才能让初始的砖块全部变 ...
随机推荐
- wamp 下运行Drupal慢的解决方法
1.Editing your php.ini and make realpath_cache_size=2M, 2.uncomment skip innodb in your my.cnf(my.in ...
- form中onsubmit的使用
form 中的onsubmit在点submit按钮时被触发,如果return false;则结果不会被提交到action中去(也就是提交动作不会发生),如果不返回或者返回true,则执行提交动作.(& ...
- POJ 2253 ——Frogger——————【最短路、Dijkstra、最长边最小化】
Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- JavaSE_3_面向对象
1.wait方法底层原理 wait是object中的方法,可以暂停线程,会释放对象锁,不像sleep方法,线程休眠期依然持有锁,通过调用notify或notifyAll方法唤醒线程. lock.wai ...
- Js常见算法实现汇总
/*去重*/ <script> function delRepeat(arr){ var newArray=new Array(); var len=arr.length; for(var ...
- Bootstrap学习笔记(三)
三.组件 1.图标字体 图标字体本质是文字,而不是图形!可以无限放大或修改颜色. 使用方式:<span class="glyphicon glyphicon-XXX"> ...
- 三、css 和 js 的装载与执行
一个网站在浏览器端是如何渲染的? 一.html 页面加载渲染的过程. 请求回来最先应该是HTML,从一个字节流转换成字符流,浏览器拿到字符流,然后浏览器端进行相应的词法分析成相应的token,然后不断 ...
- webapp一些样式记录
图片外面的div设置宽高自适应width: 100vw; max-width: 640px; display: block; height: 43.75vw; max-height: 280px; f ...
- echarts折柱混合(图表数据与x轴对应显示)
一天24个小时,每个小时不一定都有对应的数据,所以后台给出的数据,只有每个时间点对应的数据,比如4点,给的是112,5点的242,其他时间没有,则只显示4点,5点时候的数据,那么现在对应的时间点就是后 ...
- php 01
PHP 一.了解php 1.什么是php PHP 超文本预处理器 服务器端的脚本语言 是一种被广泛应用的开放源代码的多用途脚本语言 他可以嵌入到html中 尤其适用web开发 2.php在web中 ...