Submission #1090268

# Submission time Handle Problem Language Result Execution time Memory
1090268 2024-09-18T06:00:01 Z Muhammet Awesome Arrowland Adventure (eJOI19_adventure) C++17
100 / 100
65 ms 6092 KB
#include <bits/stdc++.h>

using namespace std;

#define ff first
#define ss second

int n, m, vis[505][505];

char v[] = {'N', 'E', 'S', 'W'};

vector <vector <char>> a;

vector <vector <int>> dp;

int f(char x, char y){
	int ind = 0;
	for(int i = 0; i < 4; i++){
		if(v[i] == x){
			ind = i;
			break;
		}
	}
	int cnt = 0;
	while(1){
		if(v[ind] == y) break;
		ind++;
		if(ind == 4) ind = 0;
		cnt++;
	}
	return cnt;
}

int main(){
	cin >> n >> m;
	a.assign(n+1, vector <char> (m+1));
	dp.assign(n+1, vector <int> (m+1,1e8));
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j];
			if(a[i][j] == 'X') vis[i][j] = 1;
		}
	}
	priority_queue <pair<int,pair<int,int>>> q;
	q.push({0,{n,m}});
	for(int i = 0; i <= n+1; i++){
		vis[i][0] = vis[i][m+1] = 1;
	}
	for(int i = 0; i <= m+1; i++){
		vis[0][i] = vis[n+1][i] = 1;
	}
	dp[n][m] = 0;
	a[n][m] = 'E';
	while(!q.empty()){
		int i = q.top().ss.ff, j = q.top().ss.ss, w = q.top().ff;
		w *= (-1);
		q.pop();
		if((a[i][j] == 'X') or (dp[i][j] != w)) continue;
		if(vis[i][j+1] == 0 and f(a[i][j+1],'W')+dp[i][j] < dp[i][j+1]){
			dp[i][j+1] = f(a[i][j+1],'W')+dp[i][j];
			q.push({-dp[i][j+1],{i,j+1}});
		}
		if(vis[i][j-1] == 0 and f(a[i][j-1],'E')+dp[i][j] < dp[i][j-1]){
			dp[i][j-1] = f(a[i][j-1],'E')+dp[i][j];
			q.push({-dp[i][j-1],{i,j-1}});
		}
		if(vis[i+1][j] == 0 and f(a[i+1][j],'N')+dp[i][j] < dp[i+1][j]){
			dp[i+1][j] = f(a[i+1][j],'N')+dp[i][j];
			q.push({-dp[i+1][j],{i+1,j}});
		}
		if(vis[i-1][j] == 0 and f(a[i-1][j],'S')+dp[i][j] < dp[i-1][j]){
			dp[i-1][j] = f(a[i-1][j],'S')+dp[i][j];
			q.push({-dp[i-1][j],{i-1,j}});
		}
	}
	cout << (dp[1][1] == 1e8 ? -1 : dp[1][1]);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 432 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 440 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 432 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 440 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 5 ms 824 KB Output is correct
38 Correct 8 ms 1116 KB Output is correct
39 Correct 45 ms 2908 KB Output is correct
40 Correct 46 ms 2908 KB Output is correct
41 Correct 44 ms 2932 KB Output is correct
42 Correct 51 ms 2888 KB Output is correct
43 Correct 65 ms 6092 KB Output is correct
44 Correct 47 ms 2908 KB Output is correct