hdu 1195
来源:互联网 发布:cnnic证书 知乎 编辑:程序博客网 时间:2024/05/16 04:53
// Accepted 1195 78MS 364K 2126 B C++ robotcator #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 11000+50;struct password{ char data[5]; int step;};password s, e;int vis[maxn];int get_key(password x){ int sum = 0; for(int i = 0; i < 4; i ++) sum = sum*10 + x.data[i]-'0'; return sum;}void bfs(){ memset(vis, 0, sizeof(vis)); queue<password> q; s.step = 0; q.push(s); vis[get_key(s)] = 1; password cur, next; while(!q.empty()){ cur = q.front(); q.pop(); if(strcmp(cur.data, e.data) == 0){ printf("%d\n", cur.step); return ; } for(int i = 0; i < 3; i ++){ if(i == 0){ // 任意位上加1 for(int j = 0; j < 4; j ++){ strcpy(next.data, cur.data); next.data[j] = (next.data[j]-'0'+1) == 10 ? 1+'0' : next.data[j]+1; // 复制粘贴一定要注意代码间的区别 if(vis[get_key(next)] == 0) { next.step = cur.step + 1; q.push(next); vis[get_key(next)] = 1; // 要区分等号和赋值号 } } } else if(i == 1){ // 任意位上减1 for(int j = 0; j < 4; j ++){ strcpy(next.data, cur.data); next.data[j] = (next.data[j]-'0'-1) == 0 ? 9+'0' : next.data[j]-1; if(vis[get_key(next)] == 0) { next.step = cur.step + 1; q.push(next); vis[get_key(next)] = 1; } } } else if(i == 2){ // 交换 for(int j = 0; j < 3; j ++){ strcpy(next.data, cur.data); next.data[j] = cur.data[j+1]; next.data[j+1] = cur.data[j]; if(vis[get_key(next)] == 0) { next.step = cur.step + 1; q.push(next); vis[get_key(next)] = 1; } } } } }}int main(){ int t; scanf("%d", &t); while(t--){ scanf("%s %s", s.data, e.data); bfs(); } return 0;}
0 0
- hdu 1195
- HDU 1195
- HDU 1195
- hdu-1195
- hdu 1195 BFS
- hdu 1195 BFS
- HDU 1195(bfs)
- HDU 1195 双广
- HDU 1195 深搜
- hdu 1195 双向bfs
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- IPC与TCP/IP应用及比较
- 项目管理系统
- button设置边框
- android 单元测试
- Dev Treelist控件显示行号
- hdu 1195
- ios 获取所有字体列表-日记
- 九度_题目1384:二维数组中的查找
- 二叉树的操作(二叉树的创建、先序遍历--->先根、中序遍历---->先左、后续遍历--->后根)
- Alpha、Beta、RC、GA版本的区别
- javaMail发送邮件
- UVA 11401 - Triangle Counting 组合
- 问题"Caused by: java.lang.ClassNotFoundException:..."
- 基于UDP(面向无连接)的socket编程