답안 #487842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
487842 2021-11-16T19:18:14 Z inksamurai Patkice (COCI20_patkice) C++17
50 / 50
1 ms 336 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 _3HaBFkZ 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>;

int main(){
_3HaBFkZ;
	int h,w;
	cin>>h>>w;
	vec(vec(char)) a(h,vec(char)(w));
	rep(i,h)rep(j,w) cin>>a[i][j];
	pii hm={-1,-1};
	rep(i,h)rep(j,w){
		if(a[i][j]=='o'){
			hm={i,j};
			break;
		}
	}
	const int di[]={1,-1,0,0};
	const int dj[]={0,0,1,-1};
	auto ok=[&](int x,int y)->bool{
		return !(min(x,y)<0 or x>=h or y>=w);
	};
	vec(vi) usd(h,vi(w));
	int valid=1e9;
	auto dfs=[&](auto self,int x,int y,int cnt){
		if(usd[x][y]) return;
		usd[x][y]=1;
		if(a[x][y]=='.' or a[x][y]=='o') return;
		if(a[x][y]=='x'){
			valid=cnt;
			return;
		}
		char ch=a[x][y];
		int dir=(ch=='v'?0:ch=='>'?2:ch=='<'?3:1);
		int nex=x+di[dir],ney=y+dj[dir];
		if(ok(nex,ney) and usd[nex][ney]==0){
			self(self,nex,ney,cnt+1);
		}
	};
	auto dirtochar=[](int dir)->char{
		if(dir==1){
			return 'N';
		}
		if(dir==0){
			return 'S';
		}
		if(dir==2){
			return 'E';
		}
		return 'W';
	};
	int panstime=1e9;
	char pans='Z';
	rep(dir,4){
		int nex=hm.fi+di[dir],ney=hm.se+dj[dir];
		if(ok(nex,ney)){
			usd=vec(vi)(h,vi(w,0));
			valid=1e9;
			dfs(dfs,nex,ney,0);
			if(panstime>valid){
				panstime=valid;
				pans=dirtochar(dir);
			}else if(panstime==valid){
				pans=min(pans,dirtochar(dir));
			}
		}
	}
	if(panstime==1e9){
		cout<<":(\n";
	}else{
		cout<<":)\n";
		cout<<pans<<"\n";
	}
//	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 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 1 ms 208 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 0 ms 336 KB Output is correct
16 Correct 0 ms 208 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 0 ms 208 KB Output is correct
20 Correct 0 ms 208 KB Output is correct