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 ...
随机推荐
- Java 8——Optional
本文主要介绍Java 8的 Optional 的简单使用 Address 1 2 3 4 5 6 7 @Data @AllArgsConstructor @NoArgsConstructor publ ...
- characterEncodingFilter作用
package com.demo.test; import java.io.IOException; import javax.servlet.Filter; import javax.servlet ...
- 提高java编程质量 - (四)i++ 和 ++i 探究原理
先看一个例子: package com.test; public class AutoIncrement { public static void main(String[] args) { int ...
- 浅论ajax跨域!从一个例子开始!
//所谓跨域,简单来说就是去访问不是自己域名下的数据 <!DOCTYPE html> <html lang="en"> <head> <m ...
- angular.js的ng-app 指令定义一个 AngularJS 应用程序。
<!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="UT ...
- PHP:win7 ASP.NET环境与PHP(WAMP)环境如何共存
经验地址:http://jingyan.baidu.com/article/495ba8410f794d38b30ede89.html 笔记本以前安装过asp.net,启用了Windows的IIS服务 ...
- Disruptor——一种可替代有界队列完成并发线程间数据交换的高性能解决方案
本文翻译自LMAX关于Disruptor的论文,同时加上一些自己的理解和标注.Disruptor是一个高效的线程间交换数据的基础组件,它使用栅栏(barrier)+序号(Sequencing)机制协调 ...
- java 多线程访问同一个对象数据保护的问题
java 多线程同时访问统一个数据的时候,会引起一些错误,后面的线程会修改数据,而前面的线程还在使用修改前的内容, 使用 synchronized 关键字,保证代码块只能有一个线程来访问 public ...
- test_markdown
add modifications 非科学计数法显示数字 citation[^ref1] format bank% do not use scientific expression format lo ...
- @JsonIgnoreProperties忽略转换到json的属性
bean转换到json忽略指定属性 @JsonIgnoreProperties(value={"attrName"})