POJ 3279
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 3062 | Accepted: 1178 |
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
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
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX = ;
const int dx[] = {-,,,,};
const int dy[] = {,-,,,};
int N,M;
int tile[MAX][MAX];
int flip[MAX][MAX];
int opt[MAX][MAX]; int get(int x,int y) {
int c = tile[x][y];
for(int d = ; d < ; ++d) {
int x1 = x + dx[d],y1 = y + dy[d];
if(x1 >= && x1 <= N && y1 >= && y1 < M) {
c += flip[x1][y1];
}
}
return c % ; }
int cal() {
int res = ;
int t[MAX * MAX + ];
for(int i = ; i <= N; ++i) {
for(int j = ; j < M; ++j) {
if(get(i - ,j)) {
flip[i][j] = ;
}
}
} for(int i = ; i < M; ++i) {
if(get(N,i)) return -;
} for(int i = ; i <= N; ++i) {
for(int j = ; j < M; ++j) {
res += flip[i][j];
}
} return res; } void solve() {
int res = -;
for(int s = ; s < ( << M); ++s) {
memset(flip,,sizeof(flip));
for(int i = ; i < M; ++i) {
if(s & ( << i)) flip[][i] = ;
}
int num = cal();
//printf("num = %d\n",num);
if(num >= && (res < || res > num)) {
res = num;
memcpy(opt,flip,sizeof(flip));
} } if(res < ) {
printf("IMPOSSIBLE\n");
} else {
for(int i = ; i <= N; ++i) {
for(int j = ; j < M; ++j) {
printf("%d",opt[i][j]);
if(j != M - ) printf(" "); }
printf("\n");
}
}
} int main()
{
// freopen("sw.in","r",stdin);
scanf("%d%d",&N,&M);
for(int i = ; i <= N; ++i) {
for(int j = ; j < M; ++j) {
scanf("%d",&tile[i][j]);
//printf("%d ",tile[i][j]);
}
//printf("\n");
} solve();
//cout << "Hello world!" << endl;
return ;
}
POJ 3279的更多相关文章
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 【POJ 3279 Fliptile】开关问题,模拟
题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...
- POJ 3279 Fliptile[二进制状压DP]
题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...
- 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 ...
- POJ - 3279 Fliptile (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
- poj 3279 Fliptile(二进制)
http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
随机推荐
- 完全面向于初学者的Node.js指南
新的上班时间是周二至周六,工作之余当然要坚持学习啦. 希望这篇文章能解决你这样一个问题:“我现在已经下载好Node.Js了,该做些什么呢?” 原文URL:http://blog.modulus.io/ ...
- C 网页压力测试器
引言 <<独白>> 席慕蓉 节选一 把向你借来的笔还给你吧. 一切都发生在回首的刹那. 我的彻悟如果是缘自一种迷乱,那么,我的种种迷乱不也就只是因为一种彻悟? 在一回首间,才忽 ...
- MIFARE系列6《射频卡与读写器的通讯》
1. 复位应答(Answer to request) 读写器呼叫磁场内的卡片,卡片对呼叫做出应答.对刚进入磁场得到电复位处于休闲状态的卡片,卡请求(REQA,0x26):对于已进行过读写操作并进入休眠 ...
- 透过数据看现实,漫谈实况FIFA的这些年
虽然,只是个普通玩家,虽然带了一点青春,一点爱.虽然,有那么些怀念 ~ 好吧,不浪费篇幅伪伪的煽情,直插主题.(很长且多图,更多讲述的是实况FIFA间的你来我往,互相赶超的故事.本想全面展开描述细节, ...
- WdatePicker 动态变量表
4. 日期范围限制静态限制 注意:日期格式必须与 realDateFmt 和 realTimeFmt 一致 你可以给通过配置minDate(最小日期),maxDate(最大日期)为静态日期值,来限定日 ...
- Go字符串函数
下面的代码中,列出了Go官方包中常见的字符串函数. package main import s "strings" import "fmt" //为打印函数起个 ...
- Machine Learning 学习笔记 (2) —— 使用牛顿法寻找极值
本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 用牛顿法解方程 牛顿法是一种 ...
- @synthesize 有什么好处?
如果不用 synthesize,操作的是 @property中定义的变量,使用synthesize之后,间接的操作了一个新的成员变量,到底有什么好处?直接只用一个@property不是更简单吗?
- 【每日scrum】NO.5
尝试采用自己的地图,绘点并计算路径,但是地图打开出现问题.
- 安装配置tomcat
1.安装nginx 下载nginx-1.4.3 解压: tar zxvf nginx-1.4.3.tar.gz 编译安装: ./configure --prefix=/app/act/nginx/ng ...