#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 250005;
vector<pair<int, int>> adj[N];
bool mark[N];
int n, m;
int dist[N];
void dijkstra(int start){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minq;
for(int i=0; i< n*m; i++){
dist[i]=1e9;
}
dist[0]=0;
minq.push(mp(0, 0));
while(!minq.empty()){
pair<int, int> node=minq.top();
minq.pop();
if(mark[node.S]){
continue;
}
for(pair<int, int> to : adj[node.S]){
dist[to.F]=min(dist[to.F], dist[node.S]+to.S);
minq.push(mp(dist[to.F], to.F));
}
mark[node.S]=true;
}
}
int main()
{
cin >> n >> m;
char a[n][m];
for(int i=0; i< n; i++){
for(int j=0; j< m; j++){
cin >> a[i][j];
}
}
for(int i=0; i< n; i++){
for(int j=0; j< m; j++){
int node=i*m+j;
if(a[i][j]=='X'){
continue;
}
else if(a[i][j]=='E'){
if(j<m-1)adj[node].pb(mp(node+1, 0));
if(j>0)adj[node].pb(mp(node-1, 2));
if(i>0)adj[node].pb(mp(node-m, 3));
if(i<n-1)adj[node].pb(mp(node+m, 1));
}
else if(a[i][j]=='S'){
if(j<m-1)adj[node].pb(mp(node+1, 3));
if(j>0)adj[node].pb(mp(node-1, 1));
if(i>0)adj[node].pb(mp(node-m, 2));
if(i<n-1)adj[node].pb(mp(node+m, 0));
}
else if(a[i][j]=='W'){
if(j<m-1)adj[node].pb(mp(node+1, 2));
if(j>0)adj[node].pb(mp(node-1, 0));
if(i>0)adj[node].pb(mp(node-m, 1));
if(i<n-1)adj[node].pb(mp(node+m, 3));
}
else{
if(j<m-1)adj[node].pb(mp(node+1, 1));
if(j>0)adj[node].pb(mp(node-1, 3));
if(i>0)adj[node].pb(mp(node-m, 0));
if(i<n-1)adj[node].pb(mp(node+m, 2));
}
}
}
dijkstra(0);
cout << dist[n*m-1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
6100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
6100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Incorrect |
3 ms |
6100 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
6180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
6100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |