https://www.acmicpc.net/problem/17837
[난이도] Gold2
[유형] 시뮬레이션
[풀이]
구현능력을 보는 시뮬레이션 문제입니다.
풀이법이랄게 딱히 없으며 주어진 조건에 맞게 구현해주면 됩니다.
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N,K,board[14][14];
int dy[4]={0,0,-1,1};
int dx[4]={1,-1,0,0};
struct P{
int num,d;
};
pair<int,int> pos[11];
vector<P> horse[14][14];
int cd(int d){
return d%2==0 ? d+1 : d-1;
}
bool move(int c,int y,int x,vector<P> v,int d){
v[0].d=d;
if(c==1) reverse(v.begin(),v.end());
for(auto p : v) {
horse[y][x].push_back(p);
pos[p.num]={y,x};
}
return (int)horse[y][x].size()>=4;
}
int main(){
scanf("%d%d",&N,&K);
for(int i=0;i<=N+1;i++){
for(int j=0;j<=N+1;j++) {
if(i==N+1||j==N+1||i==0||j==0) board[i][j]=2;
else scanf("%d",&board[i][j]);
}
}
for(int i=1;i<=K;i++){
int y,x,d;
scanf("%d%d%d",&y,&x,&d);
horse[y][x].push_back({i,d-1});
pos[i]={y,x};
}
for(int ans=1;ans<=1000;ans++){
for(int k=1;k<=K;k++){
auto [y,x] = pos[k];
int d,idx;
vector<P> tmp;
for(int i=0;i<horse[y][x].size();i++){
if(horse[y][x][i].num==k){
idx=i;
d=horse[y][x][i].d;
break;
}
}
for(int i=idx;i<horse[y][x].size();i++) tmp.push_back(horse[y][x][i]);
horse[y][x].erase(horse[y][x].begin()+idx,horse[y][x].end());
int ny=y+dy[d],nx=x+dx[d];
bool ret=0;
if(board[ny][nx]==2) {
d=cd(d);
int nny=y+dy[d],nnx=x+dx[d];
if(board[nny][nnx]==2) ret=move(0,y,x,tmp,d);
else ret=move(board[nny][nnx],nny,nnx,tmp,d);
}else{
ret=move(board[ny][nx],ny,nx,tmp,d);
}
if(ret) {
printf("%d",ans);
return 0;
}
}
}
puts("-1");
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold2/17837.cpp
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold2] 17090 : 미로 탈출하기 (C++) (0) | 2022.03.03 |
---|---|
[BOJ/백준][Gold2] 1949 : 우수 마을 (C++) (0) | 2022.02.26 |
[BOJ/백준][Gold3] 14505 : 팰린드롬 개수 구하기 (Small) (C++) (0) | 2022.02.26 |
[BOJ/백준][Gold3] 1278 : 연극 (C++) (0) | 2022.02.26 |
[BOJ/백준][Gold3] 13144 : List of Unique Numbers (C++) (0) | 2022.02.20 |