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. 移动端页面以rem为单位设置字体大小不生效解决方法

    这个问题在前端H5页面开发可以说是一个老生常谈的问题了.由于以前所有遇到的问题以及解决方法都会以文档的形式记录在电脑里,而非github或者blog,所以现在才一点一滴的整理上来,就当是一个心路历程吧 ...

  2. 用Python玩微信(非常详细)

    代码放在这里:wzyonggege/python-wechat-itchat 词云那里可以换成小黄人图片 ----------------------------------------------- ...

  3. mysql安装报错

    一: -- MySQL 5.5.22Warning: Bison executable not found in PATH-- Configuring done-- Generating done-- ...

  4. MySql的学习笔记

    良好的理解sql语句: 列:理解可以运算的成变量 where: 理解成表达式,放在行中看是否成立 查出来的结果可以当成一张表理解,select 套用select综合查询:   五种查询 where g ...

  5. 怎么一次性获取form所有的值?rerialize() 、 serializeArray()方法的使用

    from直接提交当然方便,但是有时候需要对数据进行处理再用ajax提交,挨个去获取值未免太麻烦,讲两个一次性获取所有值的方法. 方法1 $("form").serialize(); ...

  6. [IR] BWT+MTF+AC

    BWT (Burrows–Wheeler_transform)数据转换算法 MTF(Move-to-front transform)数据转换 基于统计的压缩算法:游程编码 良心PPT: bwt_bas ...

  7. 最近一个刚刚毕业的朋友说,他面试时候,遇到最频繁的css问题就是垂直居中,这里给出几种垂直居中方式!

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Bash中的数学扩展

    Bash只支持整数运算,不支持浮点运算.如果需要进行浮点运算,需要使用bc程序.Bash中的数学扩展有两种形式:$[ expression ]或$(( expression )) 例子:$echo $ ...

  9. fpm 制作 rpm 包

    支持的 源类型包 ① dir : 将目录打包成所需要的类型, 可用于源码编译安装软件包 ② rpm : 对 rpm 包进行转换 ③ gem : 对 rubygem 包进行转换 ④ python : 将 ...

  10. Java代码实现 增删查 + 分页——实习第四天

    今天项目内容已经开始了,并且已经完成好多基本操作,今天就开始总结今天学习到的内容,和我遇到的问题,以及分析这其中的原因. 内容模块: 1:Java代码实现对数据库的增删查: 2:分页且获取页面信息: ...