poJ 1753(dfs) Flip Game
来源:互联网 发布:淘宝店铺吸引流量 编辑:程序博客网 时间:2024/06/05 06:52
Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 46061 Accepted: 19735
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
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.
- 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
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwbbbwbbwwbbwww
Sample Output
4
这个题目是一个比较不错的DFS的题目,作为萌新,刚开始学习DFS,慢慢不断了解DFS,其实感觉它没有自己想想的那么难,更好的理解,它就是一棵树,利用前序遍历,这样来做,不懂树的遍历方法,可以手动百度下。
#include<stdio.h>#include<string.h>int step =0x7fffffff;void dfs(int a,int b,char c[10][10]){int i,j,sum=0;char ch[10][10];for(i=0;i<4;i++)//每次DFs后都扫描一边是否满足题意{for(j=0;j<4;j++){if(c[i][j] == 'b')sum++;}}if(sum==0 || sum==16){if(b < step)step=b;return ;}if(a==16)//a可以看过是扫描的棋子的数return ;for(i=0;i<4;i++)strcpy(ch[i],c[i]);dfs(a+1,b,ch);//当前位置不作处理向下递归 int r=a/4; //行数int w=a%4;//列数 if(ch[r][w]=='b')//翻转当前的状态 ch[r][w]='w';elsech[r][w]='b';if(r>=1)//行数大于等于一 代表有上界 {if(ch[r-1][w]=='b')ch[r-1][w]='w';elsech[r-1][w]='b';}if(r<=2)//说明拥有下界{if(ch[r+1][w]=='b')ch[r+1][w]='w';elsech[r+1][w]='b';} if(w>=1)//拥有左边的界限 {if(ch[r][w-1]=='b')ch[r][w-1]='w';elsech[r][w-1]='b';}if(w<=2)//拥有右边的界限{if(ch[r][w+1]=='b')ch[r][w+1]='w';elsech[r][w+1]='b'; } dfs(a+1,b+1,ch); }int main(){int i;char a[10][10];for(i=0;i<4;i++)scanf("%s",a[i]);dfs(0,0,a);if(step == 0x7fffffff)printf("Impossible\n");else printf("%d\n",step);return 0;}
就是查询了每种情况,然后找到最短的那种,记录下来~数据量大的话DFS肯定超时,因为它就和暴力差不多了,每种情况都扫描到了。
阅读全文
0 0
- POJ 1753 Flip Game (DFS)
- POJ--1753--Flip Game【DFS】
- POJ 1753 Flip Game (DFS)
- poj 1753 Flip Game【dfs】
- poJ 1753(dfs) Flip Game
- poj 1753 Flip Game(dfs)
- poj 1753 Flip Game(dfs+枚举)
- POJ 1753 Flip Game BFS/DFS
- POJ 1753 Flip Game dfs + 枚举
- POJ 1753 Flip Game ------- DFS & 位运算 .
- Poj 1753 Flip Game 状态压缩 + DFS
- POJ 1753 Flip Game DFS枚举
- POJ 1753 Flip Game (DFS + 枚举)
- POJ 1753 Flip Game【暴搜DFS】
- poj 1753 Flip Game(搜索:DFS+水题)
- poj 1753 Flip Game (dfs + 枚举)
- poj 1753 Flip Game (dfs)
- POJ 题目1753 Flip Game(DFS)
- java maven打jar包
- [11]LAMP架构
- Centos 7.2 Hadoop2.7+Spark2.1分布式集群搭建
- com/intellij/junit5/JUnit5IdeaTestRunner : Unsupported major.minor version 52.0
- Go游戏服务器开发的一些思考(十一):IO游戏同步
- poJ 1753(dfs) Flip Game
- list的三种遍历方法
- BZOJ 1878-HH的项链(离线操作+树状数组)
- Java 如何操纵 XML
- jquery的复选框操作
- 浅谈分布式消息技术 Kafka
- CentOS 7 搭建SVN服务器
- Opencv最新版本Opencv3.3.0集成了DNN神经网络模块
- VS Code 配置 C/C++ 环境