POJ3279 Fliptile —— 状态压缩 + 模拟

摘要:
因为在这一行中,只有当前的瓦片可以使头上的瓦片为零,然后它将被推到最后一行。因此,上述方法相对更“智能”。POJ1753:http://www.cnblogs.com/DOLFAMINGO/p/7538788.html代码如下:1#包括<iostream>2#包括<cstdio>3#包括<cstring>4#包括<cmath>5#包括<algorithm>6#包括<vector>7#包括<queue>8#包括<stack>9#包括<map>10#包括<string>11#包括<set>12#定义(a,b)memset13usingnamespacestd;14类型deflonglongLL;15constintINF=2e9;16constLLLNF=9e18;17常数MOD=1e9+7;18常量最大值=15+10;1920intM[MAXN][MAXN],子M[MAXN][MAXN];21intop[MAXN][MAXN],ans_ op[MAXN][MAXN];22英寸,米;2324无效压力25{26op[x][y]=1;27subM[x][y]=!

题目链接:http://poj.org/problem?id=3279

Fliptile
Time Limit:2000MSMemory Limit:65536K
Total Submissions:11771Accepted:4360

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 anM×Ngrid (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:MandN
Lines 2..M+1: Linei+1 describes the colors (left to right) of row i of the grid withNspace-separated integers which are 1 for black and 0 for white

Output

Lines 1..M: Each line containsNspace-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

USACO 2007 Open Silver

题解:

1.枚举第一行的所有状态。
2.从第二行开始,如果当前瓦片的头上瓦片为1,那么需要翻转当前瓦片。因为在这一行之内,只有当前瓦片能够使头上的瓦片变为0,然后一直递推到最后一行。
3.分析:一开始想到方法是枚举,但是数据太大,计算机要算多少年真的不好估计。先假设枚举法是可行的, 相对于枚举法, 上述方法是有明显的优势的:上述方法从第二行开始,每翻转一块瓦片都是有针对性的、有目的性的;而枚举法则是盲目地翻转,如:假如当前瓦片为0,却还是硬要翻转该瓦片,那显然是行不通的。所以上述方法相对来说更“智能”。
4.注意:s&(1<<i)的值要么等于0,要么等于(1<<i);而不是0或1。应注意!!

5.此题的弱化版,可使用枚举法。POJ1753:http://www.cnblogs.com/DOLFAMINGO/p/7538788.html

代码如下:

1 #include <iostream>  
2 #include <cstdio>  
3 #include <cstring>  
4 #include <cmath>  
5 #include <algorithm>  
6 #include <vector>  
7 #include <queue>  
8 #include <stack>  
9 #include <map>  
10 #include <string>  
11 #include <set>  
12 #define ms(a,b) memset((a),(b),sizeof((a)))  
13 using namespacestd;  
14 typedef long longLL;  
15 const int INF =2e9;  
16 const LL LNF =9e18;  
17 const int MOD = 1e9+7;  
18 const int MAXN = 15+10;  
19   
20 intM[MAXN][MAXN], subM[MAXN][MAXN];  
21 intop[MAXN][MAXN], ans_op[MAXN][MAXN];  
22 intn, m;  
23   
24 void press(int x, inty)  
25 {  
26     op[x][y] = 1;  
27     subM[x][y] = !subM[x][y];  
28     if(x-1>=0) subM[x-1][y] = !subM[x-1][y];  
29     if(x+1<n)  subM[x+1][y] = !subM[x+1][y];  
30     if(y-1>=0) subM[x][y-1] = !subM[x][y-1];  
31     if(y+1<m)  subM[x][y+1] = !subM[x][y+1];  
32 }  
33   
34 boolallZero()  
35 {  
36     for(int i = 0; i<n; i++)  
37     for(int j = 0; j<m; j++)  
38         if(subM[i][j])  return false;  
39     return true;  
40 }  
41   
42 voidsolve()  
43 {  
44     int ans_step =INF;  
45     for(int s = 0; s<(1<<m); s++)  
46 {  
47         ms(op, 0);  
48         memcpy(subM, M, sizeof(subM));  
49         int step = 0;  
50         for(int i = 0; i<m; i++)  
51             if(s&(1<<i)) press(0, i), step++;  
52   
53         for(int i = 1; i<n; i++)  
54         for(int j = 0; j<m; j++)  
55             if(subM[i-1][j])  press(i, j), step++;  
56   
57         if(allZero() && ans_step>step )  
58 {  
59             ans_step =step;  
60             memcpy(ans_op, op, sizeof(ans_op));  
61 }  
62 }  
63     if(ans_step==INF)  
64         puts("IMPOSSIBLE");  
65     else  
66 {  
67         for(int i = 0; i<n; i++){  
68             for(int j = 0; j<m; j++)  
69                 printf("%d ",ans_op[i][j]);  
70             putchar('');  
71 }  
72 }  
73 }  
74   
75 intmain()  
76 {  
77     while(scanf("%d%d",&n,&m)!=EOF)  
78 {  
79         for(int i = 0; i<n; i++)  
80         for(int j = 0; j<m; j++)  
81             scanf("%d",&M[i][j]);  
82   
83 solve();  
84 }  
85     return 0;  
86 }  
View Code

免责声明:文章转载自《POJ3279 Fliptile —— 状态压缩 + 模拟》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇swagger2的常用注解,传递参数的注意使用方法螺旋折线(可能是最简单的找规律)【蓝桥杯2018 C/C++ B组】下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

Protobuf动态解析那些事儿

收到protobuf数据后,如何自动创建特定的ProtobufMessage对象,然后对其进行反序列化。有关Protobuf的技术介绍,请参阅Google协议缓冲区的在线帮助页面或IBM developerworks上的文章“Google协议缓冲的使用和原理”。protobuffer的动态分析并没有在谷歌protobuffer官网上介绍。有关实现,请参阅淘宝...

xcode模拟器不显示键盘解决方案

当我们使用Xcode进行开发时,我们并不总是需要在iPhone上运行代码。有时模拟器可以解决这些问题。但当你使用模拟器时,你会发现,如果你使用模拟器上的键盘在TextFiled中输入信息,这是可以的,但如果你使用键盘输入信息,那么你会发现模拟器上的屏幕将不再显示。这是因为默认情况下,xcode使用计算机键盘作为外部键盘,不会弹出虚拟键盘。...

android动态申请权限

申请权限对于安卓开发很重要,从Android6.0开始,Android系统提供动态申请权限的机制,APP在使用危险权限时,需要用户的授权才可进一步操作。...

开源项目推荐:Qt有关的GitHub/Gitee开源项目

https://www.froglogic.com/windeployqthttps://doc.qt.io/Qt-5/windows部署。htmlhttps://wiki.qt.io/Deploy_an_Application_on_Windowshttps://github.com/lucasg/Dependencieshttp://www.depend...

2.页面绘制和引入组件库uView

文本+背景色的形式,而不是横幅图的形式,可以节省未来的工作量。在index.vue中,关于开关的代码:EFGHIJKLMNOPQRSTUWXYZB˃DEFGHIJKLNNOPQRSTUVWXYZEFGHIJKLMNOPQRSTUVWXYZ导出默认值{data(){return{}},onLoad()},方法:{}}。横幅{width:100%;height:...

Selenium操作示例——鼠标悬停显示二级菜单,再点击二级菜单或下拉列表

这两天在python中玩selenium时,我遇到了一个问题,那就是鼠标移动到页面上的一个按钮或菜单,二级菜单或下拉菜单自动弹出,然后二级菜单或者下拉列表自动点击。...