tjut 2822
2016-08-07 17:23
155 查看
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <string> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; #define maxn 1005 #define LL long long int cas=1,T; char mapp[maxn][maxn]; int vis[maxn][maxn]; struct Node { char l; int x,y,time; friend bool operator < (const Node&a,const Node&b) {return a.time > b.time;} }; int n,m,startx,starty,endx,endy; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; bool check(int x,int y) { if (x<1 || x>m || y<1 || y>n || vis[x][y]) return true; return false; } int bfs() { memset(vis,0,sizeof(vis)); priority_queue<Node> q; Node s,temp; s.x=startx; s.y=starty; s.time=0; vis[s.x][s.y]=1; q.push(s); while (!q.empty()) { s = q.top();q.pop(); if (s.x==endx && s.y==endy) return s.time; for (int i = 0;i<4;i++) { temp.x = s.x+dir[i][0]; temp.y = s.y+dir[i][1]; if (check(temp.x,temp.y)) continue; if (mapp[temp.x][temp.y]=='X') temp.time = s.time+0; else temp.time = s.time+1; vis[temp.x][temp.y]=1; q.push(temp); } } return -1; } int main() { while (scanf("%d%d",&m,&n)!=EOF && m && n) { for (int i = 1;i<=m;i++) scanf("%s",mapp[i]+1); scanf("%d%d",&startx,&starty); scanf("%d%d",&endx,&endy); int ans = bfs(); printf("%d\n",ans); } //freopen("in","r",stdin); //scanf("%d",&T); //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC); return 0; }
相关文章推荐
- mob 之 ShareSDK第三方分享
- C++中数字与字符串之间的转换
- Java的类(class)、包(package)和接口(interface)
- Spring
- spring schema自定义
- 驱动开发用到的派遣函数序号(wdm.h)
- 字符串:单词查找树
- JDBC入门
- Java 包(package)
- Linux服务-SSHD
- 字符串:字符串排序
- sys.stdout.flush()在Linux和Windows系统下的作用
- 数据结构学习笔记8——简单二叉树的实现与遍历
- Linux新手要了解的十个知识点
- 设计模式之抽象工厂模式
- 【回溯法】马拦过河卒_动态规划
- JS 数字转换为大写金额
- jqueryui 拖拽
- HDU 2571 命运
- vim 设置