답안 #489416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
489416 2021-11-22T22:42:34 Z inksamurai Patkice II (COCI21_patkice2) C++17
110 / 110
851 ms 43308 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3oIUrY0 ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;

const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};

const int inf=1e9;

int main(){
_3oIUrY0;
	int h,w;
	cin>>h>>w;
	vec(vec(char)) a(h,vec(char)(w));
	rep(i,h)rep(j,w){
		cin>>a[i][j];
	}
	int home=0,to=0;
	rep(i,h)rep(j,w){
		if(a[i][j]=='o') home=i*w+j;
		if(a[i][j]=='x') to=i*w+j;
	}
	int n=h*w;
	priority_queue<pii,vec(pii),greater<pii>> pq;
	vec(pii) dp(n,{inf,-1});
	dp[home]={0,-1};
	pq.push({0,home});
	auto ok=[&](int x,int y)->bool{
		return (min(x,y)>=0 and x<h and y<w);
	};
	auto vco=[&](char ch,int dir)->int{
		if(ch=='o') return 0;
		int _dir=-1;
		if(ch=='^') _dir=1;
		if(ch=='v') _dir=0;
		if(ch=='>') _dir=2;
		if(ch=='<') _dir=3;
		return !(_dir==dir);
	};
	while(sz(pq)){
		pii top=pq.top();
		pq.pop();
		int v=top.se,cost=top.fi;
		int x=v/w,y=v%w;
		if(dp[v].fi!=cost or v==to) continue;
		rep(dir,4){
			int nex=x+di[dir],ney=y+dj[dir];
			if(ok(nex,ney)){
				int u=nex*w+ney;
				int necost=cost+vco(a[x][y],dir);
				if(dp[u].fi>necost){
					pq.push({necost,u});
					dp[u]={necost,v};
				}
			}
		}
	}
	cout<<dp[to].fi<<"\n";
	while(true){
		int x=to/w,y=to%w;
		int neto=dp[to].se;
		if(neto==-1) break;
		int nex=neto/w,ney=neto%w;
		char nech=a[nex][ney];
		if(nech!='o'){
			if(nex==x-1){
				a[nex][ney]='v';
			}else if(nex==x+1){
				a[nex][ney]='^';
			}else if(ney==y-1){
				a[nex][ney]='>';
			}else{
				a[nex][ney]='<';
			}
		}
		to=neto;
	}
	rep(i,h){
		rep(j,w) cout<<a[i][j];
		cout<<"\n";
	}
//	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 91 ms 5628 KB Output is correct
7 Correct 104 ms 6716 KB Output is correct
8 Correct 217 ms 14128 KB Output is correct
9 Correct 482 ms 25536 KB Output is correct
10 Correct 579 ms 34144 KB Output is correct
11 Correct 851 ms 43308 KB Output is correct