poj dfs相关之1753 Flip Game
来源:互联网 发布:手机淘宝怎么上传图片 编辑:程序博客网 时间:2024/06/05 01:52
poj dfs相关之1753 Flip Game
132K 313MS
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<queue>using namespace std;int n, k, ans;bool IsFind, arr[6][6];int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };bool check(){ int i, j, t; t = arr[1][1]; for (i = 1; i <= 4;i++) for (j = 1; j <= 4; j++) { if (t != arr[i][j]) return false; } return true;}void flip(int row, int col){ int i, x, y; arr[row][col] = !arr[row][col]; for (i = 0; i < 4; i++) { x = row + dir[i][0]; y = col + dir[i][1]; arr[x][y] = !arr[x][y]; }}void dfs(int row,int col,int dep){ if (dep == ans) { if (check()) IsFind = true; return; } if (row>4 || col > 4 || IsFind) return; flip(row, col); if (col < 4) dfs(row, col + 1, dep + 1); else dfs(row + 1, 1, dep + 1); flip(row, col); if (col < 4) dfs(row, col + 1, dep); else dfs(row + 1, 1, dep);}int main(){ //freopen("1.txt", "r", stdin); int i, j, tmp1, tmp2; memset(arr, 0, sizeof(arr)); char tmp[15]; for (i = 1; i < 5; i++) { scanf("%s", tmp); for (j = 1; j < 5; j++) { if (tmp[j - 1] == 'b') arr[i][j] = 1; else if (tmp[j - 1] == 'w') arr[i][j] = 0; } } for (i = 0; i <= 16; i++) { IsFind = false; ans = i; dfs(1, 1, 0); if (IsFind) break; } if(IsFind) printf("%d\n", ans); else printf("Impossible\n");}
132K 125MS
这个是一个一次性枚举所有的版本,效率更高
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<queue>using namespace std;int n, k, ans;bool IsFind, arr[6][6];int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };bool check(){ int i, j, t; t = arr[1][1]; for (i = 1; i <= 4;i++) for (j = 1; j <= 4; j++) { if (t != arr[i][j]) return false; } return true;}void flip(int row, int col){ int i, x, y; arr[row][col] = !arr[row][col]; for (i = 0; i < 4; i++) { x = row + dir[i][0]; y = col + dir[i][1]; arr[x][y] = !arr[x][y]; }}void dfs(int row,int col,int dep){ if (check()) { ans = min(ans, dep); return; } if (row>4 || col > 4 || IsFind) return; flip(row, col); if (col < 4) dfs(row, col + 1, dep + 1); else dfs(row + 1, 1, dep + 1); flip(row, col); if (col < 4) dfs(row, col + 1, dep); else dfs(row + 1, 1, dep);}int main(){ freopen("1.txt", "r", stdin); int i, j, tmp1, tmp2; memset(arr, 0, sizeof(arr)); char tmp[15]; for (i = 1; i < 5; i++) { scanf("%s", tmp); for (j = 1; j < 5; j++) { if (tmp[j - 1] == 'b') arr[i][j] = 1; else if (tmp[j - 1] == 'w') arr[i][j] = 0; } } ans = 17; dfs(1, 1, 0); if(ans<17) printf("%d\n", ans); else printf("Impossible\n");}
阅读全文
0 0
- poj dfs相关之1753 Flip Game
- 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)
- 6月21日云栖精选夜读:国内首位!Node.js社区将阿里云工程师张秋怡吸纳为CTC成员
- ural 1022. Genealogical Tree 拓扑排序
- php数字转汉字代码(算法)
- NSNotificationCenter深入研究
- 设置<input type="file">的默认样式
- poj dfs相关之1753 Flip Game
- java面试问题总结
- 【技术干货】下一代私有云盘 NextCloud 的安装配置
- azkaban安装与使用
- input placeholder 文字设定样式
- AndroidStudio开发中有用快捷键整理
- mysql支持中文设置
- uboot分析之Makefile(二)两种编译
- maven下载