UVA1533 Moving Pegs(bfs+hash)
来源:互联网 发布:网络金融销售是诈骗吗? 编辑:程序博客网 时间:2024/04/28 05:10
题意:
一个跳棋,给定一个空位。求最少步数且字典序最小使得跳到最后棋盘只剩一个旗子,且在初始的空位上。
思路:
BFS + hash判重,这题的思路还是比较好想的,但是我看网络上面的代码,都是打表+bfs,我直接把状态转换为2维数组,对6个方向进行bfs。
AC代码
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int dx[] = {-1,-1, 0, 0, 1, 1};const int dy[] = {-1, 0,-1, 1, 0, 1};const int N = 1<<16;struct State { int grid[5][5], num; //the last pos and the step vector< pair<int,int> > path; //first from second to //init State() { memset(grid, false, sizeof(grid)); num = 0; path.clear(); } //create function State(int _grid[][5], int _num) { memcpy(grid, _grid, sizeof(grid)); num = _num; } //hash int Hash() { int sum = 0, cnt = 1; for(int i = 0; i < 5; i++) for(int j = 0; j <= i; j++) sum |= grid[i][j] << cnt++; return sum; }};vector< pair<int,int> > ans;int state[5][5], vis[N];int empty;void init() { memset(vis, false, sizeof(vis)); memset(state, -1, sizeof(state)); int cnt = 1; for(int i = 0; i < 5; i++) { for(int j = 0; j <= i; j++) { if(empty != cnt++) state[i][j] = 1; else state[i][j] = 0; } }}int trans(int x, int y) { return x*(x+1)/2 + (y+1);}bool check(int grid[][5]) { int pos = 0; for(int i = 0; i < 5; i++) { for(int j = 0; j <= i; j++) { if(grid[i][j]) pos = trans(i, j); } } return pos == empty;}bool overBound(int x, int y) { if(x < 0 || x >= 5 || y < 0 || y >= 5) return true; return false;}bool bfs() { //init queue<State> que; que.push(State(state, 14)); State front = que.front(), tmpState; vis[front.Hash()] = true; int nx, ny, len; while(!que.empty()) { front = que.front(); que.pop(); if(front.num == 1 && check(front.grid)) { ans = front.path; return true; } for(int x = 0; x < 5; x++) { for(int y = 0; y <= x; y++) { //num if(front.grid[x][y] == 0) continue; for(int i = 0; i < 6; i++) { //direction nx = x + dx[i], ny = y + dy[i]; if(!overBound(nx, ny)) { len = 1; tmpState = front; while(tmpState.grid[nx][ny] == 1 && !overBound(nx, ny)) { tmpState.grid[nx][ny] = 0; len++; nx += dx[i], ny += dy[i]; } } if(tmpState.grid[nx][ny] == -1 || overBound(nx,ny) || len < 2) continue; tmpState.grid[nx][ny] = 1; tmpState.grid[x][y] = 0; tmpState.num -= (len-1); tmpState.path.push_back(make_pair(trans(x,y), trans(nx,ny))); int tmp = tmpState.Hash(); if(!vis[tmp]) { vis[tmp] = true; que.push(tmpState); } } } } } return false;}int main() {// freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while(T--) { scanf("%d", &empty); init(); if(!bfs()) { printf("IMPOSSIBLE\n"); }else { printf("%d\n", ans.size()); printf("%d %d", ans[0].first, ans[0].second); for(int i = 1; i < ans.size(); i++) printf(" %d %d", ans[i].first, ans[i].second); puts(""); } } return 0;}
0 0
- UVA1533 Moving Pegs(bfs+hash)
- UVa1533 - Moving Pegs(错在什么地方)
- uva 1533 - Moving Pegs(BFS)
- UVALive - 2093 Moving Pegs bfs
- UVA 1533 - Moving Pegs 状态压缩 + bfs
- uva1533(bfs)
- UVA 1533 - Moving Pegs (广搜+hash判重)
- UVA - 1533 Moving Pegs
- UVA - 1533 (Moving Pegs)
- UVA 12569 Moving Pegs
- Moving Pegs UVA
- UVa 1533 Moving Pegs 解题报告(暴力+码力)
- UVa 1533:Moving Pegs(迭代加深搜索)
- POJ Holedox Moving BFS hash判重
- POJ 1324 Holedox Moving(bfs + hash)
- UVa #1533 Moving Pegs (习题7-12)
- 1533Moving Pegs[暴力+打表]
- POJ1324 Holedox Moving(状态压缩+BFS)
- $apply(),$digest()用法
- Ubuntu下,清屏等终端常用命令
- 使用ffmpeg将yuv编码成h264时有大小限制,太小的数据源编码会失败。
- Add Two Numbers
- YII配置环境
- UVA1533 Moving Pegs(bfs+hash)
- 树的建立与层序遍历
- 1.初识java多线程《java编程思想笔记》
- 每日一得--plsql连接oracle服务器方式
- 新添加的文章标题
- 查询同一张表和更新同一张表的时候
- 常用网址mark
- pyqt事件循环
- p122 第三章 第20题 潘璠