// cre: nnhzzz
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <cassert>
using namespace std;
//-------------------------------------------------------------//
#define __nnhzzz__ signed main()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define repdis(i,be,en,j) for(int i = (be); i<=(en); i+=j)
#define repd(i,be,en) for(int i = (be); i>=(en); i--)
#define rep(i,be,en) for(int i = (be); i<=(en); i++)
#define reset(x,val) memset((x),(val),sizeof(x))
#define bit(i,j) ((i>>j)&1ll)
#define mask(i) (1ll<<i)
#define FULL(i) (mask(i)-1)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vpii vector<pair<int,int>>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define endl "\n"
#define ull unsigned long long
#define ll long long
#define ld long double
// #define int long long
#define fi first
#define se second
#define C make_pair
#define vec vector
//-------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
//-------------------------------------------------------------//
const int maxn = 2e3+7;
const int N = 4e6+7;
const int MOD = 1e9+7;
// const int oo = 1e18+7;
const int INF = 0x3f;
// const int LINF = 0x3f3f3f3f3f3f3f3f;
// const int base = 311;
// const ld PI = 3.1415926535897932384626433832795;
// const ld EPS = 1e-9;
//-------------------------------------------------------------//
// int dx[] = {-1, 1, 0, 0};
// int dy[] = {0, 0, -1, 1};
vector<pair<pii,char>> adj[N];
char ch[maxn][maxn],fromc[N];
int dis[N],from[N];
int n,m,sx,sy,ex,ey;
void add(int u, int v, int w, char c){
if(u==-1 || v==-1){
return ;
}
adj[u].push_back({C(v,w),c});
}
int encode(int i, int j){
if(i<0 || j<0 || i>=n || j>=m){
return -1ll;
}
return (i*maxn+j+1);
}
pii decode(int val){
return {val/maxn,val%maxn-1};
}
void solve(){
cin >> n >> m;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
cin >> ch[i][j];
if(ch[i][j]=='o'){
sx = i; sy = j;
}
if(ch[i][j]=='x'){
ex = i; ey = j;
}
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
int a = (ch[i][j]!='>');
int b = (ch[i][j]!='^');
int c = (ch[i][j]!='<');
int d = (ch[i][j]!='v');
if(ch[i][j]=='o'){
a = b = c = d = 0;
}
add(encode(i,j),encode(i,j+1),a,'>');
add(encode(i,j),encode(i-1,j),b,'^');
add(encode(i,j),encode(i,j-1),c,'<');
add(encode(i,j),encode(i+1,j),d,'v');
}
}
priority_queue<pii,vpii,greater<pii>> heap;
fill(dis,dis+N,MOD);
heap.push({0ll,encode(sx,sy)});
dis[encode(sx,sy)] = 0;
while(sz(heap)>0){
auto [ww,u] = heap.top();
heap.pop();
if(dis[u]<ww){
continue;
}
for(auto [it,c]:adj[u]){
auto [v,w] = it;
if(mini(dis[v],dis[u]+w)){
fromc[v] = c;
from[v] = u;
heap.push(C(dis[v],v));
}
}
}
cout << dis[encode(ex,ey)] << endl;
while(ch[ex][ey]!='o'){
char c = fromc[encode(ex,ey)];
auto val = decode(from[encode(ex,ey)]);
ex = val.fi; ey = val.se;
if(ex==sx && ey==sy){
break;
}
ch[ex][ey] = c;
}
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
cout << ch[i][j];
}
cout << endl;
}
}
//-------------------------------------------------------------//
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "test"
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:147:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
147 | auto [ww,u] = heap.top();
| ^
Main.cpp:152:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
152 | for(auto [it,c]:adj[u]){
| ^
Main.cpp:153:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
153 | auto [v,w] = it;
| ^
Main.cpp: In function 'int main()':
Main.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
186 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
109908 KB |
Output is correct |
2 |
Correct |
49 ms |
109976 KB |
Output is correct |
3 |
Correct |
53 ms |
109980 KB |
Output is correct |
4 |
Correct |
50 ms |
110004 KB |
Output is correct |
5 |
Correct |
54 ms |
109852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
109908 KB |
Output is correct |
2 |
Correct |
49 ms |
109976 KB |
Output is correct |
3 |
Correct |
53 ms |
109980 KB |
Output is correct |
4 |
Correct |
50 ms |
110004 KB |
Output is correct |
5 |
Correct |
54 ms |
109852 KB |
Output is correct |
6 |
Correct |
200 ms |
148856 KB |
Output is correct |
7 |
Correct |
245 ms |
153452 KB |
Output is correct |
8 |
Correct |
451 ms |
205292 KB |
Output is correct |
9 |
Correct |
810 ms |
266200 KB |
Output is correct |
10 |
Correct |
1100 ms |
331748 KB |
Output is correct |
11 |
Correct |
1386 ms |
364672 KB |
Output is correct |