Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
using namespace std; int main() { int n, m; cin >> n >> m; char mat[n][m]; int si, sj; int ei, ej; vector
> dzidovi; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin >> mat[i][j]; if(mat[i][j] == 'S') { si = i; sj = j; } if(mat[i][j] == 'E') { ei = i; ej = j; } if(mat[i][j] == '#') { dzidovi.push_back(make_pair(i, j)); } } } int res = -1; int di[] = {-1, 1, 0, 0}; int dj[] = {0, 0, -1, 1}; queue
q; q.push(si); q.push(sj); q.push(0); vector
> visited(n, vector
(m, false)); vector
> dist_from_S(n, vector
(m, -1)); visited[si][sj] = true; while(!q.empty()) { int ci = q.front(); q.pop(); int cj = q.front(); q.pop(); int dist = q.front(); q.pop(); if(ci == ei and cj == ej) { cout << dist << endl; return 0; } if(mat[ci][cj] == '#') { dist_from_S[ci][cj] = dist; continue; } for(int i = 0; i < 4; i++) { int ti = ci + di[i]; int tj = cj + dj[i]; if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) { q.push(ti); q.push(tj); q.push(dist + 1); visited[ti][tj] = true; } } } q.push(ei); q.push(ej); q.push(0); vector
> visited2(n, vector
(m, false)); vector
> dist_from_E(n, vector
(m, -1)); while(!q.empty()) { int ci = q.front(); q.pop(); int cj = q.front(); q.pop(); int dist = q.front(); q.pop(); if(mat[ci][cj] == '#') { dist_from_E[ci][cj] = dist; continue; } for(int i = 0; i < 4; i++) { int ti = ci + di[i]; int tj = cj + dj[i]; if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited2[ti][tj]) { q.push(ti); q.push(tj); q.push(dist + 1); visited2[ti][tj] = true; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(dist_from_S[i][j] != -1 and dist_from_E[i][j] != -1) { if(dist_from_S[i][j] + dist_from_E[i][j] > res) { res = dist_from_S[i][j] + dist_from_E[i][j]; } } } } cout << res << endl; return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)