状态压缩+枚举 POJ 3279 Fliptile
/*
题意:问最少翻转几次使得棋子都变白,输出翻转的位置
状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
int ans[MAXN][MAXN];
int dx[] = {-, , , , };
int dy[] = {, , , -, };
int n, m; int get_col(int x, int y) {
int ret = a[x][y];
for (int i=; i<; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < || tx >= n || ty < || ty >= m) continue;
ret += b[tx][ty];
}
return ret & ;
} int check(int s) {
memset (b, , sizeof (b));
for (int i=; i<m; ++i) {
if (s & ( << i)) b[][i] = ;
}
for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) {
if (get_col (i-, j)) b[i][j] = ;
}
}
for (int i=; i<m; ++i) if (get_col (n-, i)) return INF;
int ret = ;
for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) ret += b[i][j];
}
return ret;
} int main(void) { //POJ 3279 Fliptile
while (scanf ("%d%d", &n, &m) == ) {
for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) {
scanf ("%d", &a[i][j]);
}
}
int mn = INF;
memset (ans, , sizeof (ans));
for (int i=; i<(<<m); ++i) {
int res = check (i);
if (res < mn) {
mn = res;
for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) {
if (b[i][j]) ans[i][j] = ;
else ans[i][j] = ;
}
}
}
}
if (mn < INF) {
for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) printf ("%d%c", ans[i][j], (j == m-) ? '\n' : ' ');
}
}
else puts ("IMPOSSIBLE");
} return ;
}
状态压缩+枚举 POJ 3279 Fliptile的更多相关文章
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举
题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...
- POJ - 3279 Fliptile (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
随机推荐
- 爬虫(1):requests模块
requests介绍: reqeusts模块:python原生一个基于网络请求的模块,模拟浏览器发起请求. requests模块的优点: - 1.自动处理url编码 - 2.自动处理post请求的参数 ...
- [K/3Cloud]将JSON字符串反序列化为C#动态对象
using Kingdee.BOS.Util; string errString="{/"Row/":1,/"PageId/":/"1234 ...
- Form表单的action和onSubmit示例介绍
action是form的属性,onSubmit为事件,要说执行的先后顺序,个人理解是onSubmit在先. 第一:action是form的属性,html5已经将其定义为必需的属性值,onSubmit为 ...
- operamasks—omMessageTip的使用
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- ecstore
- 改變iTunes備份路徑
*** 在任意分區建立文件夾用來移動iTunes原有備份 *** 例如:move"c:\user\vhd\appdata\roaming\apple computer"全部到&qu ...
- [Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller
In Spring boot, define a REST API endpoint is pretty easy. package com.globomatisc.bike.controllers; ...
- 【Record】ART:Android RunTime
资料来自url=9xdxrhR45Uj3p450JQvTUO-dmzcWswNmABVgYAaFS0AXYDi8Q2JOzvu7y33GIOAI_8Lz7JmLrl0x6DoRW8e5oa" ...
- 12、Cocos2dx 3.0游戏开发找小三之3.0中的生命周期分析
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27706303 生命周期分析 在前面文章中我们执行了第 ...