HDU 1242 广搜模板
2013-12-06 17:36
429 查看
广搜模板
#include "stdio.h" #include "string.h" #include "math.h" #include "queue" using namespace std; struct node { int x,y,step; friend bool operator<(node n1,node n2) { return n2.step<n1.step; } }; int dir[4][2]={1,0,-1,0,0,1,0,-1}; int n,m,x_s,y_s; int map[210][210]; int judge(int x,int y) { if (x<0 || x>=n || y<0 || y>=m) return 0; if (map[x][y]==-1) return 0; return 1; } int bfs() { priority_queue<node>q; node cur,next; int i; cur.x=x_s; cur.y=y_s; cur.step=0; q.push(cur); while (!q.empty()) { cur=q.top(); q.pop(); for (i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; if (judge(next.x,next.y)==0) continue; next.step=cur.step+1; if (map[next.x][next.y]==1) next.step++; if (map[next.x][next.y]==2) return next.step; map[next.x][next.y]=-1; q.push(next); } } return -1; } int main() { int i,ans,j; char str[1010]; while (scanf("%d%d",&n,&m)!=EOF) { getchar(); memset(map,0,sizeof(map)); for (i=0;i<n;i++) { gets(str); for (j=0;str[j];j++) { if (str[j]=='a') { x_s=i; y_s=j;} else if (str[j]=='r') map[i][j]=2; else if (str[j]=='#') map[i][j]=-1; else if (str[j]=='x') map[i][j]=1; } } ans=bfs(); if (ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- Orge 安装
- 【Linux】Linux平台Cpu使用率的计算
- 数据库锁表
- 扫雷小游戏
- 从事企业级的项目开发
- 加载listview配合适配器的使用
- Highcharts + jQuery + Servlet 实现从后台获取JSON实时刷新图表
- android 自定义控件库(jar)时属性不能识别的来看吧
- 警惕常用这6种方式吃饭必得癌
- Selenium RC 环境搭建(eclipse)
- ASP.net 关于TextBox的TextMode=“PassWord”时的动态赋值
- java中关键字volatile的作用
- java基础知识2-编译单元
- windows phone开发之准备篇
- 机构间清算导入至核心
- Unity实现手机震动
- 嵌入式ARM系统开发基础
- 我的android 第22天 - Uri介绍
- CWnd与HWND的区别与转换
- Python 抓取google链接代码