This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int INF = 1e9 + 69;
int dx[] = {0, 0, -1, 1, 0};
int dy[] = {1, -1, 0, 0, 0};
int convert(char c){
if (c == '>') return 0;
if (c == '<') return 1;
if (c == '^') return 2;
if (c == 'v') return 3;
if (c == '.') return 4;
return 5;
}
char convert_back(int c){
if (c == 0) return '>';
if (c == 1) return '<';
if (c == 2) return '^';
return 'v';
}
void solve(){
int n, m; cin >> n >> m;
vector<string> grid(n);
for(int i = 0; i<n; ++i) cin >> grid[i];
pair<int, int> s, e;
for(int i = 0; i<n; ++i) for(int j = 0; j < m; ++j)
if (grid[i][j] == 'o') s = {i, j};
else if (grid[i][j] == 'x') e = {i, j};
int dis[n][m], trace[n][m]; memset(trace, 0, sizeof trace);
for(int i = 0; i<n; ++i) for(int j = 0; j<m; ++j) dis[i][j] = INF;
bool visited[n][m]; memset(visited, 0, sizeof visited);
dis[s.first][s.second] = 0;
deque<pair<int,int>> q; q.push_back(s);
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
if (visited[u.first][u.second]) continue;
visited[u.first][u.second] = true;
int current_cell = convert(grid[u.first][u.second]);
if (current_cell <= 4){
pair<int, int> v = {u.first + dx[current_cell], u.second + dy[current_cell]};
if (0 <= v.first && v.first < n && 0 <= v.second && v.second < m && minimize(dis[v.first][v.second], dis[u.first][u.second])){
q.push_front(v);
trace[v.first][v.second] = current_cell;
}
}
for(int dir = 0; dir < 4; ++dir){
pair<int, int> v = {u.first + dx[dir], u.second + dy[dir]};
if (0 <= v.first && v.first < n && 0 <= v.second && v.second < m){
int cur_cost = dis[u.first][u.second] + (current_cell != 5);
if (minimize(dis[v.first][v.second], cur_cost)){
if (current_cell == 5) q.push_front(v);
else q.push_back(v);
trace[v.first][v.second] = dir;
}
}
}
}
cout << dis[e.first][e.second] << "\n";
while(e != s){
int dir = trace[e.first][e.second];
e.first -= dx[dir]; e.second -= dy[dir];
grid[e.first][e.second] = convert_back(dir);
}
grid[s.first][s.second] = 'o';
printArr(grid, "\n");
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |