Fliptile

POJ - 3279

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

这题有点坑,一开始完全没想到搜索,思路是想将第一行所有踩的情况遍历一遍,后面搜索到第n行,从第二行到第n行每一行的目的都是将其前一行的1转化为0,最后判断第n行是否为全为0.

以下是我的代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int ans;
int num;
int s[20][20];
int mapp[20][20];
int m,n;
int a[20][20];
int b[20][20];
void play(int x)
{ms(a);rep(i,1,n) {rep(j,1,m) {s[i][j]=mapp[i][j];}}num=0;lep(j,m,1) {if(x&1) {s[1][j-1]=!s[1][j-1];s[1][j+1]=!s[1][j+1];s[1][j]=!s[1][j];s[2][j]=!s[2][j];a[1][j]=1;num++;}x>>=1;}rep(i,2,n) {rep(j,1,m) {if(s[i-1][j]==1) {s[i][j-1]=!s[i][j-1];s[i][j+1]=!s[i][j+1];s[i][j]=!s[i][j];s[i+1][j]=!s[i+1][j];s[i-1][j]=!s[i-1][j];a[i][j]=1;num++;}}}rep(i,1,n) {rep(j,1,m) {if(s[i][j]==1)num+=ans;}}
}
void cp()
{rep(i,1,n) {rep(j,1,m) {b[i][j]=a[i][j];}}
}
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);cin>>n>>m;ms(b);rep(i,1,n) {rep(j,1,m) {cin>>mapp[i][j];}}ans=maxn;int x=(1<<m);rep(i,0,x-1) {play(i);if(num<ans) {ans=num;cp();}}if(ans==maxn) {cout<<"IMPOSSIBLE"<<endl;}else {rep(i,1,n) {rep(j,1,m) {cout<<b[i][j]<<" ";}cout<<endl;}
}return 0;
}

Fliptile【搜索】相关推荐

  1. Fliptile——搜索+二进制优化

    [题目描述] Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk ...

  2. [kuangbin带你飞]专题一 简单搜索D - Fliptile(POJ 3279)

    题目大意 给一个N行M列的矩阵,值分别为0和1,每次你可以选择将一个变成相反状态,同时,它周围的四个数也会变为相反状态. 问:最少翻转多少次,可以将所有值都变成0 多个解,输出翻转次数最少的(若有次数 ...

  3. D - Fliptile POJ - 3279(翻转问题)

    D - Fliptile POJ - 3279 题意: 给一个m*n的01矩阵,对某一块砖踩一脚,1->0 || 0->1, 求将整个举证全部变为0最少踩几次 典型的翻转问题,此类问题的特 ...

  4. POJ 3279(Fliptile)题解

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

  5. Fliptile 翻格子游戏[Usaco2007 Open]

    题目描述 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. ...

  6. 【搜索】搜刷刷题整理

    包含洛谷和vjudgekuangbin题目(慢慢更新中) 洛谷 1.马的遍历 题目链接:https://www.luogu.com.cn/problem/P1443 题意:给你一个n×m的棋盘,然后给 ...

  7. kuangbin 专题一 简单搜索

    kuangbin 专题一 简单搜索 1.POJ1321棋盘问题[DFS] 代码 自己的想法 2.POJ2251Dungeon Master[三维空间BFS] 代码 自己的想法 3.POJ3278 Ca ...

  8. 搜索专题(不定期更新)

    1.POJ 2386  Lake Counting 题意:给出一块区域,询问有多少个湖泊? 思路:DFS,对于'W',深搜一次,并标记已访问.之后每次对未访问的'W'做一次深搜. 1 #include ...

  9. 算法笔记学习(3)---深度优先搜索(DFS)

    深度优先搜索(DFS) 设想我们现在身处一个巨大的迷宫之中,以当前所在位置为起点,沿着一条路向前走,当碰到岔路口的时候,就选择其中一个岔道口前进.如果选择的这个岔路前方是一条死路,就退回到这个岔道口, ...

最新文章

  1. 一维卷积filter_从零开始学Pytorch(七)之卷积神经网络
  2. PHP session回收机制
  3. lnmp上搭建zabbix
  4. Java BigInteger类| toByteArray()方法与示例
  5. mysql重新编译_重新编译mysqld_exporter0.10-阿里云开发者社区
  6. websocket server client 编写
  7. PDF如何编辑,怎么删除空白页面
  8. CSS:实现跳动小球蒙版效果
  9. (14) 常用管理类软件需求征集 -- 组织机构管理
  10. 065 循环导入问题
  11. iOS开发入门学习路线
  12. 任务的紧急度,重要性划分 .
  13. 简单Python爬虫实例:抓取豆瓣热映电影信息
  14. 仙剑3外传(问情篇)Win7(32/64位)不能运行的完全解决办法【转载】
  15. 密码学系列 - DER编码
  16. LivePlayer H5播放器、在react中使用
  17. linux延时函数及头文件,linux延时函数
  18. 外包招聘背调,是对前雇主的一种侮辱
  19. java 使用sourceforge.pinyin4j查询汉字拼音
  20. ABAP 使用MODIF ID实现动态选择屏幕示例

热门文章

  1. QtCreate编译器在调试程序时,右侧的变量表达式值视图被不小心关闭了
  2. C语言 文件读写 fseek 函数 - C语言零基础入门教程
  3. BugkuCTF-WEB题文件包含
  4. java定义变量的输入_Terraform中输入变量
  5. php加大session,PHP :: Bug #63251 :: yaf session功能增强
  6. java crontriggerbean_java – 使用JobStoreTX为石英聚类配置CronTriggerFactoryBean
  7. 求一批整数中出现最多的个位数字_(43)C++面试之从1到n整数中1出现的次数
  8. python中怎么创建配置文件_如何在Django中创建配置文件注册表单?
  9. 计算机控制常用数据通信标准,计算机系统第6章通信ppt课件.ppt
  10. php创建输入文本框,Asp:文本框与输入文本(PHP开发人员学习ASP)