#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(time(nullptr));
/* -------------------- Typedefs -------------------- */
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
/* -------------------- Usings -------------------- */
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
/* -------------------- Defines -------------------- */
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);
/* -------------------- Constants -------------------- */
const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);
/* -------------------- Functions -------------------- */
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void precision(int x) {
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
}
ll gcd(ll a, ll b) {
if (a == 0 || b == 0) return 0;
while (b) {
a %= b;
swap(a, b);
}
return a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll range_sum(ll a, ll b) {
if (a > b) return 0ll;
ll dif = a - 1, cnt = b - a + 1;
ll ans = ((b - a + 1) * (b - a + 2)) / 2;
ans += ((b - a + 1) * dif);
return ans;
}
string dec_to_bin(ll a) {
string s = "";
for (ll i = a; i > 0; ) {
ll k = i % 2;
i /= 2;
char c = k + 48;
s += c;
}
if (a == 0) {
s = "0";
}
reverse(all(s));
return s;
}
ll bin_to_dec(string s) {
ll num = 0;
for (int i = 0; i < s.size(); i++) {
num *= 2ll;
num += (s[i] - '0');
}
return num;
}
ll factorial_by_mod(ll n, ll mod) {
ll ans = 1;
ll num;
for (ll i = 1; i <= n; ++i) {
num = i % mod;
ans *= num;
ans %= mod;
}
return ans;
}
bool isPrime(ll a) {
if (a == 1) return false;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0) return false;
}
return true;
}
ll binpow(ll a, ll b) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
b >>= 1;
a *= a;
}
return ans;
}
ll binpow_by_mod(ll a, ll b, ll mod) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
/* -------------------- Solution -------------------- */
const int N = 1005;
char a[N][N];
int wall_dist[N][N], dist[N][N];
pii ver[N][N], nerq[N][N], dzax[N][N], aj[N][N];
bool used[N][N];
int n, m, sx, sy, ex, ey;
void clr_used() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
used[i][j] = false;
}
}
}
bool is_valid(int x, int y) {
return (x >= 1 && x <= n && y >= 1 && y <= m);
}
bool wall_adj(int x, int y) {
if (a[x - 1][y] == '#') return true;
if (a[x + 1][y] == '#') return true;
if (a[x][y - 1] == '#') return true;
if (a[x][y + 1] == '#') return true;
return false;
}
void direct_nodes() {
//up
int x, y;
for (int j = 1; j <= m; ++j) {
x = -1, y = -1;
for (int i = 1; i <= n; ++i) {
if (a[i][j] == '#') {
x = -1, y = -1;
}
else if (x == -1) {
x = i, y = j;
}
ver[i][j] = { x,y };
}
}
//down
for (int j = 1; j <= m; ++j) {
x = -1, y = -1;
for (int i = n; i >= 1; --i) {
if (a[i][j] == '#') {
x = -1, y = -1;
}
else if (x == -1) {
x = i, y = j;
}
nerq[i][j] = { x,y };
}
}
//left
for (int i = 1; i <= n; ++i) {
x = -1, y = -1;
for (int j = 1; j <= m; ++j) {
if (a[i][j] == '#') {
x = -1, y = -1;
}
else if (x == -1) {
x = i, y = j;
}
dzax[i][j] = { x,y };
}
}
//rigth
for (int i = 1; i <= n; ++i) {
x = -1, y = -1;
for (int j = m; j >= 1; --j) {
if (a[i][j] == '#') {
x = -1, y = -1;
}
else if (x == -1) {
x = i, y = j;
}
aj[i][j] = { x,y };
}
}
}
void gna_to_wall(int x, int y, int nx, int ny, queue<pii>& q) {
if (is_valid(nx, ny) && a[nx][ny] != '#' && !used[nx][ny]) {
used[nx][ny] = true;
wall_dist[nx][ny] = wall_dist[x][y] + 1;
q.push({ nx,ny });
}
}
void bfs_to_wall(queue<pii> q) {
while (!q.empty()) {
int x = q.front().ff, y = q.front().ss;
q.pop();
gna_to_wall(x, y, x + 1, y, q);
gna_to_wall(x, y, x - 1, y, q);
gna_to_wall(x, y, x, y + 1, q);
gna_to_wall(x, y, x, y - 1, q);
}
clr_used();
}
void gna(int x, int y, int nx, int ny, int val, priority_queue<pair<int, pii>>& q) {
if (is_valid(nx, ny) && a[nx][ny] != '#' && dist[nx][ny] > dist[x][y] + val + 1) {
dist[nx][ny] = dist[x][y] + val + 1;
used[nx][ny] = true;
q.push({ -dist[nx][ny], { nx, ny } });
}
}
void dijkstra() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
dist[i][j] = MAX;
}
}
priority_queue<pair<int, pii>> q;
q.push({ 0,{sx,sy} });
dist[sx][sy] = 0;
while (!q.empty()) {
int x = q.top().ss.ff, y = q.top().ss.ss, her = -q.top().ff;
q.pop();
if (her > dist[x][y]) continue;
gna(x, y, x + 1, y, 0, q);
gna(x, y, x - 1, y, 0, q);
gna(x, y, x, y + 1, 0, q);
gna(x, y, x, y - 1, 0, q);
gna(x, y, ver[x][y].ff, ver[x][y].ss, wall_dist[x][y], q);
gna(x, y, nerq[x][y].ff, nerq[x][y].ss, wall_dist[x][y], q);
gna(x, y, dzax[x][y].ff, dzax[x][y].ss, wall_dist[x][y], q);
gna(x, y, aj[x][y].ff, aj[x][y].ss, wall_dist[x][y], q);
}
}
void slv() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
if (a[i][j] == 'S') {
sx = i, sy = j;
}
if (a[i][j] == 'C') {
ex = i, ey = j;
}
}
}
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= m + 1; ++j) {
if (i == 0 || i == n + 1 || j == 0 || j == m + 1) {
a[i][j] = '#';
}
}
}
queue<pii> param;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (wall_adj(i, j) && a[i][j] != '#') {
param.push({ i,j });
used[i][j] = true;
}
}
}
direct_nodes();
bfs_to_wall(param);
dijkstra();
cout << dist[ex][ey];
}
/*
5 7
S.#....
.#..##.
.......
#.#...#
.#C.##.
*/
void cs() {
int tstc = 1;
//cin >> tstc;
while (tstc--) {
slv();
}
}
void precalc() {
return;
}
int main() {
fastio();
precalc();
//precision(0);
cs();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |