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. Css中路径data用法

    Data URI scheme是在RFC2397中定义的,目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入. data:,文本数据 data:text/plain,文本数据 data: ...

  2. python处理数据(一)

    CSV数据处理 csv文件格式 逗号分隔符(csv),有时也称为字符分隔值,因为分隔字符也可以不是逗号,其文件以纯文本的形式存储表格数据(数字和文本).纯文本意味着该文件是一个字符序列,不含必须像二进 ...

  3. php 添加 redis 扩展

    Windows下PHP安装Redis扩展的具体步骤方法 下面我们就结合详细的图文,给大家介绍Windows下PHP安装Redis扩展的方法: 首先我们打开这个上面给出的下载链接地址,界面如下: 这里我 ...

  4. 《信息安全技术》实验一 PGP的原理与使用

    <信息安全技术>实验一 PGP的原理与使用(macOS High Sierra下实现) 实验目的 理解传统加密.公钥加密.混合加密.数字签名等概念 理解公钥.私钥.会话密钥.对称密钥等概念 ...

  5. ejs引擎项目

    关于这个我也很懵逼,写这篇博客就是想记录一下,有哪位大神看到之后可以略微指点一二,不胜感激....... 一.项目结构 db model user.js version.js schema xx.js ...

  6. CAN--UART的协议转换器

    CAN--UART的协议转换器 //------------------------------------------------------//  CAN <==> UART的协议转换 ...

  7. 小白第一次使用Git随笔

    想研究Git很久了,一直没有找到很好的博客或论坛,近几天工作项目任务没有那么重,就想着找几篇文章把这玩意儿给解决掉,本博客是记录读廖雪峰老师所写的<Git教程>的随笔,以便巩固学习,若想学 ...

  8. Angular7教程-01-Angular开发环境配置

    本教程基于angular7(2018-11-04) 1. 安装node.js 下载地址: http://nodejs.cn/download/ 下载对应自己操作系统的版本安装即可. 2.安装 angu ...

  9. Delphi在Android下使用Java库

    本文将以Android的USB串口通讯库为例,介绍Delphi如何在Android中使用Java的库. USB串口通讯库地址: https://github.com/felHR85/UsbSerial ...

  10. 评价指标1--F1值和MSE

    1,F1=2*(准确率*召回率)/(准确率+召回率) F1的值是精准率与召回率的调和平均数.F1的取值范围从0到1的数量越大,表明实现越理想. Precision(精准率)=TP/(TP+FP) Re ...