tjut 2822

#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;  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值