0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 14:58:48 +00:00

#1820. [COCI2017-2018#3] Portal

https://sjzezoj.com/submission/67381
This commit is contained in:
Baoshuo Ren 2023-01-27 21:56:56 +08:00
parent 57d77ced86
commit 5a60a525aa
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
54 changed files with 340 additions and 0 deletions

181
S2OJ/1820/1820.cpp Normal file
View File

@ -0,0 +1,181 @@
#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 505;
const int INF = 0x3f3f3f3f;
const int d[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m, s, t, pos[N * N][4], dist1[N][N], dist2[N * N];
std::string h[N];
std::vector<std::pair<int, int>> g[N * N];
bool vis[N * N];
int id(int x, int y) {
return x * m + y;
}
void spfa() {
memset(dist2, 0x3f, sizeof(dist2));
std::queue<int> q;
q.emplace(s);
dist2[s] = 0;
vis[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (auto e : g[u]) {
int v = e.first;
long long w = e.second;
if (dist2[v] > dist2[u] + w) {
dist2[v] = dist2[u] + w;
if (!vis[v]) {
vis[v] = true;
q.emplace(v);
}
}
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(dist1, 0x3f, sizeof(dist1));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> h[i];
h[i] = ' ' + h[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i][j] == 'C') {
s = id(i, j);
} else if (h[i][j] == 'F') {
t = id(i, j);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i][j - 1] == '#') {
pos[id(i, j)][0] = id(i, j);
} else {
pos[id(i, j)][0] = pos[id(i, j - 1)][0];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = m; j; j--) {
if (h[i][j + 1] == '#') {
pos[id(i, j)][1] = id(i, j);
} else {
pos[id(i, j)][1] = pos[id(i, j + 1)][1];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i - 1][j] == '#') {
pos[id(i, j)][2] = id(i, j);
} else {
pos[id(i, j)][2] = pos[id(i - 1, j)][2];
}
}
}
for (int i = n; i; i--) {
for (int j = 1; j <= m; j++) {
if (h[i + 1][j] == '#') {
pos[id(i, j)][3] = id(i, j);
} else {
pos[id(i, j)][3] = pos[id(i + 1, j)][3];
}
}
}
std::queue<std::pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i][j] == '#') {
q.emplace(i, j);
dist1[i][j] = 0;
}
}
}
while (!q.empty()) {
int x = q.front().first,
y = q.front().second;
q.pop();
for (auto d : d) {
int xx = x + d[0],
yy = y + d[1];
if (xx < 1 || xx > n || yy < 1 || yy > m || h[xx][yy] == '#') continue;
if (dist1[xx][yy] > dist1[x][y] + 1) {
dist1[xx][yy] = dist1[x][y] + 1;
q.emplace(xx, yy);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (h[i][j] == '#') continue;
for (auto d : d) {
int x = i + d[0],
y = j + d[1];
if (h[x][y] != '#') {
g[id(i, j)].emplace_back(id(x, y), 1);
}
}
for (int k = 0; k < 4; k++) {
if (pos[id(i, j)][k] != id(i, j)) {
g[id(i, j)].emplace_back(pos[id(i, j)][k], dist1[i][j]);
}
}
}
}
spfa();
if (dist2[t] == INF) {
cout << "nemoguce" << endl;
} else {
cout << dist2[t] << endl;
}
return 0;
}

BIN
S2OJ/1820/data/portal1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal21.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal21.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal22.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal22.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal23.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal23.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal24.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal24.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal25.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal25.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal26.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal26.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/portal9.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1820/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.