hdu1195 Open the Lock BFS 广搜
来源:互联网 发布:选股好帮手 mac 破解 编辑:程序博客网 时间:2024/04/27 15:11
题目链接:here
题意:
给你四个数字,每次可以将其中任何一个数字 +1 或者 -1 ; 1的时候-1 等于9 ;9的时候+1 等于1; 问最少需要变换几次,才可以变到目标序列。。
分析:
我先用广搜做了一遍,随后又用双向广搜做了一遍,发现双向广搜效率的确很高!
贴一下图:
第一个是双向广搜的,下面的是广搜。。。
晒代码:
广搜:
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{int num[4];int step;};queue<node> q;bool vis[10][10][10][10];int ansnum[4];int ans;int dir[8][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{-1,0,0,0},{0,-1,0,0},{0,0,-1,0},{0,0,0,-1}};void init(){memset(vis, false, sizeof(vis));while (!q.empty()) q.pop();}bool istrue(node x){if (x.num[0] == ansnum[0] && x.num[1] == ansnum[1] && x.num[2] == ansnum[2] && x.num[3] == ansnum[3])return true;return false;}bool judge(node x){if (vis[x.num[0]][x.num[1]][x.num[2]][x.num[3]]) return false;return true;}node change(node a, int b){a.step ++;if (b < 4)// +{if (a.num[b] == 9) a.num[b] = 1;else a.num[b] ++;}else if (b < 8)// -{if (a.num[b%4] == 1) a.num[b%4] = 9;else a.num[b%4] = a.num[b%4] - 1;}else// 换{int tmp;b %= 4;tmp = a.num[b];a.num[b] = a.num[b+1];a.num[b+1] = tmp;}return a;}void bfs(){node now, next;while (!q.empty()){now = q.front();q.pop();if (istrue(now)){ans = now.step;return ;}for (int i = 0; i < 11; i++){next = change(now, i);if (judge(next)){vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = true;q.push(next);}}}}int main(){int T;scanf("%d", &T);while (T--){init();int a, b;node now;scanf("%d %d", &a, &b);for (int i=3; i>=0; i--){ansnum[i] = a % 10;now.num[i] = b % 10;a /= 10;b /= 10;}now.step = 0;q.push(now);vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]] = true;bfs();printf("%d\n", ans);}return 0;}
双向广搜:
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{int num[4];int step;};queue<node> q[2];int vis[2][10][10][10][10];int ansnum[4];int ans;void init(){ans = -1;memset(vis, -1, sizeof(vis));while (!q[0].empty()) q[0].pop();while (!q[1].empty()) q[1].pop();}bool istrue(node x, int i){if (vis[1^i][x.num[0]][x.num[1]][x.num[2]][x.num[3]] != -1)return true;return false;}bool judge(node x, int i){if (vis[i][x.num[0]][x.num[1]][x.num[2]][x.num[3]] != -1) return false;return true;}node change(node a, int b){a.step ++;if (b < 4)// +{if (a.num[b] == 9) a.num[b] = 1;else a.num[b] ++;}else if (b < 8)// -{if (a.num[b%4] == 1) a.num[b%4] = 9;else a.num[b%4] = a.num[b%4] - 1;}else// 换{int tmp;b %= 4;tmp = a.num[b];a.num[b] = a.num[b+1];a.num[b+1] = tmp;}return a;}void bfs(int j){node now, next;if (ans != -1 || q[j].empty()) return ;int first = q[j].front().step;while (!q[j].empty() && q[j].front().step == first){now = q[j].front();q[j].pop();if (istrue(now, j)){ans = vis[j][now.num[0]][now.num[1]][now.num[2]][now.num[3]] + vis[1^j][now.num[0]][now.num[1]][now.num[2]][now.num[3]];return ;}for (int i = 0; i < 11; i++){next = change(now, i);if (judge(next, j)){vis[j][next.num[0]][next.num[1]][next.num[2]][next.num[3]] = next.step;q[j].push(next);}}}}void db_bfs(){while (ans == -1){bfs(0);bfs(1);}}int main(){int T;scanf("%d", &T);while (T--){init();int a, b;node now0, now1;scanf("%d %d", &a, &b);for (int i=3; i>=0; i--){now0.num[i] = a % 10;now1.num[i] = b % 10;a /= 10;b /= 10;}now0.step = 0;q[0].push(now0);vis[0][now0.num[0]][now0.num[1]][now0.num[2]][now0.num[3]] = 0;now1.step = 0;q[1].push(now1);vis[1][now1.num[0]][now1.num[1]][now1.num[2]][now1.num[3]] = 0;db_bfs();printf("%d\n", ans);}return 0;}
- hdu1195 Open the Lock BFS 广搜
- hdu1195 Open the Lock(BFS)
- HDU1195:Open the Lock(BFS)
- HDU1195:Open the Lock(BFS)
- hdu1195 Open the Lock (BFS)
- hdu1195 Open the Lock 广搜BFS 四维数组标记
- hdu1195-Open the Lock-双向&&单向bfs
- hdu1195 Open the Lock(bfs水题)
- hdu1195 Open the Lock (bfs)
- hdu1195 Open the Lock
- hdu1195 Open the Lock
- hdu1195 Open the Lock
- hdu1195 Open the Lock
- hdu1195-Open the Lock
- HDU1195 Open the Lock
- HDU1195 Open the Lock
- hdu1195 Open the Lock
- HDU1195---Open the Lock
- tomcat定时重启和存活检测
- js日期对象(Date())与wcf日期(/Date(‘…’)/)相互转换
- 带边框的ImageView
- 图像复原
- Nk2221(DFS)
- hdu1195 Open the Lock BFS 广搜
- 如何验证win8应用程序是否可以完全部署到应用商店
- android自定义相机若干问题解决方案
- TAESDK2.0 工具安装和使用说明【新手学习】
- 命令行创建,删除,启动,停止Windows服务
- Visual Studio 推荐插件--高亮,跳转,map等
- 【ROCKY】5
- python的lambda使用
- light oj 1005 - Rooks(组合数学)