POJ 3279 枚举(思维)
|
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 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 Sample Output 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 枚举(思维)的更多相关文章
- POJ - 3279 枚举 [kuangbin带你飞]专题一
这题很经典啊,以前也遇到过类似的题--计蒜客 硬币翻转. 不过这题不仅要求翻转次数最少,且翻转方案的字典序也要最小. 解法:二进制枚举第一行的翻转方案,然后处理第二行,如果第二行的k列的上一列是黑色, ...
- POJ - 1222 / POJ - 3279 枚举第一行
说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...
- POJ 3279 枚举?
思路: 1.枚举第一行 递推剩下的 判断最后一行成不成立 2. (误)高斯消元? 如何判断1最少和字典序最小- (所以这种做法好像不可取) //By SiriusRen #include <cs ...
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- 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 ...
随机推荐
- 3分钟带你了解PowerShell发展历程——PowerShell各版本资料整理
本文带你了解PowerShell发展历程,顺便整理了一点资料,方便大家查询. Windows PowerShell® 是基于任务的命令行管理程序和脚本语言,专为进行系统管理而设计. 在 .NET Fr ...
- Java之反射--练习
定义Student 类:包含:姓名和年龄等属性,有参和无参构造方法,输出所有信息的方法 1.使用多种方法生成一个Student类的Class对象 2.使用Class类获取Student类的结构信息并输 ...
- cpp(第八章)
1. #include <iostream> inline int add(int &n) { n= n+; ; } int main() { using namespace st ...
- sublime text 快捷收集
1. 文件快速导航: 这是sublime上面很好用的功能之一,ctrl+p可以调出窗口,菜单上的解释是gotoanythings ,确实如其所言,调出窗口后,直接输入关键字,可以在已打开的项目文件夹中 ...
- PHP中的面向对象OOP中的魔术方法
一.什么是魔术方法: PHP为我们提供了一系列用__开头的函数,这些函数无需自己手动调用,会在合适的时机自动调用,这类函数称为魔术函数.例如: function __construct(){} 在ne ...
- (转)关于BigDecimal 转化字符串toPlainString()和toString()的区别
对于 BigDecimal b ; (b=(0.4321)^ 20)String s = b.toPlainString() ;System.out.println(s) ; 输出为:0.0 ...
- python爬虫 模拟登陆校园网-初级
最近跟同学学习爬虫的时候看到网上有个帖子,好像是山大校园网不稳定,用py做了个模拟登陆很有趣,于是我走上了一条不归路..... 先上一张校园网截图 首先弄清一下模拟登陆的原理: 1:服务器判定浏览器登 ...
- CSS中可以通过哪些属性定义,使得一个DOM元素不显示在浏览器可视范围内?
最基本的: 设置display属性为none,或者设置visibility属性为hidden 技巧性: 设置宽高为0,设置透明度为0,设置z-index位置在-1000
- CSS技巧和经验列表
如何清除图片下方出现几像素的空白间隙? img{display:block;} 如何让文本垂直对齐文本输入框? input{vertical-align:middle;} 如何使文本溢出边界显示为省略 ...
- 使用c#解析json库
写了个c#版的json解析库,提供了json到hashtable以及hashtable到json字符串的转换 受惠于c#的语法特性,hashtable到json的解析变得非常简单 先判断传入的obje ...