HDU 1043 双向广搜 八数码 康托展开 逆序数
2015-11-15 21:47
344 查看
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; char c; int index[10] = {1,1,2,6,24,120,720,5040,40320}; int dir[4][2] = {1,0,-1,0,0,1,0,-1}; int vis[400000],found,state,father1[400000],father2[400000],move1[400000],move2[400000]; struct node { int state,loc; char map[10]; }s1,s2; queue<node>q1; queue<node>q2; int ct(char map[]) { int ans = 0; for(int i = 0; i < 9; i++) { int num = 0; for(int j = i + 1; j < 9; j++) { if(map[i] > map[j]) num++; } ans += num * index[8 - i]; } return ans; } void expend(queue<node> &q,int flag) { int x,y,xx,yy,k; node cur,end; cur = q.front(); q.pop(); k = cur.loc; x = k / 3; y = k % 3; for(int i = 0; i < 4; i++) { xx = x + dir[i][0]; yy = y + dir[i][1]; if(xx >= 0 && yy >= 0 && xx < 3 && yy < 3) { end = cur; end.loc = xx*3 + yy; end.map[cur.loc] = cur.map[end.loc]; end.map[end.loc] = 9; end.state = ct(end.map); if(flag == 1 && vis[end.state] != 1) { move1[end.state] = i; father1[end.state] = cur.state; if(vis[end.state] == 2) { found = 1; state = end.state; return; } vis[end.state] = 1; q.push(end); } else if(flag == 2 && vis[end.state] != 2) { move2[end.state] = i; father2[end.state] = cur.state; if(vis[end.state] == 1) { found = 1; state = end.state; return; } vis[end.state] = 2; q.push(end); } } } } int ReverseOrder(char map[]) { int ans = 0; for(int i = 0; i< 9; i++) { if(map[i] == 9) continue; for(int j = i + 1; j < 9; j++) { if(map[j] == 9) continue; if(map[i] > map[j]) ans++; } } return ans; } void read() { int i = 0; if(c == 'x') { s1.map[i++] = 9; s1.loc = 0; } else if(c < '9' && '0' < c) { s1.map[i++] = c - '0'; } while(scanf("%c",&c)) { if(c == 'x') { s1.map[i++] = 9; s1.loc = i - 1; } else if(c < '9' && '0' < c) { s1.map[i++] = c - '0'; } if(i == 9) return; } } void bfs2() { while(!q1.empty()) q1.pop(); while(!q2.empty()) q2.pop(); q1.push(s1); q2.push(s2); while(!q1.empty() || !q2.empty()) { if(!q1.empty()) expend(q1,1); if(found) return; if(!q2.empty()) expend(q2,2); if(found) return; } } void path1(int st) { if(father1[st] != -1) { path1(father1[st]); if(move1[st] == 0) printf("d"); else if(move1[st] == 1) printf("u"); else if(move1[st] == 2) printf("r"); else if(move1[st] == 3) printf("l"); } return; } void path2(int st) { while(father2[st] != -1) { if(move2[st] == 0) printf("u"); else if(move2[st] == 1) printf("d"); else if(move2[st] == 2) printf("l"); else if(move2[st] == 3) printf("r"); st = father2[st]; } } int main() { // freopen("t.txt","r",stdin); int i; while(~scanf(" %c",&c)) { found = 0; memset(vis,0,sizeof(vis)); if(c=='x') { s1.map[0]=9; s1.loc=0; } else s1.map[0]=c-'0'; for(i=1;i<9;i++) { scanf(" %c",&c); if(c=='x') { s1.map[i]=9; s1.loc=i; } else s1.map[i]=c-'0'; } s1.state = ct(s1.map); vis[s1.state] = 1; father1[s1.state] = -1; if(s1.state == s2.state) { puts(""); continue; } if(ReverseOrder(s1.map) % 2 == 1) { printf("unsolvable\n"); continue; } for(i = 0; i < 9; i++) { s2.map[i] = i + 1; } s2.loc = 8; s2.state = ct(s2.map); vis[s2.state] = 2; father2[s2.state] = -1; bfs2(); if(found == 0) { printf("unsolvable\n"); continue; } path1(state); path2(state); printf("\n"); } }
相关文章推荐
- 高级软件工程总结
- 匿名者黑客组织
- 一些快捷键&工具的用法收集
- 5233杨光--第十周学习总结
- nginx正则说明
- JSP中使用 jquery.tmpl.js问题
- Javascript执行效率小结
- C# ajax 文件提交
- sicily 1082. MANAGER
- 二叉树的操作
- ArcEngine保存mxd的方法
- 《黑客与画家》读后感————一堆个人谬论
- [转]reids客户端 redis-cli用法
- Squid代理服务
- 信息安全系统设计基础第十周学习总结
- 信息安全系统设计基础第十周学习总结
- Jamil+c#—实现邮件的发送
- SWT获取windows系统窗口颜色
- 服务号-获取用户基本信息
- LVS DR 负载均衡集群