일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 독일어독학
- BFS
- 백준
- 스택
- dp
- 카카오인턴
- IOS
- 리눅스
- 프로그래머스
- 부주상골수술후기
- 카카오코테
- sql
- SQLD
- 구현
- 세브란스
- 롯데정보통신
- SWIFT
- 코테
- 카카오인턴십
- 부주상골수술
- 코딩테스트
- ChatGPT
- istringstream
- 부주상골
- 부주상골증후군
- 분할정복
- 독학
- 독일어
- DFS
- c++
Archives
- Today
- Total
슈뢰딩거의 고등어
19237 어른상어 본문
https://www.acmicpc.net/problem/19237
19237번: 어른 상어
첫 줄에는 N, M, k가 주어진다. (2 ≤ N ≤ 20, 2 ≤ M ≤ N2, 1 ≤ k ≤ 1,000) 그 다음 줄부터 N개의 줄에 걸쳐 격자의 모습이 주어진다. 0은 빈칸이고, 0이 아닌 수 x는 x번 상어가 들어있는 칸을 의미
www.acmicpc.net
골드 3
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
struct SHARK {
int y, x, dir;
int pri[4][4]; // up, down, left, right
};
struct BLOCK {
int shark_no;
int time; // time when its spread
};
// arr_size, shark_no, time_counter
int n, m, k;
int answer;
BLOCK arr[21][21];
SHARK shark[400];
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
// sum : how many shark removed
void solve(int sum, int sec) {
// update map
for(int i=0; i<n; i++)
for(int j= 0; j<n; j++) {
if(arr[i][j].time != 0) {
arr[i][j].time--;
if(arr[i][j].time == 0)
arr[i][j].shark_no = -1;
}
}
if(sum == m-1) {
answer = sec;
return;
}
if(sec >= 1000) {
answer = -1;
return;
}
// spread
for(int i=0; i<m; i++) {
if (shark[i].y == -1)
continue;
// 마주쳤음. i 제거
if(arr[shark[i].y][shark[i].x].time == k) {
shark[i].y = -1;
shark[i].x = -1;
shark[i].dir = -1;
sum++;
continue;
}
// 이동
arr[shark[i].y][shark[i].x].shark_no = i;
arr[shark[i].y][shark[i].x].time = k;
}
// choose target
for(int s=0; s<m; s++) {
// 죽은 상어 고려 x
if(shark[s].y == -1)
continue;
// 현재 위치
int cy = shark[s].y;
int cx = shark[s].x;
int cd = shark[s].dir;
bool is_exists = false;
int target_y, target_x, target_d;
// turn
for(int d=0; d<4; d++) {
int nd = shark[s].pri[cd][d];
int ny = cy + dy[nd];
int nx = cx + dx[nd];
if(ny < 0 || ny >= n || nx < 0 || nx >= n)
continue;
// 냄새 없는 칸 존재
if(arr[ny][nx].shark_no == -1) {
is_exists = true;
target_y = ny;
target_x = nx;
target_d = nd;
break;
}
// 냄새 영향이 아직 있고
// 본인의 냄새가 존재하는 위치를 저장, 우선순의에 의해 처음 값만 저장
if(arr[ny][nx].shark_no == s && is_exists == false) {
is_exists = true;
target_y = ny;
target_x = nx;
target_d = nd;
}
}
// 목적지 설정
if (is_exists) {
shark[s].y = target_y;
shark[s].x = target_x;
shark[s].dir = target_d;
}
}
solve(sum, sec+1);
}
int main() {
scanf("%d %d %d", &n, &m, &k);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++) {
int shark_no;
scanf("%d", &shark_no);
shark_no--;
arr[i][j].shark_no = shark_no;
if(shark_no != -1) {
shark[shark_no].y = i;
shark[shark_no].x = j;
}
}
for(int i=0; i<m ;i++) {
scanf("%d", &shark[i].dir);
shark[i].dir--;
}
for(int i=0; i<m; i++) {
for(int j=0; j<4; j++) {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
a--; b--; c--; d--;
shark[i].pri[j][0] = a;
shark[i].pri[j][1] = b;
shark[i].pri[j][2] = c;
shark[i].pri[j][3] = d;
}
}
solve(0, -1);
printf("%d\n", answer);
return 0;
}
shark, block 구조체 정의
각 상어마다 이동 우선순위가 다름
상어의 이동시 냄새 시간 카운트
'알고리즘' 카테고리의 다른 글
14888 연산자 끼워넣기 ( DFS : 중복순열 || next_permutation) (0) | 2021.12.10 |
---|---|
17143 낚시왕 (0) | 2021.12.09 |
level 2 게임 맵 최단거리 (BFS) (0) | 2021.12.06 |
level 2 소수찾기 -순열 (algorithm :: next_permutaion) (0) | 2021.12.06 |
19236 청소년상어 (cstring :: memcpy) (0) | 2021.12.06 |
Comments