HDU 2612 Find a way (广搜)

【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=2612

题目意思

两个人约在肯德基见面,但是肯德基有多家,所以让你找出两人时间和最少的一家肯德基。每次移动是11分钟,‘@’为肯德基,‘。’为路,‘#’为墙

解题思路

跑两遍广搜,记录到的时间,找出两次和最小的就可以了

代码部分


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
struct node
{
    int x,y,step;
}s1,s2;
char maps[205][205];
int vis[205][205];
int step[205][205];  ///记录步数 
int dir[4][2]={1,0,-1,0,0,-1,0,1};  ///方向 
int Max = inf;
int n,m;
void bfs (node s, int k)
{
    queue <node>q;
    q.push(s);
    vis[s.x][s.y] = 1;
    while (!q.empty())
    {
        node t,tt;
        t = q.front();
        q.pop();
        tt.step = t.step+11;
        if (maps[t.x][t.y] == '@')  ///到肯德基时加上步数 
        {
            step[t.x][t.y] += t.step;
            if (step[t.x][t.y] < Max && k == 2)  ///如果是第二个人到看下是否跟新最小和 
                Max = step[t.x][t.y];
        }
        for (int i = 0; i < 4; i++)
        {
            tt.x = t.x+dir[i][0];
            tt.y = t.y+dir[i][1];

            if (tt.x >= 0 && tt.y >= 0 && tt.x < n && tt.y < m && !vis[tt.x][tt.y] && maps[tt.x][tt.y] !='#')
            {
                vis[tt.x][tt.y] = 1;
                q.push(tt);
            }
        }
    }
}
int main()
{
    while (cin>>n>>m)
    {
        Max = inf; 
        memset(step,0,sizeof(step));
        memset(vis,0,sizeof (vis));
        for (int i =0; i < n; i++)
            for (int j = 0; j < m;j++)
            {
                cin>>maps[i][j];
                if (maps[i][j] == 'Y')
                {
                    s1.x = i;
                    s1.y = j;
                    s1.step = 0;
                }
                if (maps[i][j] == 'M')
                {
                    s2.x = i;
                    s2.y = j;
                    s2.step = 0;
                }
            }
        bfs(s1,1);
        memset(vis,0,sizeof(vis));
        bfs(s2,2);
        cout << Max << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值