POJ3279(KB1-D 熄灯问题)
Fliptile
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
//2017-02-22
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; bool grid[][], tmp[][];
int n, m, ans[], res[]; void flip(int x, int y)
{
tmp[x][y] = !tmp[x][y];
if(x->=)tmp[x-][y] = !tmp[x-][y];
if(y->=)tmp[x][y-] = !tmp[x][y-];
tmp[x+][y] = !tmp[x+][y];
tmp[x][y+] = !tmp[x][y+];
} void solve()
{
int penn, minflip = 0x3f3f3f3f;
bool fg = false;
for(int i = ; i < (<<m); i++)
{
for(int x = ; x < n; x++)
for(int y = ; y < m; y++)
tmp[x][y] = grid[x][y];
for(int y = ; y < m; y++)
if(i&(<<y))
flip(, m--y);
ans[] = i;
for(int x = ; x < n; x++){
penn = ;
for(int y = ; y < m; y++){
if(tmp[x-][y]){
flip(x, y);
penn += (<<(m--y));
}
}
ans[x] = penn;
}
bool ok = true;
for(int j = ; j < m; j++)
if(tmp[n-][j])
ok = false;
if(ok){
fg = true;
int cnt = ;
for(int j = ; j < n; j++){
for(int pos = ; pos < m; pos++)
if(ans[j]&(<<(m--pos)))cnt++;
}
if(cnt < minflip){
minflip = cnt;
for(int k = ; k < n; k++)
res[k] = ans[k];
}
}
}
if(!fg)cout<<"IMPOSSIBLE"<<endl;
else{
for(int j = ; j < n; j++){
for(int pos = ; pos < m; pos++)
if(pos == m-)cout<<(res[j]&(<<(m--pos))?:)<<endl;
else cout<<(res[j]&(<<(m--pos))?:)<<" ";
}
}
} int main()
{
while(cin>>n>>m)
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
cin>>grid[i][j];
solve();
} return ;
}
POJ3279(KB1-D 熄灯问题)的更多相关文章
- 熄灯问题 --POJ 2811-ACM
问题描述 盏灯的状态. 列的灯的状态就不改变. 请你写一个程序,确定需要按下哪些按钮,恰好使得所有的灯都熄灭.根据上面的规则,我们知道: 次按下时所产生的结果.因此,每个按钮最多只需要按下一次: (2 ...
- C++基础算法学习——熄灯问题
有一个由按钮组成的矩阵, 其中每行有6个按钮, 共5行– 每个按钮的位置上有一盏灯– 当按下一个按钮后, 该按钮以及周围位置(上边, 下边,左边, 右边)的灯都会改变状态26熄灯问题 POJ1222– ...
- 二进制枚举例题|poj1222,poj3279,poj1753
poj1222,poj3279,poj1753 听说还有 POJ1681-画家问题 POJ1166-拨钟问题 POJ1054-讨厌的青蛙
- poj3279(dfs+二进制枚举思路)
题意转载自https://www.cnblogs.com/blumia/p/poj3279.html 题目属性:DFS 相关题目:poj3276 题目原文:[desc]Farmer John know ...
- kb-01-d<poj3279>--深搜变种,二进制优化;
poj--3279 题意: 给n*m的矩阵,0 1组成,每次翻转一个格子可以将上下左右的五个节点翻转,求,把所有的格子翻转成0:输出每个个字的翻转次数:最少字数: 做法: 从上到下,第一行翻转的情况确 ...
- POJ1222、POJ3279、POJ1753--Flip
POJ1222-EXTENDED LIGHTS OUT POJ3279-Fliptile POJ1753-Flip Game 为什么将着三个题放一起讲呢?因为只要搞明白了其中一点,就可以一次3ac了- ...
- POJ3279 Fliptile(暴力)
有一种暴力是这样的,枚举一边,确定另一边. 这一题是这么解的,枚举第一行所有翻转情况,然后剩下几行其实是确定的,因为前i行翻转方式确定后只能通过第i+1行的翻转来改变第i行的状态,于是依次模拟求出剩下 ...
- [POJ3279]Fliptile(开关问题,枚举)
题目链接:http://poj.org/problem?id=3279 题解:http://www.cnblogs.com/helenawang/p/5538547.html /* ━━━━━┒ギリギ ...
- POJ3279 Fliptile 枚举+简单搜索
题意:一个矩阵,每个点1或0,然后每次翻一个点,它周围上下左右(包括自己)1->0,0->1,问最少翻几次可以矩阵全是0,忽略题目说的字典序 分析:枚举第一行所有的情况,然后下面几行也随之 ...
随机推荐
- 使用Navicat 创建mysql存储过程,实现日期加流水号序列
目的:使用Navicat 创建mysql存储过程,实现格式为8位日期(年月日)+5位流水号序列. 步骤: 1.打开Navicat 登录数据库,点击导航栏上的函数,如下图: 2.点击新建函数,选择“过程 ...
- 实现一个算法,寻找字符串中出现次数最少的、并且首次出现位置最前的字符 如"cbaacfdeaebb",符合要求的是"f",因为他只出现了一次(次数最少)。并且比其他只出现一次的字符(如"d")首次出现的位置最靠前。
实现一个算法,寻找字符串中出现次数最少的.并且首次出现位置最前的字符如"cbaacfdeaebb",符合要求的是"f",因为他只出现了一次(次数最少).并且比其 ...
- 系统可能不会保存你所做的修改 onbeforeunload
网上找了好多实现这个的方法,说的还是不明白.害得我(我自己的原因)以为是需要在服务器环境下才能跑通 window.onbeforeunload; 后来猜想是不是函数返回值发生变化就会触发. <! ...
- Flask从入门到精通之静态文件
Web 程序不是仅由Python 代码和模板组成.大多数程序还会使用静态文件,例如HTML代码中引用的图片.JavaScript 源码文件和CSS. 在前面的章节中,我们曾检查hello.py 程序的 ...
- javascript 实用工具函数
整理日常开发中我们常常会使用到的一些工具函数. var utils = (function(){ var fay = {}; // 返回当前时间的毫秒数 fay.getTime = Date.now( ...
- Nginx Web服务(一)
一.Nginx原理介绍 1.1:什么是Nginx Nginx是一个开源的,支持高性能.高并发的WWW服务和代理服务软件 1.2:Nginx的功能特点及应用场合 ① 支持高并发:能支持几万并发连接,特别 ...
- SS配置
{ "server":"0.0.0.0", "server_port":8388, "local_address": & ...
- (转)python的paramiko模块
python的paramiko模块 原文:http://www.cnblogs.com/breezey/p/6663546.html paramiko是用python语言写的一个模块,遵循S ...
- CGI PL PERL脚本 提权
windows 2003 下,安装ActivePerl-5.16.2.1602-MSWin32-x86-296513 IIS 添加CGI支持.并在对应网站配置下面,添加CGI后缀或PL后缀 与 对应的 ...
- android listview实现点击某个item后使其显示在屏幕顶端
在该listview的点击事件中加入一下代码即可 listView.setSelectionFromTop(position, 0);