Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10457   Accepted: 3866

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

 
 
只要枚举第一行的格子的翻转情况,然后下面的行数翻转使得上面的格子变为白色,枚举完之后前(n-1)行一定全白,只要检查最后一行就ok
看的题解的思路自己没想到这样写哎
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int N,M;
int e1[20][20];
int e2[20][20];
int tim[20][20];
int an[20][20];
bool e3[20];
int ans=inf;
int fx[4][2]={1,0,-1,0,0,1,0,-1};
void turn(int x,int y)
{
    e2[x][y]=(e2[x][y]==0?1:0);
    for(int i=0;i<4;++i){
        int dx=x+fx[i][0];
        int dy=y+fx[i][1];
         if(dx<1||dy<1||dx>N||dy>M) continue;
                 e2[dx][dy]=(e2[dx][dy]==0?1:0);
    }
}
void make()
{
  int i,j;
  for(i=2;i<=N;++i){
    for(j=1;j<=M;++j){
        int up=e2[i-1][j];
        if(up==0) continue;
        else{
           turn(i,j);
           tim[i][j]++;
        }
    }
  }int s=0;
  for(i=1;i<=M;++i) if(e2[N][i]==0) s++;

if(s==M){
    s=0;

for(i=1;i<=N;++i){
        for(j=1;j<=M;++j) {
                s+=tim[i][j];
       // cout<<tim[i][j];
       // if(j!=M) cout<<" ";
        }
        //cout<<endl;
    }

if(s<ans){
        ans=s;
        memcpy(an,tim,sizeof(int)*400);
    }
  }
}
void solve()
{
   int i,j,k;
   memcpy(e2,e1,sizeof(int)*400);
   memset(tim,0,sizeof(tim));
   for(i=1;i<=M;++i){
    if(e3[i]){
        turn(1,i);
        tim[1][i]++;
    }
   }
   make();
}
void dfs(int x)
{
  if(x==M+1){
    solve();
    return;
  }
  for(int i=0;i<2;++i){
    e3[x]=i;
    dfs(x+1);
  }
}
int main()
{
    int i,j,k;
    while(cin>>N>>M){ans=inf;
        for(i=1;i<=N;++i)
            for(j=1;j<=M;++j) {
                    cin>>e1[i][j];
            }
        dfs(1);
        if(ans==inf) puts("IMPOSSIBLE");
        else{
            for(i=1;i<=N;++i){
                for(j=1;j<=M;++j){
                    if(j!=1) cout<<" ";
                    cout<<an[i][j];
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

POJ3279 搜索/思维的更多相关文章

  1. “玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)

    题目链接   DESCRIPTION INPUT OUTPUT SAMPLE INPUT 1 4 2 1 2 5 2 3 5 3 4 5 5 5 SAMPLE OUTPUT 35 HINT 对于样例, ...

  2. 【CCF】无线网络 搜索+思维

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  3. android搜索框列表布局,流程及主要步骤思维导图

    android搜索框列表布局,流程及主要步骤思维导图 android搜索框列表布局,流程及主要步骤思维导图 activity_coin_search.xml----------<com.scwa ...

  4. 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...

  5. 35、搜索插入位置 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(35)搜索插入位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "无视要 ...

  6. 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)

    心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...

  7. POJ3279 Fliptile 枚举+简单搜索

    题意:一个矩阵,每个点1或0,然后每次翻一个点,它周围上下左右(包括自己)1->0,0->1,问最少翻几次可以矩阵全是0,忽略题目说的字典序 分析:枚举第一行所有的情况,然后下面几行也随之 ...

  8. 相邻行列相互影响的状态类问题(类似状压dp的搜索)(POJ3279)

    POJ3279http://poj.org/problem?id=3279 题意:黑白的板,每次选择一个十字形翻转(十字板内黑白互换,若是边界则不管),求最小将原图变为全白的策略. 这是一道对于每个格 ...

  9. LOJ2323. 「清华集训 2017」小 Y 和地铁 【搜索】【思维】【好】

    LINK 思路 首先如果直接算每一个段有三个决策 左/右 上/下 跨不跨过端点 这样的复杂度是\((2^3)^{22}\),显然是无法接受的 然后考虑怎么优化这个东西 首先左右这个决策是没有意义的 因 ...

随机推荐

  1. Oracle卸载之linux快速卸载rac脚本-一键卸载

    #!/bin/bash#Usage:Log on as the superuser('root') on node1,node2 cd /u01/app/11.2.0/grid/bin./crsctl ...

  2. Nginx服务基础

    Nginx的英文官方网站是http://nginx.org,在这里可以查看Nginx的各个软件版本信息.Nginx软件有三种版本:稳定版.开发版和历史稳定版.开发版更新较快,包含最新的功能和bug的修 ...

  3. Linux入门之常用命令(15) lsof

    查看磁盘空间: [root@ticketb ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 981M 203M 729M ...

  4. 设置XShell快捷键 复制粘贴 并禁用智能选择

    设置XShell快捷键 复制粘贴 并禁用智能选择 1打开选项 2键盘和鼠标->打开编辑 3新建 组合键 Ctrl+C 类型:菜单 ->操作 [编辑]复制  [编辑]粘贴 4选中 ctrl+ ...

  5. 【spring mvc】基础概念

    1.容器 servlet容器 负责管理servlet生命周期. web容器–tomcat 负责管理和部署web应用,其本身可能具备servlet容器组件:如果没有,一般能将第三方servlet容器作为 ...

  6. tp模板基础

    目录简介 创建应用 在项目目录创建入口文件shop/index.php 创建虚拟主机,访问应 路由形式 路由: 系统从URL参数中分析出当前请求的分组.控制器.和操作的过程就是“路由”. Tp框架路由 ...

  7. smart基础

    主要是libs里面的smarty类,和init.inc.php配置文件 剩下的是php文件夹.模板文件夹,临时文件夹.缓存文件夹.配置文件夹.插件文件夹 调用main文件夹中的php文件,通过libs ...

  8. SpringMVC—概述

    mvc容器的实例化: http://blog.csdn.net/lin_shi_cheng/article/details/50686876 Spring的启动过程: 1: 对于一个web应用,其部署 ...

  9. viewport 编写 iPhone Friendly 的 Web 应用程序

    在了解到iPhone的一些常见布局法后,我们就可以开始着手编写一个真正能在iPhone上跑的页面了.小声说一句,之前我说要布局讨论完了,要进入交互逻辑开发,后来细心一想发现不行,有些东西不讲的话将会对 ...

  10. 20155207 2016-2017-2 《Java程序设计》第八周学习总结

    20155207 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第15章 通用API 15.1 日志 15.1.1 日志API简介 java.util.lo ...