Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19730   Accepted: 7118

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

Line 1: Two space-separated integers: M and N 
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

Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

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 思路:
因为求解的是最小按键次数,如果将所有都遍历的话,时间复杂度为 2^(N*M)太大了,因为开关按下,上下左右和它自己都会变化,所以,让第一行出现2^m次不同的情况,用二进制表示

也就是for(int i=0;i< 1<<m; i++)种不同情况, 然后将i转化为二进制。这样就可以代表具体开关的地方,第二行以及以后就可以根据第一行进行遍历了。因为第一行已经确定了所以,第二行就专门去寻找第一行还没有关的灯,比如:map[i][j] = 1,就按下a[i+1][j]处的开关保证i+1行开关按后,第i行的灯全部关闭为0 最后判断这种情况是否正确,然后判断是否最小,记录下来输出。

#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int map[][],mm[][];
int a[][],A[][];
int d[][] = {,,,,-,,,,,-}; int get_s(int x, int y){
if(map[x][y]==) return ;
else return ;
}
int main(){
int n,m;
cin>>n>>m;
memset(map,,sizeof(map));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>map[i][j];
}
}
memcpy(mm,map,sizeof(map)); int minn = 1e9;
for( int i = ; i < <<m; i++ ) {
int count=,flag = ;
memset(a,,sizeof(a));
memcpy(map,mm,sizeof(mm));
//第一行解决 将 i 转化为 二进制
for(int j=;j<=m;j++){
a[][j] = (i>>(j-))&;
if( a[][j] == ){
count++;
map[][j] = get_s(,j);
map[][j-] = get_s(,j-);
map[][j+] = get_s(,j+);
map[][j] = get_s(,j);
}
} for( int j = ; j < n; j++ ) {
for( int k = ; k <= m; k++ ) {
if( map[j][k] == ) {
a[j+][k] = ;
int dx = j+, dy = k, tx, ty;
count++;
for( int z = ; z < ; z++ ) {
tx = dx + d[z][];
ty = dy + d[z][];
// cout<<tx<<" "<<ty<<endl;
map[tx][ty] = get_s(tx,ty);
}
// cout<<map[j][k]<<" "<<" j = "<<j<<" k = "<<k<<endl;
} }
}
for( int j = ; j <= m; j++ ) {
if(map[n][j]==) flag = ;
} if( flag ) continue;
else {
if( count < minn ) {
minn = count;
memcpy(A,a,sizeof(a));
}
} }
if(minn == 1e9) printf("IMPOSSIBLE\n");
else{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
printf("%d%c",A[i][j],j==m?'\n':' ');
}
return ;
}

POJ--3279(开关问题2个不同时间写的代码)的更多相关文章

  1. poj 3279(开关问题)(待完成)

    传送门:Problem 3279 #include<iostream> #include<cstdio> #include<cstring> using names ...

  2. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  3. 【枚举】POJ 3279

    直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...

  4. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  5. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  6. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  7. 挤点时间写博客-php&MySQL实践

    hi 晚上要吃火锅的嘛,挤点时间写点东西吧,别被老板发现哦 1.PHP与MySQL 五.文章发布系统之后台 5.2 创建配置文件和初始化文件 为了统一配置以及管理方便,还有就是减少代码的冗余. 分别为 ...

  8. 基于jQuery发展历程时间轴特效代码

    分享一款基于jQuery发展历程时间轴特效代码,带左右箭头,数字时间轴选项卡切换特效下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id="time ...

  9. html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件

    html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件

随机推荐

  1. 微信小程序 置顶/取消置顶

    wxml <view wx:for="{{confirmlist}}" wx:for-item="confirm" wx:for-index=" ...

  2. virtualbox+vagrant学习-2(command cli)-21-vagrant up命令

    Up 格式: vagrant up [options] [name|id] 这个命令根据你的Vagrantfile文件创建和配置客户机. 这是“vagrant”中最重要的一个命令,因为它是创建任何va ...

  3. 最易懂的layui分页

    该篇文章是在layui前端框架之分页基础上简洁化和详细化. 首先该示例采用的是Spring+MyBatis Plus+SpringMVC(常规的SSM框架),持久层换成MyBatis也行. 至于lay ...

  4. html手机网页自适应宽度

    #在head之间加如下代码即可 <meta name="viewport" content="width=device-width, initial-scale=1 ...

  5. 关于2.4G芯片中 CC2500的相关资料

    CC2500芯片,是TI(原Chipcon被TI收购)推出的一款超低功耗.低成本的无线收发模块,其载频范围在2.400GHz-2.483GHz内可调,可用来实现多信道通信.它支持多种调制方式,包括FS ...

  6. DRCNN超分辨重建2016年

    论文疑点: Embedding层是怎么操作的? https://gshtime.github.io/2018/06/01/tensorflow-embedding-lookup-sparse/ 这篇文 ...

  7. Mysql安装(win10 64位)

    公司的测试数据库只有读的权限,而且还不能用IP和端口去访问,所有很多时候不方便(尤其是想练手的时候).闲着也是闲着,自己搭建一个Mysql数据库出来.以下操作,全部基于win10专业版 64位,仅供参 ...

  8. [iOS]异常捕捉

    UncaughtExceptionHandler.h #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interfac ...

  9. Python 包、模块、函数、变量作用域

    Python 项目的组织结构 - 包 -- 模块 --- 类 ---- 函数.变量   Python是利用包和模块来组织一个项目的.   包: 包的物理表现是一个文件夹,但是一个文件夹却不一定是个包, ...

  10. UOJ#34. 多项式乘法(NTT)

    这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项 ...