UVa 10085 - The most distant state
来源:互联网 发布:淘宝实名制 编辑:程序博客网 时间:2024/05/20 18:03
八数码问题的变形,bfs+哈希,打印路径时花了点时间,用哈希表next数组的原理进行回退就可以将问题解决。
代码如下:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;const int MAXSIZE = 1000003;const int dx[4] = {-1, 1, 0, 0};const int dy[4] = {0, 0, -1, 1};int back[MAXSIZE], path[MAXSIZE][9], dist[MAXSIZE];int head[MAXSIZE], next[MAXSIZE];char ac[MAXSIZE], save[100];int Hash(int s){ int v = 0; for(int i=0; i<9; i++) v = v * 10 + path[s][i]; return v % MAXSIZE;}int insert(int s){ int h = Hash(s); int u = head[h]; while(u) { if(!memcmp(path[u], path[s], sizeof(path[s]))) return 0; u = next[u]; } next[s] = head[h]; head[h] = s; return 1;}int bfs(){ memset(head, 0, sizeof(head)); int front = 1, rear = 2; while(front < rear) { int z; for(z=0; z<9; z++) if(!path[front][z]) break; int x = z/3, y = z%3; for(int i=0; i<4; i++) { int newx = x + dx[i], newy = y +dy[i]; int newz = newx * 3 +newy; if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3) { memcpy(path[rear], path[front], sizeof(path[front])); path[rear][newz] = path[front][z]; path[rear][z] = path[front][newz]; dist[rear] = dist[front] + 1; if(insert(rear)) { back[rear] = front; switch(i) { case 0: ac[rear] = 'U'; break; case 1: ac[rear] = 'D'; break; case 2: ac[rear] = 'L'; break; case 3: ac[rear] = 'R'; break; } rear++; } } } ++front; } return rear - 1;}int main(){#ifdef test freopen("sample.txt", "r", stdin);#endif int num, goal; scanf("%d", &num); for(int i=1; i<=num; i++) { int sum = 0; back[1] = 0; for(int j=0; j<9; j++) scanf("%d", &path[1][j]); goal = bfs(); printf("Puzzle #%d\n", i); for(int j=0; j<3; j++) { printf("%d", path[goal][j*3]); for(int k=1; k<3; k++) printf(" %d", path[goal][j*3+k]); puts(""); } while(goal) { save[++sum] = ac[goal]; goal = back[goal]; } for (int i=sum-1; i; i--) printf("%c", save[i]); printf("\n\n"); } return 0;}
- uva 10085 10085 - The most distant state
- UVA 10085 - The most distant state
- uva 10085 - The most distant state
- UVa 10085 - The most distant state
- UVA 10085 - The most distant state
- uva 10085-The most distant state
- UVa:10085 The most distant state
- uva 10085 The most distant state
- uva:10085 - The most distant state
- UVa 10085 - The Most Distant State
- uva 10085 The most distant state
- uva 10085The most distant state
- UVA 10085 The most distant state
- UVA - 10085 The most distant state
- uva 10085 The most distant state
- UVA The most distant state
- 10085 - The most distant state
- UVa 10085-The most distant state(BFS,Eight).cpp
- 用@Resource注解完成属性装配
- PHP获取根目录方法
- java 入门程序 之array(数组)
- Android上使用SkBitmap 进行屏幕截图
- 关于table view的content mode的个人看法
- UVa 10085 - The most distant state
- JQuery下关于$.Ready()的分析
- Java程序如何生成Jar、exe及安装文件
- 快速排序1--HoardSort/Sort in CLRS
- android 2D skia库 简单应用
- 天天记录 - 自定义ViewGroup使用Adapter提供视图
- FFMPEG for android 文章汇总
- 编写高效的android代码(译文)
- 同类炸弹Ⅱ(9900炮) 游戏机 玩法技巧