您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: