HDU-1043 Eight 八数码问题
来源:互联网 发布:艾滋病感染概率 知乎 编辑:程序博客网 时间:2024/05/19 16:34
http://acm.hdu.edu.cn/showproblem.php?pid=1043
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<vector>#include<queue>#include<map>#include<algorithm>using namespace std;const int maxn = 105;const int inf = 1<<30;int xs[] = {0,1,0,-1};int ys[] = {1,0,-1,0};int goal[10] = {1,2,3,4,5,6,7,8,9};char dir[] = { 'l','u','r','d' };int mod[] = {100000000,10000000,1000000,100000,10000,1000,100,10,1};int s[10];int counts;int pos = 0;string path[192000];map<int,int>mark;struct node{int p;int k;int id;};int fun( node x,int k ){int ans = x.p,w,temp;w = (ans/mod[k])%10; //ans += (9-w)*mod[k];ans -= (9-w)*mod[x.k];return ans;}void BFS(){node cur,cnt;queue<node>que;cur.p = 123456789; cur.k = 8;cur.id = 0;que.push(cur);while ( !que.empty() ){cur = que.front(); que.pop();int x = cur.k/3,y = cur.k%3;for( int i = 0; i < 4; i ++ ){int xx = x + xs[i];int yy = y + ys[i];cnt.k = xx*3 + yy;if( xx >= 0 && xx < 3 && yy >= 0 && yy < 3 ){cnt.p = fun( cur,cnt.k );if( mark[cnt.p] )continue;path[++pos] = dir[i] + path[cur.id];cnt.id = pos;mark[cnt.p] = pos;counts ++;que.push( cnt );}}}}int main(){//freopen("data.txt","r",stdin);char str[50];int num;BFS();while ( gets(str) ){num = 0;for ( int i = 0; str[i]; i ++ ){if( str[i] == 'x' )num= num *10 + 9;else if( str[i] != ' ' )num = num*10 + str[i] - '0';}int ans = mark[num];if( ans )cout<<path[ans]<<endl;elseputs("unsolvable");}return 0;}
- hdu 1043 eight 八数码问题
- HDU-1043 Eight 八数码问题
- hdu 1043 Eight 经典八数码问题
- HDU 1043 eight 八数码
- 八数码--HDU - 1043 Eight
- HDU 1043 Eight 八数码
- hdu 1043 eight 八数码问题 bfs 和 A*
- HDU 1043 / POJ 1077 Eight(八数码问题)
- HDU 1043 Eight 八数码问题 A*搜索 启发式算法
- hdu 1043 Eight(八数码问题 高级搜索: A* 搜索)
- hdu 1043 Eight(八数码)
- HDU 1043 eight (Astar 八数码)
- POJ 1077 Eight && HDU 1043 Eight 八数码问题(A*算法)
- 1077 Eight 八数码问题
- HDU1043 eight 八数码问题
- HDU1043 Eight 八数码问题
- HDU 1043 Eight (经典八数码问题,BFS+状态枚举+伪哈希)
- HDU 1043 Eight(经典八数码问题)对比POJ 1077(bfs)
- uva11396 - Claw Decomposition
- 多线程时钟模拟
- leetcode之Insertion Sort List
- linux下实现串口通讯
- JSF —— Facelets 标签
- HDU-1043 Eight 八数码问题
- Spirng3基于注解(annotation)整合ehcache 使用页面缓存、对象缓存
- 数据库索引的作用和优点缺点
- 最小步数nyoj--92
- 算法实战5:多种数据结构实现四则运算
- 相似度计算
- Win7 安装Apache 2.2.4报错:<OS 5>拒绝访问. :Failed to open the WinNT service manager
- 注册表空间
- javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)