POJ:3279-Fliptile(矩阵反转)
Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14701 Accepted: 5381
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
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
解题心得:
- 题意就是给你一个矩阵,你可以选择一个里面的数进行0-1反转,在反转的同时,和这个数相邻的四个方向(上下左右)的数也会跟着0-1反转,现在叫你输出一个选择数反转的方案出来,要求反转次数最少。
- 如果进行枚举,可能性为2^(n*m),太大了。可以明确的一点是,反转的顺序对于结果是没有影响的,有影响的只是反转的次数。所以如果我们规定一个从上到下的反转顺序,那么第一排如果能够确定,根据第一排的结果第二排也能确定,就可以得到答案了。但是如何确定第一排呢,只有枚举,将第一排所有的情况枚举出来,然后在第一排已知的情况下向下推,去计算整个矩阵。复杂度为O(n*m(2^n)),这个复杂度是可以接受的。
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <climits>
using namespace std;
const int maxn = 20;
int dir[5][2] = {1,0,0,1,0,0,-1,0,0,-1};
int flip[maxn][maxn],opt[maxn][maxn];//opt用来存放最后反转的操作,flip用来存放当前的反转操作
int tile[maxn][maxn],n,m;//
void init() {//tile用来储存原矩阵
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
scanf("%d",&tile[i][j]);
}
bool check(int x,int y) {//检查是否超出了矩阵的范围
if(x < 0 || y < 0 || x >= m || y >= n)
return true;
return false;
}
bool get(int x,int y) {//检验同一列上一行的这个位置是否需要反转
int cnt = tile[x][y];
for(int i=0;i<5;i++) {
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(check(xx,yy))
continue;
cnt += flip[xx][yy];
}
if(cnt%2)
return true;
return false;
}
int cal() {
int cnt = 0;
for(int i=1;i<m;i++) {
for(int j=0;j<n;j++) {
if(get(i-1,j))
flip[i][j] = 1;//如果需要反转做好标记
}
}
for(int i=0;i<n;i++) {
if(get(m-1,i))//检验最后一行是否符合条件
return -1;
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) {
if(flip[i][j])
cnt++;
}
return cnt;
}
int solve() {
int res = INT_MAX;
for(int i=0;i<(1<<n);i++) {//枚举第一行
memset(flip,0,sizeof(flip));
for(int j=n-1;j>=0;j--)
flip[0][j] = (i >> j) & 1;
int num = cal();
if(num > 0 && res > num) {
res = num;
memcpy(opt,flip,sizeof(flip));
}
}
return res;
}
void Print() {
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) {
printf("%d%c",opt[i][j],j == n-1 ? '\n' : ' ');
}
}
int main() {
init();
int ans = solve();
if(ans == INT_MAX) {
printf("IMPOSSIBLE\n");
return 0;
}
Print();
return 0;
}
POJ:3279-Fliptile(矩阵反转)的更多相关文章
- POJ 3279 Fliptile(反转 +二进制枚举)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13631 Accepted: 5027 Descrip ...
- POJ - 3279 Fliptile(反转---开关问题)
题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 1.枚举第一行 ...
- 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)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(DFS+反转)
题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子, ...
- poj 3279 Fliptile (简单搜索)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16558 Accepted: 6056 Descrip ...
- 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的,最小需要点击多少步,并输出最 ...
随机推荐
- 浅谈JavaScript原型
在JavaScript中,所有函数都会拥有一个叫做prototype的属性,默认初始值为“空”对象(没有自身属性的对象). 1.原型属性 如下所示,简单地定义一个函数: function foo(a, ...
- 减少服务器压力php生成静态xml文件
一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...
- css随堂笔记(一)
Css初体验第一天 1 css初识:css主要用于设置HTML页面中文本内容,图片的外形,以及版面的布局等外观显示样式 Css样式规范:h1{属性:值} 2 css的三总书写方式:1 行内样式 将样 ...
- php的yii框架开发总结4
用户验证的实现:/protected/components/UserIdentity.php 修改:function authenticate()函数中的语句 public function auth ...
- window下mycat要放在根目录下
原理文档没有跟我开玩笑呢? 建议放在盘符根目录下 ,无视的我,检查了多遍jdk环境,检查了多遍bat文件内容,仍然没有解决 找不到或无法加载主类 直到我乖乖的把目录放跟盘符才解决,心好累= =! 可能 ...
- 触发transition的几种方式--转
鼠标单击 获取焦点 或元素发生任何改变,怎么说呢,目前的理解是,元素发生了什么改变,使得它跟以前不一样了.比如同样是p元素,先有一个样式.后来这个p被hover了.被focus了.或者通过另外一条途径 ...
- Linux MySQL单实例源码编译安装5.6
cmake软件 tar -zxvf cmake-2.8.11.2.tar.gz cd cmake-2.8.11.2 ./bootstrap make make install cd ../ 依赖包 ...
- BestCoder Round #91 1001 Lotus and Characters
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意: Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的 ...
- 起一个node服务
使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...
- 【转】NodeJS教程--基于ExpressJS框架的文件上传
本文是翻译的一篇文章,原文地址:Handle File Uploads in Express (Node.js). 在NodeJS发展早期上传文件是一个较难操作的功能,随后出现了formidable. ...