Fliptile 开关问题 poj 3279
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4031 | Accepted: 1539 |
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int ans[MS][MS];
int pic[MS][MS];
int flag[MS][MS];
int M,N;
int dir[][]={{,},{,},{,},{,-},{-,}}; int color(int x,int y)
{
int sum=pic[x][y];
for(int i=;i<;i++)
{
int r=x+dir[i][];
int c=y+dir[i][];
if(r>=&&r<M&&c>=&&c<N)
sum+=flag[r][c];
}
return sum&;
} int calc()
{
int res=;
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(color(i-,j))
{
flag[i][j]=;
}
}
}
for(int i=;i<N;i++)
if(color(M-,i))
return -;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
res+=flag[i][j];
return res;
} void solve()
{
int res=-;
for(int i=;i<<<N;i++)
{
memset(flag,,sizeof(flag));
for(int j=;j<N;j++)
flag[][N--j]=(i>>j)&;
int cnt=calc();
if(cnt>=&&(res<||res>cnt))
{
res=cnt;
memcpy(ans,flag,sizeof(flag));
}
}
if(res<)
printf("IMPOSSIBLE\n");
else
{
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(j)
printf(" ");
printf("%d",ans[i][j]);
}
printf("\n");
}
}
} int main()
{
scanf("%d%d",&M,&N);
for(int i=;i<M;i++)
for(int j=;j<N;j++)
scanf("%d",&pic[i][j]);
solve();
return ;
}
Fliptile 开关问题 poj 3279的更多相关文章
- 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),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- 【POJ 3279 Fliptile】开关问题,模拟
题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...
- Fliptile POJ - 3279 (开关问题)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16483 Accepted: 6017 Descrip ...
- POJ 3279 Fliptile ( 开关问题)
题目链接 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give ...
- POJ - 3279 Fliptile(反转---开关问题)
题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 1.枚举第一行 ...
随机推荐
- 第三百二十八天 how can I 坚持
今天电脑快把我搞疯了,一天得死机快十次,不知道怎么回事,最后升级了win10,感觉就是比较好. 哎,成了这个样子,当初为什么又让我抽中了那个签,搞不懂啊,这都是为啥. 我哪里错了,还是冥冥中自有天意, ...
- TBluetoothLE
delphi 蓝牙技术 D:\Users\Public\Documents\Embarcadero\Studio\17.0\Samples\Object Pascal\Multi-Device Sam ...
- C++11角括号
[C++11角括号] 标准 C++ 的剖析器一律将 ">>" 视为右移运算符. 但在样板定义式中,绝大多数的场合其实都代表两个连续右角括号. 为了避免剖析器误判,撰码时 ...
- USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)
USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...
- C#图解教程读书笔记(第15章 委托)
委托是C#的一个很重要的知识点. 1.什么是委托 委托在我认为,就是一系列格式相同的方法列表,可能就是定义的名称不一致,参数.返回值等都是一样的. 2.如何声明委托 delegate void MyF ...
- Linux /proc 的意义
/proc 是个虚拟文件系统.也就是.重新引导后修改会被重新初始化 提供了进程信息.内存资源.硬件设备.内核内存等信息 比如: 网卡:/proc/sys/vm/ipv4/ip_ ...
- Java Service Wrapper配置详解
#encoding=UTF-8 # Configuration files must begin with a line specifying the encoding # of the the fi ...
- C# try catch finally 执行
try { //dosomething eg: int a = 1; int b = 2; int c = a + b; if(c>2) { return; } } catch(Exceptio ...
- Java工具类 Apache Commons:commons-lang
Commons Lang The standard Java libraries fail to provide enough methods for manipulation of its core ...
- Js制作点击输入框时默认文字消失的效果
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-02-17) 为了提高用户体验和易用度,一些设计师会对网页中用户经常用的东西进行优化,比如输入框.一般的输入框是怎样优化的呢 ...