#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_NUM=2000;
const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};
char c[MAX_NUM+2][MAX_NUM+2];
int t[MAX_NUM+2][MAX_NUM+2],d[MAX_NUM+2][MAX_NUM+2];
int numrow,numcol;
int type(char c){
if (c=='<') return 0;
if (c=='>') return 1;
if (c=='^') return 2;
if (c=='v') return 3;
return -1;
}
struct Node{
int x,y;
int cost;
bool operator < (const Node&other) const{
return cost>other.cost;
}
};
struct Store{
int t,x,y;
};
Store trace[MAX_NUM+2][MAX_NUM+2];
bool possible(int u,int v){
if (u<=0||u>numrow||v<=0||v>numcol) return false;
if (c[u][v]=='o') return false;
return true;
}
char change(int t){
if (t==0) return '<';
if (t==1) return '>';
if (t==2) return '^';
if (t==3) return 'v';
return '.';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0) ; cout.tie(0);
#define task "main"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>numrow>>numcol;
pair<int,int>start={-1,-1};
pair<int,int>end_point={-1,-1};
for(int i=1;i<=numrow;++i){
for(int j=1;j<=numcol;++j){
cin>>c[i][j];
t[i][j]=type(c[i][j]);
if (c[i][j]=='o') {
start={i,j};
}
else if (c[i][j]=='x') {
end_point={i,j};
}
}
}
priority_queue<Node>q;
assert(start.first!=-1 && start.second!=-1);
memset(d,0x3f,sizeof d);
for(int i=0;i<4;++i){
int nxt_u=start.first+dx[i],nxt_v=start.second+dy[i];
if (possible(nxt_u,nxt_v)==false) continue;
d[nxt_u][nxt_v]=0;
q.push({nxt_u,nxt_v,d[nxt_u][nxt_v]});
}
while (q.size()){
int u=q.top().x,v=q.top().y;
int cost=q.top().cost;
q.pop();
if (cost!=d[u][v]) continue;
if (c[u][v]=='x') break;
for(int i=0;i<4;++i){
int nxt_u=u+dx[i],nxt_v=v+dy[i];
if (possible(nxt_u,nxt_v)==false) continue;
int change_cost=1;
if (t[u][v]==i) change_cost=0;
if (d[nxt_u][nxt_v]>cost+change_cost){
d[nxt_u][nxt_v]=cost+change_cost;
trace[nxt_u][nxt_v]={i,u,v};
q.push({nxt_u,nxt_v,d[nxt_u][nxt_v]});
}
}
}
int u=end_point.first,v=end_point.second;
while (true){
Store x=trace[u][v];
if (x.x==0&&x.y==0) break;
c[x.x][x.y]=change(x.t);
u=x.x,v=x.y;
}
cout<<d[end_point.first][end_point.second]<<'\n';
for(int i=1;i<=numrow;++i){
for(int j=1;j<=numcol;++j) cout<<c[i][j];
cout<<'\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:51:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:52:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |