2018ACM-ICPC 焦作站现场赛 F. Honeycomb(BFS求最短路,卡memset)

F. Honeycomb

从前不信命,从这道题开始,我信了。

我就是没有拿牌子的命。这道题或者说这个memset,击碎了我所有对ACM的美好记忆。

#include<bits/stdc++.h>
using namespace std;

const int N = 1e3 + 10;
int r, c, w, h;
char s[N*6][N*6];
bool vis[N*6][N*6];
int dir[2][6] = {{0, 0, 3, -3, 3, -3}, {2, -2, 1, 1, -1, -1}};

struct node{
    int x, y, t;
}st;
bool check(int x, int y){
    if(x > 0 && x < w && y > 0 && y <h) return true;
    return false;
}
int BFS(){
    queue<node> Q;
    node u, v;
    u.x = st.x;
    u.y = st.y;
    u.t = st.t;
    vis[u.x][u.y] = true;
    Q.push(u);
    while(!Q.empty()){
        v = Q.front();
        Q.pop();
        if(s[v.x][v.y] == 'T') return v.t;
        for(int i = 0; i < 6; i++){
            int x = v.x + dir[1][i];
            int y = v.y + dir[0][i];
            int dx = x + dir[1][i];
            int dy = y + dir[0][i];
            if(s[x][y] == ' ' && !vis[dx][dy] && check(dx, dy)){
                vis[dx][dy] = true;
                u.x = dx; u.y = dy; u.t = v.t + 1;
                Q.push(u);
            }
        }
    }
    return -1;
}

int main(){
    //freopen("in.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &r, &c);
        getchar();
        w = 4 * r + 3;
        h = 6 * c + 3;
        for(int i = 0; i < w; i++)
            gets(s[i]);
        for(int i = 0; i < w; i++){
            int len = strlen(s[i]);
            for(int j = 0; j < len; j++){
                if(s[i][j] == 'S'){
                    st.x = i; st.y = j; st.t = 1;
                    break;
                }
            }
            if(st.t) break;
        }
        int ans = BFS();
        printf("%d\n", ans);
        for(int i = 0; i < w; i++){
            int len = strlen(s[i]);
            for(int j = 0; j < len; j++){
                s[i][j] = '\0';
                vis[i][j] = false;
            }
        }
        st.x = 0; st.y = 0; st.t = 0;
        r = c = w = h = 0;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值