티스토리 뷰
https://www.acmicpc.net/problem/17837
17837번: 새로운 게임 2
재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되고, 사용하는 말의 개수는 K개이다. 말은 원판모양이고, 하�
www.acmicpc.net
C++ 소스 코드
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int n,k;
struct pos{
int x,y,d;
};
vector<pos> v;
const int dx[]={0,0,-1,1}, dy[]={1,-1,0,0};
int a[13][13];
string mp[13][13] = {""};
bool move(int x, int y, int d, char target){
string now = mp[x][y].substr(mp[x][y].find(target));
int nx = x+dx[d],ny = y+dy[d];
if(nx<1 || nx>n || ny<1 || ny>n || a[nx][ny]==2) {
//체스판을 벗어나거나, 다음칸이 파란색일경우 방향을 바꿈
if(d == 1 || d ==3){
d--;
}else if(d==0 || d == 2){
d++;
}
v[target-'0'].d = d;
if(x+dx[d]<1 || x+dx[d]>n || y+dy[d]<1 || y+dy[d]>n || a[x+dx[d]][y+dy[d]]==2){
//그 다음칸도 파란색이거나 범위를 벗어나면 움직이지 않음
return false;
}
}
nx = x+dx[d],ny = y+dy[d];
if (a[nx][ny]==1){ //다음칸이 빨간색이면 순서바꿈
reverse(now.begin(),now.end());
}
for(int i=0;i<now.length();i++){
v[now[i]-'0'].x =nx;
v[now[i]-'0'].y =ny;
}
mp[x][y].erase(mp[x][y].find(target));
mp[nx][ny]+=now;
if(mp[nx][ny].length()>=4) return true;
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> a[i][j];
}
}
v.resize(k);
for(int i=0;i<k;i++){
cin >> v[i].x >> v[i].y >> v[i].d;
v[i].d--;
mp[v[i].x][v[i].y]+=to_string(i);
}
for(int time = 1;time<=1000;time++){
for(int i=0;i<k;i++){
if(move(v[i].x,v[i].y, v[i].d, i+'0')){
cout << time << '\n';
return 0;
}
}
}
cout << -1 << '\n';
return 0;
}
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
프로그래머스) Lv2 - 쿼드압축 후 개수 세기 (0) | 2020.10.30 |
---|---|
BOJ) 10451 - 순열 사이클 (0) | 2020.10.22 |
BOJ) 1389 - 케빈 베이컨의 6단계 법칙 (0) | 2020.10.08 |
BOJ) 12869 - 뮤탈리스크 (0) | 2020.10.06 |
BOJ) 2206 - 벽 부수고 이동하기 (0) | 2020.10.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ios
- blendshape
- 백준
- SwiftUI
- cocoapods
- bounds
- 프로그래머스
- blendshapes
- Kotlin
- 알고리즘
- 코코아팟
- GraphDB
- Swift
- rxswift
- rxswift6
- infallible
- Neo4j
- C++
- Lottie
- 카카오인턴십
- mergesort
- SWEA
- ARKit
- Reactivex
- 안드로이드
- boj
- DispatchQueue
- coreml
- 백준온라인저지
- disposeBag
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함