Submission #61392640


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1000 + 5;
const int oo = 1e9;
int h, w;
string s[N];
int dis[N][N][2]; // last element is last direction
pair<int, int> st, ed;
queue<pair<pair<int, int>, int>> q; // position(x, y), last direction[1 or 0] 0: vertical, 1: horizontal
bool checkValid(int x, int y) {
if(x < 0 || x >= h || y < 0 || y >= w || s[x][y] == '#')
return false; // invalid
return true; // else valid
}
// down, right, up, left
int dx[] = {1, 0, -1, 0};
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int N = 1000 + 5;
const int oo = 1e9;

int h, w;
string s[N];
int dis[N][N][2]; // last element is last direction
pair<int, int> st, ed;
queue<pair<pair<int, int>, int>> q; // position(x, y), last direction[1 or 0] 0: vertical, 1: horizontal

bool checkValid(int x, int y) {
	if(x < 0 || x >= h || y < 0 || y >= w || s[x][y] == '#')
		return false; // invalid
	return true; // else valid
}

// down, right, up, left
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void Main() {
    cin >> h >> w;
	for(int i = 0; i < h; i++) {
		cin >> s[i];
		for(int j = 0; j < w; j++)
			if(s[i][j] == 'S') st.first = i, st.second = j;
			else if(s[i][j] == 'G') ed.first = i, ed.second = j;
	}

	// fill(dis, dis + N * N * 2, oo);
	fill(&dis[0][0][0], &dis[0][0][0] + N * N * 2, oo);
	// for(int i = 0; i < 3; i++)
	// 	for(int j = 0; j < 3; j++)
	// 		for(int k = 0; k < 2; k++)
	// 			cout << dis[i][j][k] << " ";
	// cout << endl;

	dis[st.first][st.second][0] = 0;
	q.push({st, 0});
	dis[st.first][st.second][1] = 0;
	q.push({st, 1});
	while(!q.empty()) {
		auto [x, y] = q.front().first;
		int lastdir = q.front().second; q.pop();
		
		for(int dir = 0; dir < 4; dir++) {
			int xx = x + dx[dir];
			int yy = y + dy[dir];
			if(checkValid(xx, yy) && (lastdir != dir % 2) && dis[xx][yy][dir % 2] > dis[x][y][lastdir] + 1) {
				dis[xx][yy][dir % 2] = dis[x][y][lastdir] + 1;
				q.push({{xx, yy}, dir % 2});
			}
		}
	}



	int result = min(dis[ed.first][ed.second][0], dis[ed.first][ed.second][1]);
	if(result > oo - 100)
		cout << -1 << '\n';
	else cout << result << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // int t; cin >> t; while(t--) Main();
    Main();
    return 0;
}
/*
	contest name: beginner-contest387
	contest link: https://atcoder.jp/contests/abc387
	problem letter: D
	Time: 2025-01-04 15:31 UTC: +3:30

	Writer: EnAnsari
	Email: Rahmat2022a@gmail.com
	website: enansari.github.io

	Success does not consist in never making mistakes but in never making the same one a second time. ~George Bernard 
	Shaw

	this code created by rcph (https://github.com/EnAnsari/cph)
*/

Submission Info

Submission Time
Task D - Snaky Walk
User enansari
Language C++ 20 (gcc 12.2)
Score 400
Code Size 2292 Byte
Status AC
Exec Time 51 ms
Memory 12524 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 57
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 4 ms 11328 KB
00_sample_01.txt AC 4 ms 11388 KB
00_sample_02.txt AC 4 ms 11404 KB
01_random_00.txt AC 13 ms 11712 KB
01_random_01.txt AC 5 ms 11424 KB
01_random_02.txt AC 5 ms 12208 KB
01_random_03.txt AC 4 ms 11564 KB
01_random_04.txt AC 5 ms 12068 KB
01_random_05.txt AC 4 ms 11328 KB
01_random_06.txt AC 4 ms 11256 KB
01_random_07.txt AC 4 ms 11396 KB
01_random_08.txt AC 6 ms 12456 KB
01_random_09.txt AC 51 ms 12484 KB
01_random_10.txt AC 46 ms 12436 KB
01_random_11.txt AC 44 ms 12472 KB
01_random_12.txt AC 42 ms 12424 KB
01_random_13.txt AC 37 ms 12496 KB
01_random_14.txt AC 44 ms 12476 KB
01_random_15.txt AC 6 ms 12516 KB
01_random_16.txt AC 6 ms 12524 KB
01_random_17.txt AC 6 ms 12464 KB
01_random_18.txt AC 46 ms 12428 KB
01_random_19.txt AC 48 ms 12404 KB
02_random2_00.txt AC 6 ms 12468 KB
02_random2_01.txt AC 6 ms 12384 KB
02_random2_02.txt AC 6 ms 12396 KB
02_random2_03.txt AC 6 ms 12468 KB
02_random2_04.txt AC 6 ms 12460 KB
02_random2_05.txt AC 6 ms 12452 KB
02_random2_06.txt AC 6 ms 12456 KB
02_random2_07.txt AC 6 ms 12404 KB
02_random2_08.txt AC 6 ms 12456 KB
02_random2_09.txt AC 6 ms 12460 KB
02_random2_10.txt AC 6 ms 12380 KB
02_random2_11.txt AC 6 ms 12324 KB
02_random2_12.txt AC 6 ms 12460 KB
02_random2_13.txt AC 6 ms 12452 KB
02_random2_14.txt AC 6 ms 12400 KB
02_random2_15.txt AC 6 ms 12468 KB
02_random2_16.txt AC 6 ms 12520 KB
02_random2_17.txt AC 6 ms 12464 KB
02_random2_18.txt AC 6 ms 12452 KB
02_random2_19.txt AC 7 ms 12472 KB
03_random3_00.txt AC 26 ms 12468 KB
03_random3_01.txt AC 16 ms 12380 KB
03_random3_02.txt AC 32 ms 12520 KB
03_random3_03.txt AC 27 ms 12316 KB
04_random4_00.txt AC 24 ms 12400 KB
04_random4_01.txt AC 20 ms 12464 KB
04_random4_02.txt AC 23 ms 12468 KB
04_random4_03.txt AC 22 ms 12380 KB
05_handmade_00.txt AC 19 ms 12468 KB
05_handmade_01.txt AC 34 ms 12500 KB
05_handmade_02.txt AC 4 ms 11396 KB
05_handmade_03.txt AC 4 ms 11392 KB
05_handmade_04.txt AC 4 ms 11340 KB
05_handmade_05.txt AC 27 ms 12360 KB


2025-03-05 (Wed)
12:33:06 +00:00