hdu2612

刚开始想着不标记@点了,没想到超时了。。。(@点真多)
又开个数组,把@的点标记,A了

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 202
struct node{                         //队列,广搜;
	int x,y,step;
};
char map[N][N];
int dp[N][N],p[N][N];
int dir[4][2]={0,1, 0,-1, 1,0, -1,0};
int a,b,f,res;
bool judge(int x,int y)
{
    if(x>=0 && x<a && y>=0&& y<b&&p[x][y]!=f&&map[x][y]!='#')
        return 1;
    return 0;
}
int bfs(int x,int y)
{
    queue<node>q;
    node cur,next;
    int k;
    cur.x=x;
    cur.y=y;
    cur.step=0;
    q.push(cur);
    p[x][y]=f;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        next.step=cur.step+1;
        for(k=0;k<4;k++)
        {
            next.x=x=cur.x+dir[k][0];
            next.y=y=cur.y+dir[k][1];
           if(judge(x,y))
            {
                if(map[x][y]=='@')
                {
                    if(dp[x][y]==0)
                        dp[x][y]=next.step;
                    else if(f==-2&&dp[x][y]+next.step<res)
                        res=dp[x][y]+next.step;
                }
                p[x][y]=f;
                q.push(next);
            }
        }
    }
    return -1;
}
int main ()
{
    int x1,y1,x2,y2,i,k;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(p,0,sizeof(p));
        for(i=0;i<a;i++)
        {
            scanf("%s",map[i]);
            for(k=0;k<b;k++)
            {
                if(map[i][k]=='M') {x1=i;y1=k;}
                if(map[i][k]=='Y') {x2=i;y2=k;}
            }
        }
        res=0xfffffff;
         f=-1;
         bfs(x1,y1);
         f=-2;
         bfs(x2,y2);
         printf("%d\n",res*11);
    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值