Find a way HDU - 2612

14 篇文章 0 订阅
8 篇文章 0 订阅

哇~~做了一个小时的题啊、、简直了,一个双向BFS,最初的做法是从起点出发,先找到一家KFC,再从KFC出发找到终点,妥妥的超时,后来想了一想,这样的求法基本上是图里有几家KFC,就会遍历几遍图,所以超时,后来换了成从两个端点分别BFS,在KFC的地方分别记录下他们的值,最后去这些中最小的即可,中间一直超时,发现是忘了判断这家KFC能否抵达,结果不是wrong answer 是超时。。。。太神奇了

总结一下这个题目(G++)
如果用cin读取数据,不判断KFC能否抵达会超时
如果用scanf读取,不判断KFC能否抵达会Wrong Answer如果用scanf读取,判断KFC能否抵达,AC,78ms
如果用cin读取,判断KFC能否抵达,AC,31ms
如果用cin读取,把读取放到函数里,AC,46ms
如果用scanf读取,把读取放到函数里,AC,93ms
第二次用cin读取,把读取放到函数里,AC,31ms,比之前的46ms快了15ms
第三次用cin读取,把读取放到函数里,AC,46ms

用cin读取,放在函数中,并通过vector记录KFC店的位置,寻找最短解的时候之间通过vector取出KFC店的位置,AC,15ms
用scanf读取,放在函数中,其余同上,AC,46ms
用cin读取,放在主函数中,其余同上,AC,两次都31ms
用scanf读取,放在主函数中,其余同上,AC,78ms

C++
cin读取,放到函数里,187ms
scanf读取,放到函数里,124ms
cin读取,放在主函数里,156ms
scanf读取,放在主函数里,78ms

代码都是公开的,
https://vjudge.net/contest/65959#status/leehaoze/N/0/
这是提交记录

在这里贴一个最快的(15ms)的代码

//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;
const int INF = 1<<29;
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long

#define MAXN (200 + 50)

struct Pos{
    Pos():x_(0),y_(0),step_(0){}
    Pos(int x,int y,int s):x_(x),y_(y),step_(s){}
    int x_,y_,step_;
    bool operator==(const Pos&B){
        return x_ == B.x_ && y_ == B.y_;
    }
};

struct Inf{
    Inf():step_Y_(-1),step_M_(-1){}
    int step_Y_;
    int step_M_;
};

int Move_X[] = {0, 1, 0,-1};
int Move_Y[] = {1, 0,-1, 0};

int n,m;
char map[MAXN][MAXN];
Inf ans[MAXN][MAXN];
bool visit[MAXN][MAXN];
vector<Pos> V;
Pos Y,M;

bool Input(){
    if(!(cin >> n >> m))
        return false;
    V.clear();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> map[i][j];
            if (map[i][j] == 'Y') {
                Y.x_ = i;
                Y.y_ = j;
            }
            else if (map[i][j] == 'M') {
                M.x_ = i;
                M.y_ = j;
            }
            if(map[i][j] == '@'){
                V.push_back(Pos(i,j,0));
            }
            ans[i][j].step_Y_ = -1;
            ans[i][j].step_M_ = -1;
        }
    }
    return true;
}

void Clear_Visit(){
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            visit[i][j] = false;
        }
    }
}

bool Legal(int dx,int dy){
    return dx >= 0 && dx < n && dy >= 0 && dy < m && !visit[dx][dy] && map[dx][dy] != '#';
}

void BFS(Pos start){
    Clear_Visit();
    queue<Pos>Q;
    Q.push(start);
    visit[start.x_][start.y_] = true;
    while(!Q.empty()){
        Pos now = Q.front();
        Q.pop();
        if(map[now.x_][now.y_] == '@'){
            if(start == Y){
                ans[now.x_][now.y_].step_Y_ = now.step_;
            }
            else{
                ans[now.x_][now.y_].step_M_ = now.step_;
            }
        }
        for (int i = 0; i < 4; ++i) {
            int dx = now.x_ + Move_X[i];
            int dy = now.y_ + Move_Y[i];
            if(Legal(dx,dy)){
                visit[dx][dy] = true;
                Q.push(Pos(dx,dy,now.step_ + 1));
            }
        }

    }
}

void Search(){
    BFS(Y);
    BFS(M);
    int length = MAXN * MAXN;
    int size = (int)V.size();
    for (int i = 0; i < size; ++i) {
        Inf inf = ans[V[i].x_][V[i].y_];
        if(inf.step_M_ != -1 && inf.step_Y_ != -1){
            length = min(length,inf.step_M_ + inf.step_Y_);
        }
    }
    cout << length * 11 << endl;
}

int main() {
#ifdef LOCAL
    freopen("IN.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    while(Input()) {
        Search();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值