H - Find a way HDU - 2612 ——2次BFS

Think:
1自己用结构体队列测试数据都对,但是结果错误,一直没有找到错误所在,暂时将问题记录
2借鉴前辈博客,用C++中STl里面的queue队列,与自己之前的思想基本相同,Accepted
3题目题点理解:
1>有的KFC可能无法都达到
2>KFC可以作为路径结点
3>Y点M点可以作为对方的路径结点
4知识收获:C++中STL里的queue队列用法+1,多点BFS

H - Find a way HDU - 2612
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

以下为Wrong Answer代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

struct node
{
    int x;
    int y;
    int step;
}link_Y[44444], link_M[44444];

int yo, yt, mo, mt, tp, ans, n, m;
int v[240][240], w[240][240], book[2][240][240];
char st[240][240];

int jk[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void Ans();
bool Judge(int dx, int dy);

int main()
{
    int i;
    while(scanf("%d %d", &n, &m) != EOF){
        yo = yt = 0, mo = mt = 0;
        tp = 0, ans = 84444;
        memset(v,0, sizeof(v));
        for(i = 0; i < 44444; i++){
            link_Y[i].step = 0, link_M[i].step = 0;
        }
        for(i = 0; i < n; i++){
            scanf("%s", st[i]);
        }

        Ans();
        printf("%d\n", ans*11);
    }
    return 0;
}
bool Judge(int dx, int dy){
    if(dx < 0 || dx >= n || dy < 0 || dy >= m)
        return false;
    else
        return true;
}
void Ans(){
    int i, j;
    memset(w, 0, sizeof(w));
    memset(book , 0, sizeof(book));
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            if(st[i][j] == 'Y'){
                link_Y[yt].x = i;
                link_Y[yt].y = i;
                yt++;
            }
            else if(st[i][j] == 'M'){
                link_M[mt].x = i;
                link_M[mt].y = j;
                mt++;
            }
        }
    }
    v[link_Y[yo].x][link_Y[yo].y] = 1;
    while(yo < yt){
        for(i = 0; i < 4; i++){
            int x = link_Y[yo].x + jk[i][0];
            int y = link_Y[yo].y + jk[i][1];
            if(Judge(x, y) && st[x][y] != '#' && v[x][y] == 0){
                v[x][y] = 1;
                link_Y[yt].x = x;
                link_Y[yt].y = y;
                link_Y[yt].step = link_Y[yo].step + 1;
                w[x][y] += link_Y[yt].step;
                if(st[x][y] == '@')
                    book[0][x][y] = 1;
                yt++;
            }
        }
        yo++;
    }
    memset(v, 0, sizeof(v));
    v[link_M[mo].x][link_M[mo].y] = 1;
    while(mo < mt){
        for(i = 0; i < 4; i++){
            int x = link_M[mo].x + jk[i][0];
            int y = link_M[mo].y + jk[i][1];
            if(Judge(x, y) && st[x][y] != '#' && v[x][y] == 0){
                v[x][y] = 1;
                link_M[mt].x = x;
                link_M[mt].y = y;
                link_M[mt].step = link_M[mo].step + 1;
                w[x][y] += link_M[mt].step;
                if(st[x][y] == '@')
                    book[1][x][y] = 1;
                mt++;
            }
        }
        mo++;
    }
    for(i = 0; i < n; i++){
        for(j = 0; j < m; j++){
            if(st[i][j] == '@' && book[0][i][j] && book[1][i][j])
                ans = min(ans, w[i][j]);
        }
    }
}

以下为Accepted代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>
#define N 240
#define Inf 0x3f3f3f3f

using namespace std;

int n, m, flag, v[N][N], dis[N][N][2];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char st[N][N];

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

bool Judge(int dx, int dy){
    if(dx < 0 || dx >= n || dy < 0 || dy >= m || st[dx][dy] == '#' || v[dx][dy] != 0)
        return false;
    else
        return true;
}

void BFS(int x, int y){
    int k;
    queue<struct node>q;
    struct node cur, next;
    cur.x = x, cur.y = y, cur.step = 0;
    v[x][y] = 1;
    q.push(cur);
    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)){
                v[x][y] = 1;
                if(st[x][y] == '@')
                    dis[x][y][flag] = next.step;
                q.push(next);
            }
        }
    }
}
int main()
{
    int i, j, ans;
    while(scanf("%d %d", &n, &m) != EOF){
        ans = Inf;
        for(i = 0; i < n; i++){
            for(j = 0; j < m; j++){
                dis[i][j][0] = dis[i][j][1] = Inf;
            }
        }
        for(i = 0; i < n; i++)
            scanf("%s", st[i]);
        for(i = 0; i < n; i++){
            for(j = 0; j < m; j++){
                if(st[i][j] == 'Y'){
                    flag = 0;
                    memset(v, 0, sizeof(v));
                    BFS(i, j);
                }
                else if(st[i][j] == 'M'){
                    flag = 1;
                    memset(v, 0, sizeof(v));
                    BFS(i, j);
                }
            }
        }
        for(i = 0; i < n; i++){
            for(j = 0; j < m; j++){
                if(st[i][j] == '@' && dis[i][j][0] != Inf && dis[i][j][1] != Inf){
                    ans = min(ans, dis[i][j][0]+dis[i][j][1]);
                }
            }
        }
        printf("%d\n", ans*11);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值