poj2243
来源:互联网 发布:excel数据保存vba代码 编辑:程序博客网 时间:2024/05/21 11:03
poj2243 A*算法+优先队列优化#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<iostream>#include<queue>#define INF 10000000using namespace std;int visit1[10][10],visit2[10][10],g[10][10],h[10][10],di[8][2]={2,1,1,2,-2,1,-1,2,-1,-2,-2,-1,1,-2,2,-1};struct node{ int x,y; friend bool operator <(node a,node b) { return (g[a.x][a.y]+h[a.x][a.y]>g[b.x][b.y]+h[b.x][b.y]); }};priority_queue<node>q1;int change(char c){ if(c>='0'&&c<='9') return c-'0'; else return c-'a'+1;}int main(){ char s1[5],s2[5]; int i,j,k,l,sx,sy,tx,ty,xx,yy; while(scanf("%s%s",s1,s2)!=EOF) { memset(visit1,0,sizeof(visit1)); memset(visit2,0,sizeof(visit2)); sx=change(s1[0]);sy=change(s1[1]); tx=change(s2[0]);ty=change(s2[1]); //printf("%d %d %d %d\n",sx,sy,tx,ty); for(i=1;i<=8;i++) for(j=1;j<=8;j++) { g[i][j]=INF; h[i][j]=INF; } g[sx][sy]=0; h[sx][sy]=max(abs(sx-tx),abs(sy-ty))/2; node a; a.x=sx;a.y=sy; q1.push(a); visit1[sx][sy]=1; while(!q1.empty()) { node b; b=q1.top(); q1.pop(); //printf("*%d %d\n",b.x,b.y); if(visit2[b.x][b.y]) continue; if(b.x==tx&&b.y==ty) break; for(i=0;i<8;i++) { xx=b.x+di[i][0];yy=b.y+di[i][1]; if(xx>=1&&xx<=8&&yy>=1&&yy<=8&&visit2[xx][yy]==0) { if(visit1[xx][yy]) { if(g[b.x][b.y]+1<g[xx][yy]) { g[xx][yy]=g[b.x][b.y]+1; node c; c.x=xx; c.y=yy; q1.push(c); } } else { g[xx][yy]=g[b.x][b.y]+1; h[xx][yy]=max(abs(sx-tx),abs(sy-ty))/2; node c; c.x=xx; c.y=yy; q1.push(c); } } } visit2[b.x][b.y]=1; } printf("To get from %s to %s takes %d knight moves.\n",s1,s2,g[tx][ty]); while(!q1.empty()) q1.pop(); } return 0;}
0 0
- POJ2243
- poj2243
- poj2243
- poj2243
- poj2243
- poj1915 & poj2243 Knight Moves
- poj2243 Knight Moves
- poj2243 Knight Moves (BFS)
- POJ2243 简单BFS
- POJ2243 A*算法BFS
- 1poj2243(bfs)
- poj2243 【搜索】【深度优先】
- poj2243 广搜第一道
- POJ2243 Knight Moves
- poj2243+poj1915骑士问题
- BFS POJ2243- Dario
- poj2243~双向bfs
- poj2243 hdu 1372 bfs
- 网站加载优化
- 《程序员面试宝典》读书笔记
- 读贺老师逆袭大学前三章有感
- cortex A9 中断模式遇到的问题
- codeforces D. Ice Sculptures 题解
- poj2243
- poj1017
- HDU 2044.一只小蜜蜂
- strstr函数用法小结
- final关键字
- 术语
- js 图片轮转
- 在unity中建立最小的shader(Minimal Shader)
- 开始记录学习过程