HDU 1195 Open the Lock (BFS 剪枝)
来源:互联网 发布:软件设计师考试教程 编辑:程序博客网 时间:2024/06/03 22:00
HDU 1195
输入T,
然后每组数据给两个四位字符串(1 ~9),问从第一个字符串转化为第二个字符串最少需要几步。转化方式有两种,一是每个数字位加一或减一(9+1=1,1-1=9),二是相邻两位可以交换(最头最末两位不相邻)。
利用一个数组来剪枝。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;char a[5], b[5];struct node {char str[5];int num;};int book[10][10][10][10];void bfs() {queue <node> q;node cur, next;cur.num = 0, strcpy(cur.str, a);book[a[0] - '0'][a[1] - '0'][a[2] - '0'][a[3] - '0'] = 1;q.push(cur);while(!q.empty()) {cur = q.front();if(strcmp(cur.str, b) == 0) {printf("%d\n", cur.num);return;}q.pop();int i;for(i = 0; i < 4; i++) {strcpy(next.str, cur.str);cur.str[i] == '9' ? next.str[i] = '1' : next.str[i]++;next.num = cur.num + 1;if(book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] == 0) {book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] = 1;q.push(next);}strcpy(next.str, cur.str);cur.str[i] == '1' ? next.str[i] = '9' : next.str[i]--;next.num = cur.num + 1;if(book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] == 0) {book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] = 1;q.push(next);}}for(i = 0; i < 3; i++) {strcpy(next.str, cur.str);char tmp = next.str[i];next.str[i] = next.str[i + 1];next.str[i + 1] = tmp;next.num = cur.num + 1;if(book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] == 0) {book[next.str[0] - '0'][next.str[1] - '0'][next.str[2] - '0'][next.str[3] - '0'] = 1;q.push(next);}}}}int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", a); scanf("%s", b); memset(book, 0, sizeof(book)); bfs();} return 0;}
0 0
- HDU 1195 Open the Lock (BFS 剪枝)
- HDU 1195 Open the lock( BFS)
- hdu-1195--Open the Lock(BFS)
- hdu 1195 Open the Lock (BFS)
- 【HDU 1195】Open the Lock(BFS)
- HDU 1195 Open the Lock(bfs or 双向bfs)
- hdu 1195 Open the Lock bfs
- HDU 1195 Open the Lock BFS
- hdu 1195 Open the Lock (bfs)
- Hdu 1195 Open the Lock [Bfs]
- HDU 1195 Open the Lock (双向BFS)
- HDU-1195-Open the Lock(BFS)
- HDU 1195 Open the Lock BFS
- HDU 1195 Open the Lock(双向BFS)
- hdu 1195 Open the Lock(基础bfs)
- 双向BFS-->hdu 1195 Open the Lock
- hdu 1195 Open the Lock(BFS)
- HDU 1195 Problem:OPEN THE LOCK (bfs)
- 大数相乘
- 底层库把我坑惨了
- valgrind
- POJ 1007 DNA Sorting
- c++设计模式之原型模式(protoptype)
- HDU 1195 Open the Lock (BFS 剪枝)
- 编辑距离问题
- 关于aircrack-ng的一些学习笔记及问题
- JAVA常用集合框架用法详解基础篇三之Colletion子接口Set
- caffe源码分析:layer.hpp分析
- 机器学习实战(5)--SVM(Support vector machine)(六)--Python实现
- 【BZOJ1025】[SCOI2009]游戏【计数DP】【质因数分解】【置换】
- [bzoj3123][SDOI2013]森林
- struct、union的内存分配