이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
int n, m;
char c;
int kol[502][502];
int dist[502][502];
set< pair<int, pair<int, int> > > s;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main () {
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> c;
			if (c == 'N') kol[i][j] = 0;
			else if (c == 'E') kol[i][j] = 1;
			else if (c == 'S') kol[i][j] = 2;
			else if (c == 'W') kol[i][j] = 3;
			else kol[i][j] = -1;
		
			dist[i][j] = 1e9;
			if (i == 0 && j == 0) dist[i][j] = 0;
			s.insert({dist[i][j], {i, j}});
		}
	}
	
	while (!s.empty()) {
		int cost = s.begin() -> first;
		pair<int, int> p = s.begin() -> second;
		s.erase(s.begin());
		if (kol[p.X][p.Y] == -1) continue;
		
		for (int i = 0; i < 4; i++) {
			int x = p.X + dx[i];
			int y = p.Y + dy[i];
			if (x < 0 || y < 0 || x >= n || y >= m) continue;
			
			int novi = i-kol[p.X][p.Y];
			if (novi < 0) novi += 4;
			if (cost+novi < dist[x][y]) {
				s.erase({dist[x][y], {x, y}});
				dist[x][y] = cost+novi;
				s.insert({dist[x][y], {x, y}});
			}
		}
	}
	
	if (dist[n-1][m-1] >= 1e9) cout << -1;
	else cout << dist[n-1][m-1];
	
	
	return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |