2018ACM/ICPC亚洲区域赛(焦作)F. Honeycomb

F. Honeycomb (2018-ACM/ICPC焦作)

[TOC]

Problem F. Honeycomb Input file: standard input Output file: standard output

A honeycomb is a mass wax cells built by honey bees, which can be described as a regular tiling of the Euclidean plane, in which three hexagons meet at each internal vertex. The internal angle of a hexagon is 120 degrees, so three hexagons at a point make a full 360 degrees. The following figure shows a complete honeycomb with 3 rows and 4 columns.

Here we guarantee that the first cell in the second column always locates in the bottom right side of the first cell in the first column, as shown above. A general honeycomb may, on the basis of a complete honeycomb, lose some walls between adjacent cells, but the honeycomb is still in a closed form. A possible case looks like the figure below.

Hamilton is a brave bee living in a general honeycomb. Now he wants to move from a starting point to a specified destination. The image below gives a feasible path in a 3×4 honeycomb from the 1-st cell in the 2-nd column to the 1-st cell in the 4-th column.

Please help him find the minimum number of cells that a feasible path has to pass through (including the starting point and the destination) from the specified starting point to the destination.

Input The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 1e4. For each test case, the first line contains two integers r and c indicating the number of rows and the number of columns of the honeycomb, where 2 ⇐ r, c ⇐ 1e3. The following (4r+3) lines describe the whole given honeycomb, where each line contains at most (6c+3) characters. Odd lines contain grid vertices represented as plus signs (“+”) and zero or more horizontal edges, while even lines contain two or more diagonal edges. Specifically, a cell is described as 6 vertices and at most 6 edges. Its upper boundary or lower boundary is represented as three consecutive minus signs (“-”). Each one of its diagonal edges, if exists, is a single forward slash (“/”) or a single backslash (“\”) character. All edge characters will be placed exactly between the corresponding vertices. At the center of the starting cell (resp. the destination), a capital “S” (resp. a capital “T”) as a special character is used to indicate the special cell. All other characters will be space characters. Note that if any input line could contain trailing whitespace, that whitespace will be omitted. We guarantee that all outermost wall exist so that the given honeycomb is closed, and exactly one “S” and one “T” appear in the given honeycomb. Besides, the sum of r · c in all test cases is up to 2e6.

Output For each test case, output a line containing the minimum number of cells that Hamilton has to visit moving from the starting cell (“S”) to the destination (“T”), including the starting cell and the destination. If no feasible path exists, output -1 instead.

题意是二维平面最短路,不过地图形成蜂窝一样的。我们需要做一个映射。这题题目内存给了一个G,%>_<%,!!!知道这一点后就很好做了。现场题目找不到内存时间,网页提交的地方也没写,下载输入输出的地方才写了。赛后才找到。唉。。。

具体看代码把,映射点

map<pair<int, int>, vector<pair<int, int> > > E;存图,然后bfs。

代码在学校windows都跑不起来,内存用太多了。

注意memset复杂度可能不太对。因为T比较多。

标记点不要用set<pair<int, int> >s了,直接开一个二维数组。内存不要命的开

/**
1
3 4
 12345678901234567890123456789
1  +---+   |   +---+   |
2 /  |  \  |  /  |  \  |
3+---O   +---+   O   +---+      [3, 5]      [3, 17]
4 \  |     |  \     /  |  \
5  +   +   S   +---+   T   +        [5, 11]     [5, 23]
6 /     \     /           /
7+       +---+       +   +      [7, 5]      [7, 17]
8 \           \     /     \
9  +---+       +---+       +        [9, 11]     [9, 23]
0 /                       /
1+       +---+       +   +
2 \                 /     \
3  +---+       +---+       +
4       \     /     \     /
5        +---+       +---+

*/
#include <bits/stdc++.h>

using namespace std;
const int MAX_R = (1e3 + 10) * 4 + 3;
const int MAX_C = (1e3 + 10) * 6 + 3;

char mp[MAX_R][MAX_C];

bool point_flag[MAX_R][MAX_C];

int t, r, c;

int Next[6][2] = {-1, -3, -1, 3, 1, -3, 1, 3, -2, 0, 2, 0};

//set<pair<int, int> > point_flag;

map<pair<int, int>, vector<pair<int, int> > > E;

void init() {
    for(int i = 3; i <= MAX_R; i += 4) {
        for(int j = 5; j <= MAX_C; j += 12) {
            //point_flag.insert(make_pair(i, j));
            point_flag[i][j] = true;
        }
    }
    for(int i = 5; i <= MAX_R; i += 4) {
        for(int j = 11; j <= MAX_C; j += 12) {
            //point_flag.insert(make_pair(i, j));
            point_flag[i][j] = true;
        }
    }
}

bool isin(int x, int y) {
    return x >= 1 && x <= 4 * r + 3 && y >= 1 && y <= 6 * c + 3;
}

struct Node {
    pair<int, int>pos;
    int step;
    Node() {}
    Node(int xx, int yy, int st) {
        step = st;
        pos = make_pair(xx, yy);
    }
    Node(pair<int, int>p, int st) {
        step = st;
        pos = p;
    }
};

int bfs(pair<int, int> &begin_s, pair<int, int> &end_t) {
    queue<Node>que;
    set<pair<int, int> >vis;
    que.push(Node(begin_s, 1));
    vis.insert(begin_s);
    while(!que.empty()) {
        Node now = que.front();
        que.pop();
        if(now.pos == end_t) {
            return now.step;
        }
        for(auto it: E[now.pos]) {
            if(!vis.count(it)) {
                vis.insert(it);
                mp[it.first][it.second] = '#';
                que.push(Node(it, now.step + 1));
            }
        }
    }
    return -1;
}

int main() {

    init();
    scanf("%d", &t);
    while(t--) {
        E.clear();
        scanf("%d %d", &r, &c);
        getchar();
        for(int i = 1; i <= 4 * r + 3; i++ ) {
            fgets(mp[i] + 1, MAX_C, stdin);
        }
        pair<int, int>begin_s, end_t;
        for(int i = 1; i <= 4 * r + 3; i++ ) {
            for(int j = 1; mp[i][j] != '\n'; j++ ) {
                if(point_flag[i][j]) {
                    if(mp[i][j] == 'S') {
                        begin_s = make_pair(i, j);
                    }
                    if(mp[i][j] == 'T') {
                        end_t = make_pair(i, j);
                    }
                    for(int k = 0; k < 6; k++ ) {
                        int gx = i + Next[k][0];
                        int gy = j + Next[k][1];
                        int mx = i + Next[k][0] * 2;
                        int my = j + Next[k][1] * 2;
                        if(!isin(gx, gy) || !isin(mx, my) || mp[gx][gy] != ' ') {
                            continue;
                        }
                        if(mp[mx][my] == 'S' || mp[mx][my] == 'T' || mp[mx][my] == ' ') {
                            E[make_pair(i, j)].push_back(make_pair(mx, my));
                        }
                    }
                }
            }
        }
        printf("%d\n", bfs(begin_s, end_t));
    }
    return 0;
}

全部评论

相关推荐

02-01 12:05
复旦大学 Java
腾讯的提前批大概率应该是没有笔试的,但是这个时候有相当部分的同学简历估计都没有准备好,没准备好的同学也不用急,大部分都是3月之后开,这个时候开的绝大多数都是神仙打架,问的东西也比较难,打算投递的同学也多看下计算机网络和操作系统,腾讯对这部分的知识问的比较多。另外多刷下牛客的热门题库,刷题注意刷ACM模式,和牛客的周赛题,腾讯有的部门会从这里面出原题。我是@程序员花海关注我,带你了解更多校招资讯!
程序员花海:还没有来得及准备的同学可以看下学习路线:https://www.nowcoder.com/discuss/824693499982315520?sourceSSR=users算法题:https://www.nowcoder.com/feed/main/detail/20e7a999fa04485b88340a274411ca0d?sourceSSR=users八股文:https://www.nowcoder.com/discuss/833102362771251200?sourceSSR=users简历书写方式:https://www.nowcoder.com/discuss/839907820706205696?sourceSSR=users都是以前在牛客发的文章~
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务