#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#define endl '\n'
#define int long long
#define mod 1000000007
using namespace std;
char aa[1001][1001];
int dist[1001][1001];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
signed main(){
int r,c; cin >> r >> c;
priority_queue<pair<int,pair<int,int>>>d;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++) {
cin >> aa[i][j];
dist[i][j] = 1e18;
if(aa[i][j] == 'S') {
dist[i][j] = 0;
d.push({0,{i,j}});
}
}
}
while(d.size()){
auto x = d.top();
d.pop();
if(x.first > dist[x.second.first][x.second.second]) continue;
int k = x.second.first-1, j = x.second.second;
while(k >= 0){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k + 1][j]){
dist[k + 1][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k + 1][j]}, {k + 1,j}});
}
break;
}else if(k == 0){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
k--;
}
k = x.second.first + 1;
while(k < r){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k - 1][j]){
dist[k - 1][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k - 1][j]}, {k - 1,j}});
}
break;
}else if(k == r - 1){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
k++;
}
k = x.second.first;
j--;
while(j >= 0){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j + 1]){
dist[k][j + 1] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j + 1]}, {k,j + 1}});
}
break;
}else if(j == 0){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
j--;
}
j = x.second.second + 1;
while(j < c){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j - 1]){
dist[k][j - 1] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j - 1]}, {k,j - 1}});
}
break;
}else if(j == c - 1){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
j++;
}
for(int i = 0; i < 4; i++){
int a = x.second.first + dx[i], b = x.second.second + dy[i];
if(a < 0 or b < 0 or a >= r or b >= c) continue;
if(aa[a][b] == '#') continue;
if(1 + x.first < dist[a][b]){
dist[a][b] = x.first + 1;
d.push({{x.first + 1}, {a,b}});
}
}
}
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(aa[i][j] == 'C'){
if(dist[i][j] == 1e18){
cout << -1;
}else{
cout << dist[i][j];
}
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
35 ms |
1744 KB |
Output is correct |
6 |
Execution timed out |
1081 ms |
1812 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |