kuangbin 专题一 简单搜索 (HDU 2612)Find a way

Find a way

Time limit1000 ms
Memory limit32768 kB
OSWindows

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.

  • Input
The input contains multiple test cases. 
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character. 
‘Y’ express yifenfei initial position. 
‘M’    express Merceki initial position. 
‘#’ forbid road; 
‘.’ Road. 
‘@’ KCF 
  • Output
For each test case output the minimum total time that both yifenfei and Merceki to 
arrival one of KFC.You may sure there is always have a KFC that can let them meet.
  • Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
  • Sample Output
66
88
66

经过一年的杭州学习,伊芬菲终于来到了家乡宁波。离开宁波一年,伊芬菲有很多人要见,尤其是他的朋友美世。伊芬菲的家在农村,美世的家在市中心。于是,伊芬菲和美世安排在肯德基见面。宁波有很多肯德基,他们想选择一个让总时间最短的。现在给你一张宁波地图,已知伊芬菲和美世上下左右移动到相邻的道路上要花费11分钟。

  • 输入
输入包含多个测试用例
每个测试用例包括两个整数n,m(2<=n,m<=200)
接下来的n行中,每行包含m个字符
 "Y"表示伊芬菲的初始位置
 "M"表示美世的初始位置
 "#"死路
 "."道路
 "@"KFC
  • 输出
对于每一个测试用例,输出伊芬菲和美世到达其中一家KFC的最短总时间
可以确定,总有一家可以让他们都到达的KFC

样例输入输出如上
这里有我大哥发现个bug
就是测试例子中没有一方到不了的KFC或者有但时间比总时间长
我之前只用一个数组存到KFC的时间,两个BFS间没有清零,就是直接加然后AC了
那个只用15ms就过了…但一想不符合题意所以我新改了一版

Time46ms
Memory1820kB
Length1631
LangG++
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct zb{
  int x;
  int y;
}a,b;
int m, n;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};/*第一次不用复制粘贴 建了个方向数组*/
int step[205][205];
char map[205][205];
int  KFC_Y[205][205], KFC_M[205][205];

int judge (zb d) {
  int f = 1;
  if(0 > d.x || d.x >= n || 0 > d.y || d.y >= m) f = 0;
  else if(step[d.x][d.y]) f = 0;
  else if (map[d.x][d.y] == '#') f = 0;
  return f;
}

void bfs(zb c, int KFC[205][205]){
    zb d;
    queue<zb>q;
    q.push(c);
    while (!q.empty()) {
      c = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        d.x = c.x + dir[i][0];
        d.y = c.y + dir[i][1];
        if(!judge(d)) continue;
        step[d.x][d.y] = step[c.x][c.y] + 1;
        if(map[d.x][d.y] == '@') KFC[d.x][d.y] = step[d.x][d.y];/*单独存了下到KFC的时间*/
        q.push(d);
      }
    }
  }

int main(int argc, char const *argv[]) {
  while (~scanf("%d %d", &n, &m)) {
    int yi, yj, mi, mj;
    memset(KFC_M, 0, sizeof(KFC_M));
    memset(KFC_Y, 0, sizeof(KFC_Y));
    memset(step, 0, sizeof(step));
    for (int i = 0; i < n; i ++) {
      scanf("%s", map[i]);
      for(int j = 0;j < m;j ++) {
        if(map[i][j] == 'Y') a.x = i, a.y = j;
        if(map[i][j] == 'M') b.x = i, b.y = j;/*记一下起点位置*/
      }
     }
     bfs(a, KFC_Y);
     memset(step, 0, sizeof(step));/*这个一定不要忘ya*/
     bfs(b, KFC_M);
     int min = 0x3f3f3f3f;
     for (int i = 0; i < n; i ++) {
       for(int j = 0;j < m;j ++) {
         if(KFC_M[i][j] != 0 && KFC_Y[i][j] != 0 && KFC_M[i][j] + KFC_Y[i][j] < min){
           min = KFC_M[i][j] + KFC_Y[i][j];
         }
       }
      }
     printf("%d\n", 11* min);/*走一步11分钟呢,没想到吧..反正我之前忘了*/
    }
  return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值