Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10931   Accepted: 4029

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

Source

思路:1.先枚举第一行的翻转情况,然后相邻的下一行的翻转情况就已经确定,因为这时只有下一行的翻转能改变当前行的状态,这样每个状态只由第一行确定。

2.要保证字典序最小,只需使第一行的字典序最小。

代码:

 #include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = ;
const int dx[] = {-, , , , };
const int dy[] = { ,-, , , }; int s[N][N], st[N][N], tmp[N][N], rec[N][N];
int n, m, ans;
void flip(int x, int y) {
tmp[x][y] = ;
int nx, ny;
for(int i = ; i < ; i++) {
nx = x+dx[i];
ny = y+dy[i];
st[nx][ny] = !st[nx][ny];
}
}
bool ok() {
for(int j = ; j <= m; j++) {
if(st[n][j]) return ;
}
return ;
}
void f(int t) {
memcpy(st, s, sizeof(s));
memset(tmp, , sizeof(tmp));
int cnt = ;
for(int j = ; j < m; j++) {
if((t>>j) & ) {//从第一行的二进制第一位开始枚举翻转状态。
flip(, j+);
cnt++;
}
}
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(st[i-][j] == ) {
flip(i, j);
cnt++;
}
}
}
if(ok() && cnt < ans) {
ans = cnt;
memcpy(rec, tmp, sizeof(tmp));
}
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%d", &s[i][j]);
}
}
ans = inf;
int end = << m;
for(int t = ; t < end; t++) f(t);
if(ans == inf) puts("IMPOSSIBLE");
else {
for(int i = ; i <= n; i++) {
printf("%d", rec[i][]);
for(int j = ; j <= m; j++)
printf(" %d", rec[i][j]);
puts("");
}
}
}
return ;
}

POJ 3279 枚举(思维)的更多相关文章

  1. POJ - 3279 枚举 [kuangbin带你飞]专题一

    这题很经典啊,以前也遇到过类似的题--计蒜客 硬币翻转. 不过这题不仅要求翻转次数最少,且翻转方案的字典序也要最小. 解法:二进制枚举第一行的翻转方案,然后处理第二行,如果第二行的k列的上一列是黑色, ...

  2. POJ - 1222 / POJ - 3279 枚举第一行

    说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...

  3. POJ 3279 枚举?

    思路: 1.枚举第一行 递推剩下的 判断最后一行成不成立 2. (误)高斯消元? 如何判断1最少和字典序最小- (所以这种做法好像不可取) //By SiriusRen #include <cs ...

  4. 【枚举】POJ 3279

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

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

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

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

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

  7. POJ 3279(Fliptile)题解

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

  8. POJ 3279 Fliptile(翻格子)

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

  9. POJ 3279 - Fliptile - [状压+暴力枚举]

    题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...

随机推荐

  1. 一个html页面传入参数到另一个html页面用js获取方法

    没错使用以下函数就能够完整的获取到路径里的你想要的参数:function getURLParameter(name) { return decodeURIComponent((new RegExp(' ...

  2. 使用 zabbix 自动发现监控 MySQL

    介绍 使用 zabbix 的 low-level 自动发现功能完成单主机多端口的监控, 详见low_level_discovery, 整体上监控类似 percona 的 zabbix 监控插件, 不过 ...

  3. Eclipse设置文字大小

    1,选择窗口,preference 2,general

  4. 软件raid 5

    软件raid 5的实现 RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保 ...

  5. 封装TableView有可能用到的数据结构和UITableViewCell的一个继承类

    最近4年的时间,我已经做了5个App完全独立开发, 工作经历5个App, 维护了两个App. 在这期间用的最多的是UITableView, 因此也有许多感觉可以封装的. 现在就是我封装的. RXCel ...

  6. Chapter 7:Statistical-Model-Based Methods

    作者:桂. 时间:2017-05-25  10:14:21 主要是<Speech enhancement: theory and practice>的读书笔记,全部内容可以点击这里. 书中 ...

  7. javascript基础数据类型与引用类型

    javascript一共有6种数据类型 有5种基本类型:Null,String Number,Boolean,Undefined 和一种引用类型Object 基础类型在内存中存在于栈空间中,例如 va ...

  8. 学习mysql语法--基础篇(一)

      前  言  mysql  mysql语法--本篇学习都是通过使用Navicat Premium(数据库管理工具),连接mysql数据. 本篇学习主要有两个部分:    一.创建用户,创建数据库,给 ...

  9. pythonic-让python代码更高效

    何为pythonic? pythonic如果翻译成中文的话就是很python.很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等. 我的理解为,很+名词表达了一种特殊和强调的意味.所以很 ...

  10. Android 音乐播放

    android简单音乐播放控制代码 这个几个月业余时间一直在做一个android项目,里面涉及到了音乐播放功能.很简单那种,播放.暂停.上一曲.下一曲.音量调节等. 音乐播放主要使用的对象是Media ...