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的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
随机推荐
- 如何使用django显示一张图片
django显示图片对新手来说真的算是一个坑.. 这里记录下小白爬坑的历程. 首先,你需要一个可以运行的django服务器,能显示正常的html文本,无法显示图片 这是html的文本,可以显示文字,无 ...
- Python 学习经历分享
如果说 Java 是亲儿子的话,那么 Python 应该就是干儿子了.看了一下所有关于 Python 的笔记,我发现原来我在 4 月份的时候就已经涉足 Python 了,但是到目前为止才真正算做出了一 ...
- iOS 9应用开发教程之多行读写文本ios9文本视图
iOS 9应用开发教程之多行读写文本ios9文本视图 多行读写文本——ios9文本视图 文本视图也是输入控件,与文本框不同的是,文本视图可以让用户输入多行,如图2.23所示.在此图中字符串“说点什么吧 ...
- ARM 常用汇编指令
ARM 汇编程序的框架结构 .section .data <初始化的数据> .section.bss <未初始化的数据> .section .text .global _sta ...
- BZOJ 4826: [Hnoi2017]影魔 单调栈 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4826 年少不知空间贵,相顾mle空流泪. 和上一道主席树求的东西差不多,求两种对 1. max(a ...
- with在模板中的应用
var str = 'Hello <%= name %>!'; var o = { name: 'Alice' }; function tmpl(str, obj) { str = 'va ...
- [HNOI2008]玩具装箱
OJ题号: BZOJ1010 思路: 斜率优化动态规划. 由题意得状态转移方程为$f_i=\displaystyle{\min_{j=0}^{i-1}}\{f_j+\left(i-j-1+\displ ...
- UC浏览器 垂直水平居中
今天使用下述方式定义水平垂直居中不起作用 #box{ position: fixed; left:; right:; top:; bottom:; margin: auto; } 然后改用: #box ...
- 5、Redis中对Set类型的操作命令
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- ------------ ...
- pygame系列_pygame的各模块叙述
在pygame中,有很多模块,每个模块对应着不同的功能,如果我们知道这些模块是做什么的,那么,对我们的游戏开发会起到关键性的作用. 我们就说说pygame中的各个模块吧!!! #pygame modu ...