POJ 1753 Flip Game (枚举)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26492 | Accepted: 11422 |
Description
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
Source
题意:
有一个4*4的正方形,每个格子上的棋子(即块状物)有正反两面,一面黑色,一面白色,当一个格子里的棋子被翻转时,其周围的四个棋子(如果存在的话)也被翻转,问至少翻转几个格子可以使4*4的正方形里的棋子变为全白或全黑?
思路:
一开始凭感觉吧,同个棋子被翻转2次的话好像作用和未翻转一样,那题目就成了一个棋子是否被翻转的问题了(要么被翻转1次,要么不被翻转)。
那就想到枚举每个棋子的情况,最多枚举 2 ^ (4 * 4) = 2 ^ 16 次,应该可以过。
然后又想到以前书上看过类似的一题,枚举第一排就能推出之后的几排的情况,然后翻书,继续想想,就想通了。确实当第一排被确定时可以推出下面几排的情况。而且只需要枚举 2 ^ 4 * 2次(乘以2是考虑到翻转成全白或者全黑两种情况)即可。
/*************************************************************************
> File Name: POJ1753.c
> Author: BSlin
> Mail:
> Created Time: 2013年10月30日 星期三 19时44分55秒
************************************************************************/ #include <stdio.h>
#include <string.h>
#define INF 20 int map1[5][5],map2[5][5]; int min(int a, int b) {
return a > b ? b : a;
} int inmap(int x, int y) {
if(x >= 0 && x < 4 && y >= 0 && y < 4)
return 1;
return 0;
} void change(int x, int y) {
if(inmap(x,y)) {
map2[x][y] = 1 - map2[x][y];
}
if(inmap(x-1,y)) {
map2[x-1][y] = 1 - map2[x-1][y];
}
if(inmap(x,y+1)) {
map2[x][y+1] = 1 - map2[x][y+1];
}
if(inmap(x+1,y)) {
map2[x+1][y] = 1 - map2[x+1][y];
}
if(inmap(x,y-1)) {
map2[x][y-1] = 1 - map2[x][y-1];
}
} int check(int s, int num) {
int i, j, cnt, x, y;
for(i=0; i<4; i++) {
for(j=0; j<4; j++) {
map2[i][j] = map1[i][j];
}
}
cnt = 0;
for(j=0; j<4; j++) {
if(s & (1 << j)) {
cnt ++;
change(0,j);
}
}
//if(s == 1) {
//for(x=0; x<4; x++) {
//for(y=0; y<4; y++) {
//printf("%d",map2[x][y]);
//}
//printf("\n");
//}
//printf("\n");
//}
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
if(map2[i][j] != num) {
cnt ++;
change(i+1,j);
//if(s == 1) {
//for(x=0; x<4; x++) {
//for(y=0; y<4; y++) {
//printf("%d",map2[x][y]);
//}
//printf("\n");
//}
//printf("\n");
//}
}
}
}
for(j=0; j<4; j++) {
if(map2[3][j] != num)
return INF;
}
return cnt;
} int main() {
freopen("in.txt","r",stdin);
int i,j,ans;
char ch;
for(i=0; i<4; i++) {
for(j=0; j<4; j++) {
scanf("%c",&ch);
if(ch == 'b') {
map1[i][j] = 1;
} else if(ch == 'w') {
map1[i][j] = 0;
}
}
getchar();
}
ans = INF;
for(i=0; i<16; i++) {
ans = min(ans, check(i,0));
}
for(i=0; i<16; i++) {
ans = min(ans, check(i,1));
}
if(ans == INF) {
printf("Impossible\n");
} else {
printf("%d\n",ans);
}
}
POJ 1753 Flip Game (枚举)的更多相关文章
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- POJ 1753 Flip Game(二进制枚举)
题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...
- POJ 1753 Flip Game【枚举】
题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- POJ - 1753 Flip Game(状压枚举)
https://vjudge.net/problem/POJ-1753 题意 4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
随机推荐
- Eclipse反编译插件的安装
步骤: 1.已经安装了Eclipse,如我的Eclipse目录:C:\Programs\JAVA\eclipse 2.反编译插件包:eclipse 反编译插件 jad 3.3.0.zip 3.解压反编 ...
- A - Superset CodeForces - 97B(人生第一个分治法,感觉,像二分啊。。)
/* 分治法,第一次做不是很懂,借鉴了神犇代码,但实操之后感觉像二分,,可能做得少了或者就是.... */ 题目大意: 一个集合里有若干点,要求你添加某些点后保证这个集合里的任意两点满足以下三个条件中 ...
- JAVAEE——Solr:安装及配置、后台管理索引库、 使用SolrJ管理索引库、仿京东的电商搜索案例实现
1 学习回顾 1. Lucene 是Apache开源的全文检索的工具包 创建索引 查询索引 2. 遇到问题? 文件名 及文件内容 顺序扫描法 全文检索 3. 什么是全文检索? 这种先创建索引 再 ...
- Linux-CentOs7-svn安装及钩子配置
做个svn的教程 首先进入test目录下,新建一个svn目录,准备做svn测试cd /testmkdir svncd svn然后使用yum安装svn,这里就不使用编译安装了,这玩意只要能用就行,版本无 ...
- NetCore+Dapper WebApi架构搭建(四):仓储的依赖注入
上一节我们讲到实体,仓储接口和仓储接口的实现需要遵循约定的命名规范,不仅是规范,而且为了依赖注入,现在我们实现仓储的依赖注入 在NetCore WebApi项目中新添加一个文件夹(Unit),当然你也 ...
- luogu P3592 [POI2015]MYJ
题目链接 luogu P3592 [POI2015]MYJ 题解 区间dp 设f[l][r][k]表示区间l到r内最小值>=k的最大收益 枚举为k的位置p,那么包含p的区间答案全部是k 设h[i ...
- [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)
对于点集$A$,$B$,闵可夫斯基和$C=\{(x1+x2,y1+y2)|(x1,x2)\in A,(y1,y2)\in B\}$.由此可知,对于两个凸包$A$,$B$的闵可夫斯基和$C$满足,$C$ ...
- hdu 3338 最大流 ****
题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...
- bzoj 4017 子序列和的异或以及异或的和
位运算很好的一个性质是可以单独每一位考虑..... 题解请看:http://blog.csdn.net/skywalkert/article/details/45401245 对于异或的和,先枚举位, ...
- 移动端适配之REM
随着手机等移动设备的普及,移动端带来的流量已经不可忽视,一个网站不只是只有pc的页面就足够了,移动端的适配已经势在必行.但是移动设备种类繁多,屏幕尺寸也千奇百怪,能不能找到一种方式可以适配所有的手机屏 ...