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. 分布式一致性hash算法

    写在前面  在学习Redis的集群内容时,看到这么一句话:Redis并没有使用一致性hash算法,而是引入哈希槽的概念.而分布式缓存Memcached则是使用分布式一致性hash算法来实现分布式存储. ...

  2. 【转】fiddler抓包HTTPS请求

    本文转自:http://blog.csdn.net/idlear/article/details/50999490 fiddler抓包HTTPS请求 跟着教程来,保证100%成功抓HTTPS包 教程开 ...

  3. Gcode命令【转】

    https://www.jianshu.com/p/f8a328457a45 简述 研究过3D打印机的朋友,都会用到G-code文件.要使用3D打印机打印东西要经过几个步骤:        1.创建3 ...

  4. grid - 隐式网格

    当网格项目确认在显式网格之外时就会创建隐性网格,当没有足够的空间或者显式的网格轨道来设置网格项目,此时网格项目就会自动创建隐式网格. 隐式网格可以定义:grid-auto-rows.grid-auto ...

  5. TensorFlow实战Google深度学习框架1-4章学习笔记

    目录 第1章 深度学习简介 第2章 TensorFlow环境搭建 第3章 TensorFlow入门 第4章 深层神经网络   第1章 深度学习简介 对于许多机器学习问题来说,特征提取不是一件简单的事情 ...

  6. JSONObject、JSONArray、Map、JavaBean的相互转换

    1,JSONObject json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} 2,JSONArray json数组,使用中括号[ ],只不过数组里面的项也是json键 ...

  7. LIN 笔记

    LIN 使用了 1 根线来进行通信,但是,它必须要参考 VBat 和 GND.离开这两个参考电平,并没有办法来判断线上的 bit 状态. 另外,根据经典的 LIN 驱动电路(一个 OC 门),RX 接 ...

  8. protobuf的简单使用

    操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 go 版本 : go1.8.3 linux/amd64 Python 版本 : 2.7.5 libprotoc : 2.5 ...

  9. P Invoke struct结构

    一.获取Struct CHCNetSDK.NET_DVR_PTZPOS pos = new CameraTest.CHCNetSDK.NET_DVR_PTZPOS(); int size = Mars ...

  10. Java之Servlet

    Servlet规范了JavaWeb项目的结构Servlet的规范约束了服务器如何来实现Servlet规范,如何解析JavaWeb项目的结构. Java就是通过接口来约束 Servlet规范的jar就在 ...