#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fi first
#define sec second
#define pii pair<ll,ll>
const ll N = 1e5 + 5;
const ll MOD = 1e9 + 7;
char grid[505][505];
ll dist[505][505];
ll dx[] = {0,0,1,-1};
ll dy[] = {1,-1,0,0};
char dir[] = {'R', 'L', 'S', 'U'};
signed main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll tc = 1;
// cin >> tc;
while(tc--){
ll n,m; cin >> n >> m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >> grid[i][j];
dist[i][j] = 1e18;
}
}
priority_queue<pair<ll,pii>,vector<pair<ll,pii>>,greater<pair<ll,pii>>> pq;
pq.push({0, {1,1}});
dist[1][1] = 0;
while(!pq.empty()){
auto x = pq.top(); pq.pop();
if(dist[x.sec.fi][x.sec.sec] < x.fi || grid[x.sec.fi][x.sec.sec] == 'X') continue;
for(int i=0;i<4;i++){
ll newi = x.sec.fi + dx[i], newj = x.sec.sec + dy[i];
ll cost = (dir[i] != grid[x.sec.fi][x.sec.sec]);
if(newi < 1 || newj < 1 || newi > n || newj > m || dist[newi][newj] < dist[x.sec.fi][x.sec.sec] + cost) continue;
dist[newi][newj] = dist[x.sec.fi][x.sec.sec] + cost;
pq.push({dist[newi][newj], {newi, newj}});
}
}
cout << (dist[n][m] == 1e18 ? -1 : dist[n][m]) << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |