Fliptile
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

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

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的更多相关文章

  1. 【枚举】POJ 3279

    直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...

  2. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  3. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  4. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  5. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  6. 【POJ 3279 Fliptile】开关问题,模拟

    题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...

  7. POJ 3279 Fliptile[二进制状压DP]

    题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...

  8. 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 ...

  9. POJ - 3279 Fliptile (枚举)

    http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...

  10. poj 3279 Fliptile(二进制)

    http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...

随机推荐

  1. Android请求返回417解决办法

    今天碰到个很奇怪的问题,APP通过代理链接服务器会收到HTTP 417错误,经过网上查找发现是由于以下代码造成: HttpParams params = new BasicHttpParams(); ...

  2. Linux 常见的进程调度算法

    1.在介绍进程调度之前,先对进程的状态的概念应该有所了解,下面是关于进程状态的一些基本概念:进程的状态分为三种,分别为: 1).运行态:该状态表明进程在实际占用CPU 2).就绪态: 该状态下进程可以 ...

  3. 多路转接之poll和select

    先看poll(): #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  4. 在SQL Server 2012中新建用户

    一.问题描述 在最开始装SQL Server 2012时我选择的是Windows身份认证方式,现在想添加一个用户采用SQL Server身份验证. 二.具体思路 1.新建用户 2.将新建的用户添加到相 ...

  5. 我爱我家:我为什么选择AppCan?

    10年前,说起手机,大家联想到的词大概是:电话.短信.QQ.拍照,以及贪吃蛇等有限的几个小游戏.而如今,手机毫无疑问已经成为人们生活中不可或缺的部分.这是一个神奇的东西:通讯工具,外卖神器,游戏机,移 ...

  6. android开发系列之回调函数

    想必对于回调函数大家肯定不陌生,因为这是我们开发里面常用的代码技巧.我也就不废话了,让我们直接来看代码吧! public class TestCallback { public interface I ...

  7. 将meteor部署在自己服务器上的简易方法

    有meteor-up等众多工具,如果你不喜欢它们,可以尝试如下方法,自由控制. 1,创建及打包项目 meteor create newapp meteor build . 2,上传 将bunder.t ...

  8. 面向对象原生js幻灯片代淡出效果

    面向对象原生js幻灯片代淡出效果 下面是代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  9. exception -----> Functions

    /* current_exception */ exception_ptr current_exception() noexcept; 返回指向当前异常(或其副本)的智能指针[具体返回对象本身还是副本 ...

  10. 0-N背包为题(动态规划算法)

    /****************0-N背包问题****************** * 有n个物体装入容量为c的背包,每一个物体有一个体积 * 和一个价值,所装入的物体体积之和不大于背包体积, * ...