POJ 1753 Flip Game 棋盘状态搜索
来源:互联网 发布:淘宝客服要开通管理 编辑:程序博客网 时间:2024/06/06 13:17
按规则翻转棋子的模拟题。也是一道搜索题。
题意:给出一个棋盘的初始状态,翻动一粒棋子会使得翻动的棋子和周围东南西北四个方向上的棋子也会翻转。black to white , white to black
传送门:POJ-1753-Flip Game
/*** @Author: Hallbow* @Date: 2016-07-22T11:04:59+08:00* @Email: 812966736@qq.com* @Last modified by: Hallbow* @Last modified time: 2016-08-01T11:01:02+08:00*/#include <stdio.h>#define INF 0xFFFFFFbool map[4][4] = {0};bool vis[4][4] = {0};int minSteps = INF;// 是否已全白全黑bool checkMap(){ for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ if(map[i][j] != map[0][0]){ return false; } } } return true;}// 翻转(x,y)void converseMap(int x, int y){ if(x > 0) map[x-1][y] = !map[x-1][y]; if(x < 3) map[x+1][y] = !map[x+1][y]; if(y > 0) map[x][y-1] = !map[x][y-1]; if(y < 3) map[x][y+1] = !map[x][y+1]; map[x][y] = !map[x][y];}// dfsvoid dfs(int x, int y, int steps){ // 若已到第四行 check是否胜利 if(x == 4){ if(checkMap()){ if(steps < minSteps) minSteps = steps; } return; } int i,j; // 翻 converseMap(x, y); // 换行 if(y == 3){ // 下个棋子 dfs(x+1, 0, steps+1); // 回溯 不翻 converseMap(x, y); // 下一棋子 dfs(x+1, 0, steps); }else{ dfs(x, y+1, steps+1); converseMap(x, y); dfs(x, y+1, steps); }}int main(){ int i,j; char _map[5]; // freopen("data.in", "r", stdin); while(1){ minSteps = INF; for(i=0; i<4; i++){ if(scanf("%s", _map) == EOF) return 0; for(j=0; _map[j]; j++){ map[i][j] = _map[j] == 'b' ? true: false; } } dfs(0, 0, 0); if(minSteps != INF){ printf("%d\n", minSteps); }else{ printf("Impossible\n"); } } return 0;}
1 0
- POJ 1753 Flip Game 棋盘状态搜索
- poj 1753 Flip Game 搜索
- POJ 1753:Flip Game:棋盘枚举1
- POJ flip Game翻转棋盘
- poj 1753 Flip Game 二进制状态压缩
- POJ 1753 Flip Game 状态压缩 枚举
- Poj 1753 Flip Game 状态压缩 + DFS
- POJ 1753 Flip Game(状态压缩)
- 【BFS+状态压缩】-POJ-1753-Flip Game
- POJ 1753 Flip Game(状态压缩BFS)
- POJ 1753 Flip Game(BFS+状态压缩)
- poj 1753 Flip Game 状态压缩
- poj 1753 Flip Game(状态压缩枚举)
- poj 1753 Flip Game(搜索:DFS+水题)
- poj 1753 Flip Game (枚举+搜索)
- POJ 1753 Flip Game(暴力+搜索递归)
- POJ 1753 Flip Game【翻转棋盘+枚举+dfs】
- POJ-1753-Flip Game【翻转棋盘+枚举+dfs】
- 123452345678
- 小方法1——CountChar--统计一个字符串中每个字符出现的次数
- Hibernate关联映射之一对多,多对一以及双向一对多
- 量化分析师的Python日记
- lintcode178. graph valid tree 图是否是树
- POJ 1753 Flip Game 棋盘状态搜索
- JUnit4学习笔记
- jzoj 2548. 【NOIP2011模拟9.4】最大正方形
- mipi差分信号原理
- html+js的遮罩实现
- OPENCV入门教程二十:Canny边缘检测
- Android 如何通过menu id来得到menu item 控件
- Javaweb异常提示信息统一处理
- 一篇专题让你秒懂GCD死锁问题!