HDU 2612 Find a way (bfs)

66 篇文章 0 订阅
17 篇文章 1 订阅

简单题,,,

#include "string"
#include "iostream"
#include "cstdio"
#include "cmath"
#include "set"
#include "queue"
#include "vector"
#include "cctype"
#include "sstream"
#include "cstdlib"
#include "cstring"
#include "stack"
#include "ctime"
#include "algorithm"
#define pa pair<int,int>
#define Pi M_PI
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef long long LL;
#define INF 2<<20
const int M=55;

char mp[205][205];
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int vis[205][205];
int res[201][201];
int n,m;

struct node
{
    int x;
    int y;
    int step;
}st,sd;

bool judge(int x, int y)
{
	if(x<0||x>=n||y<0||y>=m||vis[x][y] == 1 || mp[x][y] == '#')
		return false;
	return true;
}

void bfs(int a,int b)
{
    queue<node>q;
    node ss;
    //ss.x=0;
   // ss.y=0;
    memset(vis,0,sizeof(vis));
    //vis[a][b]=1;
    q.push((node){a,b,0});
    while(!q.empty())
    {
       // q.pop();
       st=q.front();
       q.pop();
       //printf("(%d, %d)\n",st.x,st.y);
       if(mp[st.x][st.y]=='@')
       {
           res[st.x][st.y]+=st.step;
       }
       for(int i=0;i<4;++i)
        {
            int xx=st.x+dir[i][0];
            int yy=st.y+dir[i][1];

            if(judge(xx,yy))
            {
               // printf("(%d, %d)\n",xx,yy);
               // sd.x=xx;
               // sd.y=yy;
                vis[xx][yy]=1;
                q.push((node){xx,yy,st.step+1});
            }




        }

    }

     return ;


}

//void print(int x,int y)
//{
//    if(!x&& !y)
//    {
//         printf("(0, 0)\n");
//         return ;
//    }
//
//
//    for(int i=0;i<4;++i)
//    {
//        int xx=x+dir[i][0];
//        int yy=y+dir[i][1];
//        if(xx>=0 &&xx<5 &&yy>=0&&yy<5 &&vis[xx][yy]==vis[x][y]-1)
//        {
//            print(xx,yy);
//            printf("(%d, %d)\n",x,y);
//            return ;
//        }
//
//    }
//
//
//}

int main()
{
    int s1x,s1y,s2x,s2y;
	while(scanf("%d %d", &n, &m)!=EOF)
	{
		for(int i=0; i<n; i++)
		{
			scanf("%s", mp[i]);
			for(int j=0; j<m; j++)
			{
				if(mp[i][j] == 'Y')
				{
					s1x = i;
					s1y = j;
				}
				if(mp[i][j] == 'M')
				{
					s2x = i;
					s2y = j;
				}
			}
		}
		memset(res, 0, sizeof(res));

		bfs(s1x, s1y);
		bfs(s2x, s2y);

		int ans = INF;
		for(int i=0;i<n;i++)
			for(int j=0; j<m; j++)
			{
				if(mp[i][j] == '@' && res[i][j] != 0)
				{
					ans = min(ans, res[i][j]);
				}
			}
		cout<<ans*11<<endl;
	}
	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值