解题报告:POJ 1753 Flip Game 简单深搜dfs
来源:互联网 发布:长空栈道 知乎 编辑:程序博客网 时间:2024/06/10 21:59
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
Source
Northeastern Europe 2000
类型:简单深搜DFS
题目大意:
小时候玩的一种棋,游戏规则是每一步选一个棋子,将这个棋子和所有和它相邻的棋子翻转。现在题目给你一个初始状态,问你最少多少步可以获得全黑或者全白的状态。
思路:
所有的情况就是翻转棋子数从0到16,然后DFS深搜选择翻转的棋子,最后判断是否到达结束状态,到达则输出并且结束,如果最后也没有达到这个状态,则输出Impossibel 。最近POJ网站好像出了点问题,一直登不上= =
AC代码:
#include<cstdio>#include<cstring>using namespace std;int Map[5][5];int A[4][2]={0,1,0,-1,1,0,-1,0};char str[10];int k;int check(){ int x=Map[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(Map[i][j]!=x) return 0; return 1;}void oper(int a,int b){ Map[a][b]=!Map[a][b]; for(int i=0;i<4;i++) { int x=a+A[i][0],y=b+A[i][1]; if(x>=0&&x<4&&y>=0&&y<4) Map[x][y]=!Map[x][y]; }}void dfs(int x,int y,int m,int n){ if(k) return ; if(m==n) { if(check()) k=1; } else { if(y==4) y=0,x++; if(x==4) return ; for(int i=x;i<4&&!k;i++) for(int j=y;j<4&&!k;j++) { oper(i,j); dfs(i,j+1,m+1,n); oper(i,j); } }}int main(){ while(scanf("%s",str)==1) { k=0; for(int j=0;j<4;j++) { if(str[j]=='w') Map[0][j]=0; else Map[0][j]=1; } for(int i=1;i<=3;i++) { scanf("%s",str); for(int j=0;j<4;j++) { if(str[j]=='w') Map[i][j]=0; else Map[i][j]=1; } } for(int i=0;i<=16;i++) { dfs(0,0,0,i); if(k) { printf("%d\n",i); break; } } if(!k) printf("Impossible\n"); }}
0 0
- 解题报告:POJ 1753 Flip Game 简单深搜dfs
- POJ 1753 Flip Game 解题报告(高斯消元法)
- POJ 1753 Flip Game (状态压缩+BFS) -- 解题报告
- 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+水题)
- wc滚粗记
- JDK,JRE,JVM区别与联系
- ScrollView不能滚动的问题
- 时间分割线:2016年1月的文章都是从以前chinaunix的博客文章
- xutils 2.x(2.6)中的session获得和cookieStore使用
- 解题报告:POJ 1753 Flip Game 简单深搜dfs
- 快速排序
- java 图片的放大与缩小--等距采样算法
- 关于普通dij算法为什么不能解决负权边的分析
- [Spring实战系列](2)Maven创建Spring-HelloWorld项目
- 从头认识Spring-1.5 Bean的作用域
- JS小Demo实战之今天外卖点什么?(一)
- iOS 蓝牙开发用到的一些格式转换
- 浅谈七层——架构师成长