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),问你最少踩哪些砖块才能让初始的砖块全部变 ...
随机推荐
- java CyclicBarrier同步屏障
CyclicBarrier的字面意思是可循环使用的屏障,它的主要作用是,让一组线程到达一个屏障时被阻塞,知道最后一个线程到达屏障时,屏障才会打开,所有被屏障拦截的线程才会继续运行. 1.简介: Cyc ...
- chart.js 使用方法 特别说明不是中文的
以上是一个饼图的案例,其他统计类型查看文档 http://www.chartjs.org/docs/latest/charts/doughnut.html 注意看域名 chartjs.org 不是 ...
- 多个图标图片(雪碧图)使用CSS样式显示
现在的网页中显示很多图标算是常态,发现项目中页面上用到的图标都是单个图标单个文件,用的时候直接往页面上挂,这确实很常态. 如果,网站是挂在外网上,或者网速过低,又大量使用图标的情况下,由于浏览器和服务 ...
- Android 应用开机自启和无需权限开启悬浮框
开机自启主要自定义广播接收类,且需要在清单文件中注册,不要在代码中动态注册. <uses-permission android:name="android.permission.REC ...
- Android 超简单的拖动按钮 悬浮按钮 吸附按钮
第一种 第二种 第一种实现方法 xml布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...
- nbtscan ip地址
查找网络(192.168.1.0)中netbios名字信息,对应命令如下: nbtscan 192.168.1.1-254 找到有netbios名字后,可以使用如下的命令查看这些主机运行的服务. nb ...
- 关于HTML中时间格式以及查询数据库的问题
1.默认时间格式,加入属性dateFormate="yyyy-MM-dd" 2.设置默认值,value="2017-6-22" 3.在JavaScript中将获 ...
- Hadoop之—— WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...
[hadoop@hadoop000 hadoop]$ ldd --version ldd (GNU libc) 2.12 Copyright (C) Free Software Foundation, ...
- linux下如何实现mysql数据库定时自动备份
概述 备份是容灾的基础,是指为防止系统出现操作失误或系统故障导致数据丢失,而将全部或部分数据集合从应用主机的硬盘或阵列复制到其它的存储介质的过程.而对于一些网站.系统来说,数据库就是一切,所以做好 ...
- C++各大有名科学计算库(转)
在 C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功能要好过设计更多的语法的言论.现实中,C++的库门类繁多,解决的问题也是极其广泛,库从轻量级 ...