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代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
随机推荐
- C基础 北京大公司面试简单总结
作者有话说 这是关于程序员面试的一篇文章, 希望对你有帮助. 干了快3年了. 可以简单参考, 对比总结.虽然本人很水. 很喜欢当前做的手游项目.做的很认真.后端每个人技术都很好.但是结果都不如意.在死 ...
- 【转】C# 子窗体如何调用父窗体的方法
网络上有几种方法,先总结如下: 调用窗体(父):FormFather,被调用窗体(子):FormSub. 方法1: 所有权法 //FormFather: //需要有一个公共的 ...
- linux系统设置-防火墙
基础知识 Linux系统内核内建了netfilter防火墙机制.Netfilter(数据包过滤机制),所谓的数据包过滤,就是分析进入主机的网络数据包,将数据包的头部数据提取出来进行分析,以决该连接为放 ...
- oracle group 语句探究(笔记)
1.group by语句在oracle中没有排序功能,必须依靠order by才能实现按照预定结果的排序 2.group by 的cube扩展 with test as ( id, name from ...
- Redbean:入门(四) - 反射机制 以及 事务
<?php //引入rb入口文件 include_once 'rb.php'; //定义dsn以及相关的数据 $dsn = 'mysql:host=localhost;dbname=hwibs_ ...
- 九度oj 1525 子串逆序打印
原题链接:http://ac.jobdu.com/problem.php?pid=1525 字符串简单题,注意开有结尾有空格的情况否则pe or wa #include<algorithm> ...
- Golang之ring.Ring的Link操作
ring.Ring 是一个环链. 其Link操作规则是: r.Link(s) => t = r.Next r.Next.Prev = s.Prev s.Prev.Next = r.Next r. ...
- Your First ASP.NET 5 Application on a Mac
Your First ASP.NET 5 Application on a Mac By Daniel Roth, Steve Smith, Rick Anderson ASP.NET 5 is cr ...
- MySQL命令输入错误 取消命令
mysql命令行输入错误字符需要取消执行时,可在分号“:”出现前输入“\c”来取消该条命令,注意c前边的“\”. 1 2 3 4 mysql> select -> user() -&g ...
- [转]KDE/QT与GNOME/GTK比较
[转]KDE/QT与GNOME/GTK比较 http://www.cnblogs.com/itech/archive/2009/08/18/1548964.html 虽然在商业方面存在竞争,GNOME ...