FAFU- 1387 Astar寻妹纸 a*
2013-10-24 17:02
204 查看
http://acm.fafu.edu.cn/problem.php?id=1387
#include<stdio.h> #include<cmath> #include<string.h> #include<queue> using namespace std; const int maxn = 410; const int inf = 1<<29; int n,m; int map[maxn][maxn]; int xs[] = {0,1,1,1,0,-1,-1,-1}; int ys[] = {1,1,0,-1,-1,-1,0,1}; struct node { int x,y; int t,h,g,f; // t步数 h几何距离 g移动距离 f f = h + g; bool operator < ( const node &k ) const { return f > k.f; } }; node s,e; int getdis( node x ) //求几何距离 { return ( abs(x.x-e.x) + abs(x.y-e.y) )*10; } bool ok( node x ,int i) //判断对角 { if( !map[x.x + xs[i-1]][x.y + ys[i-1]] ) return false; if( i == 7 && !map[x.x][x.y+1]) return false; if( i != 7 && !map[x.x + xs[i+1]][x.y + ys[i+1]] ) return false; return true; } int Astar( node s,node e ) { node cnt,pos; priority_queue <node> que; s.t = 0; s.h = getdis(s); s.g = 0; s.f = s.h + s.g; que.push(s); while( !que.empty() ) { cnt = que.top(); que.pop(); map[cnt.x][cnt.y] = 0; //走过的点置零 if( cnt.x == e.x && cnt.y == e.y ) //找到终点 输出步数 return cnt.t; for( int i = 0; i < 8; i ++ ) { pos.x = cnt.x + xs[i]; pos.y = cnt.y + ys[i]; if( map[pos.x][pos.y] && pos.x >= 1 && pos.y <= n && pos.x >= 1 && pos.y <= m ) { if( i % 2 ) //为对角方向 if( ok(cnt,i) ) //判断对角能否走 pos.g = cnt.g + 14; else continue; else pos.g = cnt.g + 10; pos.t = cnt.t + 1; pos.h = getdis(pos); pos.f = pos.h + pos.g; que.push( pos ); } } } return 1; } int main() { //freopen("data.txt","r",stdin); while( scanf("%d%d",&m,&n) == 2 ) { for( int i = 1; i <= n; i ++ ) { for( int j = 1; j <= m; j ++ ) { scanf("%1d",&map[i][j]); } } scanf("%d%d%d%d",&s.y,&s.x,&e.y,&e.x); s.x++;s.y++;e.x++;e.y++; int ans = Astar( s,e ); printf("%d\n",ans-1); } return 0; }
相关文章推荐
- [iphone-game] 介绍 Astar (a*) A星算法在iPhone/iPad 上的应用
- 用Astar(A*)算法 实现的迷宫最短路。代码比较规范,可作为模板
- astar 集合的交与并
- 吴昊品工程级别软件项目 Round 2(附加)之(一个标准的DBMS系统的需求分析+数据库设计)(PS:感谢王露珠妹纸)
- 今天妹纸我,第一天开通博客。mark一下
- hdu 1387 Team Queue 优先队列
- fafu - 1074 质因数分解
- AStar 算法(erlang版本)
- 程序猿,你们这么拼是找不到妹纸的!
- 【fafu】1003 数字接龙(排序意识小谈)
- hdu 1387 Team Queue(队列的嵌套)
- 洛谷1387 最大正方形
- 混战在帝都的妹纸一枚,整理于Github上的工具合集
- 利用python程序浪漫安慰失恋妹纸
- 趣文:如果网络浏览器是妹纸
- POJ1324(Astar)
- 妹纸UI
- WHAT?写代码也能俘虏妹纸!