2018ICPC焦作F-Honeycomb 蜂巢BFS
Posted hesorchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018ICPC焦作F-Honeycomb 蜂巢BFS相关的知识,希望对你有一定的参考价值。
题目
https://codeforces.com/gym/102028/problem/F
求S-T至少要经过几个蜂巢,例如下图为7。
题解
注意细节,按照题意模拟即可。
代码
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
char s[4 * N][6 * N];
int id[4 * N][6 * N];
int mov[][2] = {2, 0, -1, 3, 1, 3};
int sid, eid;
vector<int> mp[N * N];
void add(int u, int v)
{
mp[u].emplace_back(v);
mp[v].emplace_back(u);
}
bool vis[N * N];
int bfs()
{
queue<pair<int, int>> q;
q.push(make_pair(sid, 0));
vis[sid] = 1;
while (q.size())
{
int u = q.front().first;
int step = q.front().second;
q.pop();
if (u == eid)
{
return step + 1;
}
for (auto v : mp[u])
{
if (!vis[v])
{
q.push(make_pair(v, step + 1));
vis[v] = 1;
}
}
}
return -1;
}
int n, m;
void init(int num)
{
for (int i = 1; i <= num; i++)
mp[i].clear(), vis[i] = 0;
}
void solve()
{
int r, c;
scanf("%d %d", &r, &c);
getchar();
n = r * 4 + 3;
m = 6 * c + 3;
for (int i = 0; i <= n + 20; i++)
for (int j = 0; j <= m + 20; j++)
s[i][j] = 0;
for (int i = 1; i <= n; i++)
{
gets(s[i] + 1);
// cout << (s[i] + 1) << endl;
}
int ct = 0;
for (int i = 3; i <= n; i += 4)
for (int j = 5; j <= m; j += 12)
{
id[i][j] = ++ct;
if (s[i][j] == 'S')
sid = ct;
else if (s[i][j] == 'T')
eid = ct;
for (int k = 0; k < 3; k++)
id[i - mov[k][0]][j - mov[k][1]] = ct;
}
for (int i = 5; i <= n; i += 4)
for (int j = 11; j <= m; j += 12)
{
id[i][j] = ++ct;
if (s[i][j] == 'S')
sid = ct;
else if (s[i][j] == 'T')
eid = ct;
for (int k = 0; k < 3; k++)
id[i - mov[k][0]][j - mov[k][1]] = ct;
}
for (int i = 3; i <= n; i += 4)
{
for (int j = 5; j <= m; j += 12)
{
for (int k = 0; k < 3; k++)
{
int xx = i + mov[k][0];
int yy = j + mov[k][1];
if (s[xx][yy] == ' ')
add(id[i][j], id[xx][yy]);
}
}
}
for (int i = 5; i <= n; i += 4)
{
for (int j = 11; j <= m; j += 12)
{
for (int k = 0; k < 3; k++)
{
int xx = i + mov[k][0];
int yy = j + mov[k][1];
if (s[xx][yy] == ' ')
add(id[i][j], id[xx][yy]);
}
}
}
printf("%d\\n", bfs());
init(ct);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
solve();
return 0;
}
以上是关于2018ICPC焦作F-Honeycomb 蜂巢BFS的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse
icpc2018焦作Mathematical Curse(动态规划)
icpc2018焦作Transport Ship(背包思想)