Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14297   Accepted: 5257

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

 
时隔一年,终于又开始A题了。。。
又要重新拾起来,好多东西都忘了。。。
 
题意:有一个m*n的棋盘,每一个棋子都是一面黑色(1表示),一面白色(0表示),每次翻一颗棋子时,他的上下左右四个也随着翻过来,目标是翻成全是白色的,问用最少的次数达到目标时,都是要翻哪些棋子。
题目归到了搜索里面,想了好久也没想出来。
 
因为棋子只有黑色和 白色,所以翻一次时会变成另一个颜色,翻两次就变回原来的颜色了,所以每个棋子都可以看作只有翻和不翻两种状态。
最暴力的想法是枚举m*n个棋子的状态,每一个棋子都是有两种状态,时间复杂度就是2^(m*n),这样太大了,肯定不行。
看了几个题解,都是写了思路,但是具体为什么这样做好像解释的不太清楚。
思路:因为翻每一个棋子,都会导致他的上下左右的棋子变化,如果想让1变成0,可以翻他的旁边的棋子来达到目的,(统一按照翻它下方的棋子),如果从第一行往下赶,赶到最后一行,上面的m-1行肯定都是0了,所以只需要解决最后一行就行了,而最后一行最多15位,枚举就行了,时间复杂度是2^15。
枚举是借助二进制,00001代表只翻最后一个,+1之后是00010,代表翻倒数第二个。。。
我写的代码是先枚举第一行,然后往下赶,再看最后一行是不是全是0.
 
 
 
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int m,n; bool right(int i,int j){
if(i>=&&i<m&&j>=&&j<n){
return true;
}else{
return false;
}
} void flip(char grid[][],int i,int j){
int ii=i,jj=j; if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
} ii=i+,jj=j;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i-,jj=j;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i,jj=j+;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i,jj=j-;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} } int main()
{
char reserve[][];
char grid[][];
char result[][];
char output[][];
int bin[];
scanf("%d %d",&m,&n);
getchar();
for(int i=;i<m;i++){
for(int j=;j<n;j++){
scanf("%c",&reserve[i][j]);
getchar();
}
}
int coun=<<n; int mincou=;
for(int iii=;iii<coun;iii++){ memcpy(grid,reserve,sizeof(reserve));
memset(result,'',sizeof(result)); int cou=;
int t=iii;
int b_i=;
bin[]=; while(t){
bin[b_i]=t%;
t/=;
result[][b_i]=''+bin[b_i];
b_i++;
}
for(int i=;i<n;i++){
if(result[][i]==''){
cou++;
flip(grid,,i);
}
} for(int i=;i<m-;i++){
for(int j=;j<n;j++){
if(grid[i][j]==''){
flip(grid,i+,j);
result[i+][j]='';
cou++;
}
}
}
bool f=true;
for(int i=;i<n;i++){
if(grid[m-][i]==''){
f=false;
break;
}
}
if(f==true){
if(cou<mincou){
mincou=cou;
memcpy(output,result,sizeof(result));
}else if(cou==mincou){
bool flag=true;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(output[i][j]>result[i][j]){
break;
}else if(output[i][j]<result[i][j]){
flag=false;
break;
}
}
}
if(flag){
memcpy(output,result,sizeof(result));
}
}
}
} if(mincou==){
printf("IMPOSSIBLE\n");
}else{
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if((i==m-) && (j==n-)){
printf("%c",output[i][j]);
}else{
printf("%c ",output[i][j]);
}
}
if(i!=m-){
printf("\n");
} }
} return ;
}
 
 

POJ - 3279(枚举+暴力)的更多相关文章

  1. POJ 3279 枚举(思维)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 4029 Descrip ...

  2. POJ - 3279 枚举 [kuangbin带你飞]专题一

    这题很经典啊,以前也遇到过类似的题--计蒜客 硬币翻转. 不过这题不仅要求翻转次数最少,且翻转方案的字典序也要最小. 解法:二进制枚举第一行的翻转方案,然后处理第二行,如果第二行的k列的上一列是黑色, ...

  3. poj 3279(暴力)

    题意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子,它上下左右的格子都会跟着反转.请求出用最小步数完成反转时每个格子反转的次数. ...

  4. POJ - 1222 / POJ - 3279 枚举第一行

    说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...

  5. POJ 3279 枚举?

    思路: 1.枚举第一行 递推剩下的 判断最后一行成不成立 2. (误)高斯消元? 如何判断1最少和字典序最小- (所以这种做法好像不可取) //By SiriusRen #include <cs ...

  6. 【枚举】POJ 3279

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

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

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

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

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

  9. POJ 3279(Fliptile)题解

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

  10. POJ 3279 Fliptile(翻格子)

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

随机推荐

  1. MySql的优化步骤

    MySql优化的一般步骤: 1.通过show status 命令了解各种sql的执行效率 SHOW STATUS提供msyql服务器的状态信息 一般情况下,我们只需要了解以”Com”开头的指令 sho ...

  2. 测试通过!为何线上还有很多BUG?实践中的质量控制

    质量控制 大多数测试人员认为测试工作是发现bug,虽然这是测试的主要任务,但其实测试最重要的任务是质量控制,而发现bug和验证bug只是质量控制的一个重要环节而已. 我想很多测试人员都经历过这样的场景 ...

  3. springboot RestTemplate httpclient

    RestTemplate是spring支持的一个请求http rest服务的模板对象,性质上有点像jdbcTemplate RestTemplate底层还是使用的httpclient(org.apac ...

  4. hadoop ha 读取 activce状态的活动节点

    方式一 package com.xxx.hadoop; import com.sun.demo.jvmti.hprof.Tracker; import com.sun.xml.bind.util.Wh ...

  5. Scikit-learn 概述

    https://www.leiphone.com/news/201701/ZJMTak4Y8ch3Nwd0.html

  6. 解决TextView drawableRight左侧图片大小不可控的问题

    通过代码来修改图片的大小: Drawable rightDrawable= context.getResources().getDrawable(R.drawable.more); rightDraw ...

  7. Love2D游戏引擎制作贪吃蛇游戏

    代码地址如下:http://www.demodashi.com/demo/15051.html Love2D游戏引擎制作贪吃蛇游戏 内附有linux下的makefile,windows下的生成方法请查 ...

  8. Django模版基本标签详解

    一.if/else{% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% endif %} 之间的任何内 ...

  9. 离线环境下安装ansible,借助有网环境下pip工具

    环境 有网的机器(192.168.19.222):rhe65,python2.7.13,pip9.0.1 离线机器(192.168.19.203):rhe65,python2.6 FTP(192.16 ...

  10. 同一个tomcat下面放多个项目 每个项目用不同的域名访问

    vim ./conf/server.conf <Host name=" appBase="/www/test1/webapps" ##这是war包存放的位置 unp ...