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的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动.现要求把所有棋子都翻成同一种颜色,问最少需要几步. 分析 同一个 ...
随机推荐
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
- 【Naive Splay Template】
写小作业的时候重新复习了一下splay 只支持插入,删除,查k大,查节点数.没有迭代器. T类型需要重载==和<,要调用拷贝构造函数. template<class T> class ...
- 4144: [AMPPZ2014]Petrol (多源最短路+最小生成树+启发式合并)
4144: [AMPPZ2014]Petrol Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 752 Solved: 298[Submit][Sta ...
- TortoiseGit + msysgit 记住帐号密码方法及使用密匙的方法
Windows 重度用户只能用 for windows 的软件了,所以虽然使用 Git,但还是要找专门的 windows 版本. 最近开始使用 GitHub 来托管一些小项目/兴趣,而自己是重度 wi ...
- [CC-SEINC]Sereja and Subsegment Increasings
[CC-SEINC]Sereja and Subsegment Increasings 题目大意: 有长度为\(n(n\le10^5)\)的序列\(A\)和\(B\). 在一次操作中,可以选择一个区间 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String 二分
C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...
- ACdream 速攻组~
1007 a + b /*这题就是一个快速幂,但是十分猥琐的是,模是1e10 + 7,不是1e9 + 7,这就产生了一个爆long long的问题.所以要对快速幂中的乘法操作进行一下改造.请教了BIT ...
- Matlab 常用绘图指令(二维图形)
使用matlab的时候常常会忘掉一些指令,每次都要重新查找,挺麻烦的,这里收集一些常用的绘图指令,供自己和大家以后方便查找和使用. 1.例子-包含了常用绘图命令 clear clc %%数据准备 x ...
- Antd前端开发采坑记录
背景 基于页面友好,界面整洁美观:基于Antd框架开发虾能平台 选型 基于Antd-admin工程架构,进行开发:基于Antd+React+Umj 采坑记录 按照Html方式天机onClick方法,每 ...
- HDU 4730 We Love MOE Girls (2013成都网络赛,签到水题)
We Love MOE Girls Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...