搜索 Seven Puzzle (AOJ 0121 bfs)
来源:互联网 发布:喷绘用什么软件做 编辑:程序博客网 时间:2024/05/22 11:49
#include<stdio.h>#include<algorithm>#include<string.h>#include<map>#include<iostream>#include<queue>using namespace std;map<string,int>gmap;int a[2][4];int b[2][4];int vis[4][2]={1,0,0,1,0,-1,-1,0};struct text{ int a[2][4];}st,tt,start;queue<text> q;char s[9];char ss[9];int temp=0;int cnt=1;bool check(int x,int y){ if(x<0||y<0||x>=2||y>=4) return false; return true;}void bfs(){ while(!q.empty()) { st=q.front(); q.pop(); int x,y; for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { if(st.a[i][j]==0) { x=i; y=j; } a[i][j]=st.a[i][j]; ss[i*4+j]=st.a[i][j]+'0'; } } for(int i=0;i<4;i++) { for(int j=0;j<2;j++) { int k=x+vis[i][0]; int l=y+vis[i][1]; if(check(k,l)) { int xx=a[x][y]; a[x][y]=a[k][l]; a[k][l]=xx; for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { s[i*4+j]=a[i][j]+'0'; tt.a[i][j]=a[i][j]; } } s[8]='\0'; if(!gmap[s]>0) { gmap[s]=gmap[ss]+1; q.push(tt); } xx=a[x][y]; a[x][y]=a[k][l]; a[k][l]=xx; } } } }}int main(){ for(int i=0;i<8;i++) a[i/4][i%4]=i; for(int i=0;i<2;i++) { for(int j=0;j<4;j++) start.a[i][j]=a[i][j]; } for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { s[i*4+j]=start.a[i][j]+'0'; } } s[8]='\0'; gmap[s]=1; q.push(start); bfs();//预处理 将结果用map<string,int >存储 while(~scanf("%d",&a[0][0])) { for(int i=1;i<4;i++) scanf("%d",&a[0][i]); for(int i=0;i<4;i++) scanf("%d",&a[1][i]); for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { s[i*4+j]=a[i][j]+'0'; } } s[8]='\0'; // puts(s); printf("%d\n",gmap[s]-1); }}
0 0
- 搜索 Seven Puzzle (AOJ 0121 bfs)
- Seven Puzzle (AOJ 0121 bfs)
- Aoj 0121 Seven Puzzle【bfs】
- AOJ 0121 Seven Puzzle (bfs)
- AOJ 0121:Seven Puzzle(bfs)
- Seven Puzzle AOJ 0121宽度优先搜索
- AOJ 0121 Seven Puzzle
- AOJ--0121 Seven Puzzle
- AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)
- AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)
- Seven Puzzle (Aizu 0121 反向搜索bfs)
- AOJ 0121 Seven Puzzle {广度优先搜索}(*)
- [挑战程序设计竞赛] AOJ 0121 - Seven Puzzle
- G - Seven Puzzle(7宫格)AOJ 0121
- Aizu 0121 Seven Puzzle
- AOJ0121——Seven Puzzle(BFS)
- AOJ 0121 bfs
- zoj 3814 Sawtooth Puzzle(搜索-bfs)
- 教你透彻了解红黑树
- hdu2795
- Linux命令学习之一
- 跟我一起写 Makefile
- mysql基本语法及面试常见问题
- 搜索 Seven Puzzle (AOJ 0121 bfs)
- Hadoop1.2.1 完全分布式集群搭建实操笔记
- CodeForces 545B
- UIButton
- HTTP协议-无状态性
- Android之线程池深度剖析
- hpu 1695 问题 A 一道签到题(KMP算法的考察)
- 【git】git bash 启动默认路径
- 并查集之找祖先的模板