이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
map<char, vector<vector<int>>> dir;
int n, m;
bool check(int x, int y){
if (x >= 0 && x < n && y >= 0 && y < m){
return true;
}
return false;
}
void init(){
dir['N'] = {{0, -1, 0},{1, 0, 1}, {0, 1, 2}, {-1, 0, 3}};
dir['E'] = {{0, -1, 3},{1, 0, 0}, {0, 1, 1}, {-1, 0, 2}};
dir['S'] = {{0, -1, 2},{1, 0, 3}, {0, 1, 0}, {-1, 0, 1}};
dir['W'] = {{0, -1, 1},{1, 0, 2}, {0, 1, 3}, {-1, 0, 0}};
dir['X'] = {};
}
int bfs(vector<vector<char>> ma, vector<vector<int>> costs){
queue<vector<int>> q;
set<vector<int>> al;
q.push({0, 0});
al.insert({0, 0});
costs[0][0] = 0;
while(!q.empty()){
vector<int> u = q.front();
q.pop();
al.erase(u);
vector<vector<int>> moves = dir[ma[u[1]][u[0]]];
for (auto el : moves){
int x = el[0] + u[0], y = el[1] + u[1];
if (check(x, y) && costs[y][x] > costs[u[1]][u[0]] + el[2]){
costs[y][x] = costs[u[1]][u[0]] + el[2];
if (al.find({x, y}) == al.end()){
q.push({x, y});
al.insert({x, y});
}
}
}
}
return costs[m - 1][n - 1];
}
int main(){
cin>>m>>n;
vector<vector<char>> ma;
vector<vector<int>> costs;
char r;
for (int i = 0; i < m; i++){
ma.push_back({});
costs.push_back({});
for (int j = 0; j < n; j++){
cin>>r;
ma[i].push_back(r);
costs[i].push_back(INF);
}
}
init();
int res = bfs(ma, costs);
if (res == INF){
cout<<-1;
}
else{
cout<<res;
}
}
# | 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... |