F. Honeycomb (2018-ACM/ICPC焦作)
Problem F. Honeycomb Input file: standard input Output file: standard outputA 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] != '
'; 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
", bfs(begin_s, end_t));
}
return 0;
}