Submission #61398146
Source Code Expand
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#pragma GCC optimize("O3,unroll-loops")
#include "bits/stdc++.h"
#ifdef AlRntn
#include "debug.hpp"
#else
#define debug(...) void(0)
#define tstr(...) void(0)
#endif
using int64 = int64_t;
using uint64 = uint64_t;
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
#define fi first
#define se second
#define sz(x) int64_t(x.size())
#define all(x) x.begin(), x.end()
#define spc ' '
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ever ;1;
constexpr int64_t inf = 5e18 + 100;
constexpr int64_t mod1 = 1e9+7;
constexpr int64_t mod2 = 998244353;
constexpr int64_t mod3 = 1e9+9;
constexpr int64_t mod4 = (1ll<<31) - 1;
constexpr long double PI = 2*asinl(1.0);
int dx[4] = {0 , 1 , 0 , -1}; // clockwise from north
int dy[4] = {-1 , 0 , 1 , 0};
int dx2[8] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1}; // clockwise from north
int dy2[8] = {-1 , -1 , 0 , 1 , 1 , 1 , 0 , -1};
const int N = 1e6 + 50, L = 25, M = 1000 + 50;
int64_t H, W;
std::vector<std::string> G;
int64_t dis[2][M][M];
bool inside(int64_t i, int64_t j){
return i >= 0 && i < H && j >= 0 && j < W;
}
void dfs(bool type, int64_t si, int64_t sj, bool ver){
std::stack<std::pair<std::pair<int64_t, int64_t>, bool>> DFS;
DFS.push(std::make_pair(std::make_pair(si, sj), ver));
while(!DFS.empty()){
int64_t i = DFS.top().fi.fi;
int64_t j = DFS.top().fi.se;
bool ver = DFS.top().se;
DFS.pop();
for(int64_t x = ver; x < 4; x += 2){
if(inside(i + dx[x], j + dy[x]) && (dis[type][i + dx[x]][j + dy[x]] == 0 || dis[type][i + dx[x]][j + dy[x]] > dis[type][i][j] + 1) && G[i + dx[x]][j + dy[x]] != '#'){
dis[type][i + dx[x]][j + dy[x]] = dis[type][i][j] + 1;
DFS.push(std::make_pair(std::make_pair(i + dx[x], j + dy[x]), !ver));
}
}
}
}
int32_t main(){
#ifdef AlRntn
freopen("inputf.in" , "r" , stdin); freopen("outputf.out" , "w" , stdout); freopen("errorf.out" , "w" , stderr);
#endif
using namespace std;
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int32_t tc=1;
//#define TestCases
#ifdef TestCases
std::cin >> tc;
#endif
while(tc--){
cin>>H>>W;G.resize(H);
for(auto &ss : G) cin >> ss;
int64_t st_i = 0, st_j = 0;
int64_t en_i = 0, en_j = 0;
for(int64_t i = 0; i < H; i++){
for(int64_t j = 0; j < W; j++){
if(G[i][j] == 'S') st_i = i, st_j = j;
if(G[i][j] == 'G') en_i = i, en_j = j;
}
}
dis[0][st_i][st_j] = dis[1][st_i][st_j] = 1;
dfs(0, st_i, st_j, 0);
dfs(1, st_i, st_j, 1);
if(dis[0][en_i][en_j] || dis[1][en_i][en_j]){
if(dis[0][en_i][en_j] == 0) cout << dis[1][en_i][en_j] - 1 << endl;
else if(dis[1][en_i][en_j] == 0) cout << dis[0][en_i][en_j] - 1 << endl;
else cout << (std::min(dis[1][en_i][en_j], dis[0][en_i][en_j]) - 1) << endl;
}else{
cout << "-1\n";
}
}
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Snaky Walk |
User |
AIRntn |
Language |
C++ 20 (gcc 12.2) |
Score |
0 |
Code Size |
3110 Byte |
Status |
TLE |
Exec Time |
2211 ms |
Memory |
23736 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
All |
00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_00.txt |
AC |
1 ms |
3464 KB |
00_sample_01.txt |
AC |
1 ms |
3460 KB |
00_sample_02.txt |
AC |
1 ms |
3500 KB |
01_random_00.txt |
TLE |
2207 ms |
9120 KB |
01_random_01.txt |
AC |
119 ms |
6108 KB |
01_random_02.txt |
AC |
2 ms |
4300 KB |
01_random_03.txt |
AC |
1 ms |
3712 KB |
01_random_04.txt |
AC |
2 ms |
4272 KB |
01_random_05.txt |
AC |
1 ms |
3492 KB |
01_random_06.txt |
AC |
1 ms |
3596 KB |
01_random_07.txt |
AC |
1 ms |
3456 KB |
01_random_08.txt |
AC |
2 ms |
4612 KB |
01_random_09.txt |
TLE |
2208 ms |
15756 KB |
01_random_10.txt |
TLE |
2208 ms |
16444 KB |
01_random_11.txt |
TLE |
2208 ms |
16984 KB |
01_random_12.txt |
TLE |
2208 ms |
17880 KB |
01_random_13.txt |
TLE |
2208 ms |
18924 KB |
01_random_14.txt |
TLE |
2208 ms |
17008 KB |
01_random_15.txt |
AC |
2 ms |
4424 KB |
01_random_16.txt |
AC |
3 ms |
5536 KB |
01_random_17.txt |
AC |
3 ms |
5260 KB |
01_random_18.txt |
TLE |
2211 ms |
17164 KB |
01_random_19.txt |
TLE |
2211 ms |
16068 KB |
02_random2_00.txt |
AC |
2 ms |
4384 KB |
02_random2_01.txt |
AC |
2 ms |
4568 KB |
02_random2_02.txt |
AC |
2 ms |
4496 KB |
02_random2_03.txt |
AC |
2 ms |
4520 KB |
02_random2_04.txt |
AC |
2 ms |
4452 KB |
02_random2_05.txt |
AC |
2 ms |
4596 KB |
02_random2_06.txt |
AC |
2 ms |
4616 KB |
02_random2_07.txt |
AC |
3 ms |
4596 KB |
02_random2_08.txt |
AC |
2 ms |
4516 KB |
02_random2_09.txt |
AC |
2 ms |
4496 KB |
02_random2_10.txt |
AC |
2 ms |
4504 KB |
02_random2_11.txt |
AC |
2 ms |
4580 KB |
02_random2_12.txt |
AC |
2 ms |
4540 KB |
02_random2_13.txt |
AC |
2 ms |
4612 KB |
02_random2_14.txt |
AC |
2 ms |
4580 KB |
02_random2_15.txt |
AC |
2 ms |
4524 KB |
02_random2_16.txt |
AC |
2 ms |
4584 KB |
02_random2_17.txt |
AC |
2 ms |
4752 KB |
02_random2_18.txt |
AC |
3 ms |
5028 KB |
02_random2_19.txt |
AC |
39 ms |
6264 KB |
03_random3_00.txt |
AC |
123 ms |
22020 KB |
03_random3_01.txt |
AC |
136 ms |
19716 KB |
03_random3_02.txt |
AC |
396 ms |
23736 KB |
03_random3_03.txt |
AC |
156 ms |
22544 KB |
04_random4_00.txt |
AC |
840 ms |
20188 KB |
04_random4_01.txt |
AC |
813 ms |
19188 KB |
04_random4_02.txt |
AC |
810 ms |
19204 KB |
04_random4_03.txt |
AC |
813 ms |
18556 KB |
05_handmade_00.txt |
TLE |
2208 ms |
14188 KB |
05_handmade_01.txt |
TLE |
2210 ms |
7952 KB |
05_handmade_02.txt |
AC |
1 ms |
3456 KB |
05_handmade_03.txt |
AC |
1 ms |
3456 KB |
05_handmade_04.txt |
AC |
1 ms |
3536 KB |
05_handmade_05.txt |
AC |
38 ms |
21012 KB |