반응형
단순 구현문제이다.
소요시간(약 2시간50분)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M, K;
static Smell[][] grid;
static Shark[] sharks;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
grid = new Smell[N][N];
sharks = new Shark[M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
int sharkNum = Integer.parseInt(st.nextToken());
if (sharkNum != 0) {
sharks[sharkNum - 1] = new Shark(i, j, sharkNum);
grid[i][j] = new Smell(sharkNum, K);
}
}
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) {
sharks[i].setSharkArrow(Integer.parseInt(st.nextToken()) - 1);
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < 4; j++) {
st = new StringTokenizer(br.readLine());
for (int k = 0; k < 4; k++) {
sharks[i].setLook_preferArrow(j, k, Integer.parseInt(st.nextToken()) - 1);
}
}
}
//for (int ss = 0; ss < M; ss++) sharks[ss].print();
//System.out.println("===========");
int answer=-1;
for(int i=1; i<=1000; i++) {
//System.out.println(i+"------------------");
start();
if(delCount==M-1){
answer=i;
break;
}
}
System.out.println(answer);
}
static int[] dirR = {-1, 1, 0, 0};
static int[] dirC = {0, 0, -1, 1};
static int delCount=0;
static void start() {
int[][] alreadyFull = new int[N][N];
for (int i = 0; i < M; i++) {
if(sharks[i] == null) continue;
int r = sharks[i].r;
int c = sharks[i].c;
//System.out.println(r+1 + "," +(c+1) + "-->snum:"+sharks[i].snum +"-->arrow:"+(sharks[i].sharkArrow+1));
//1. 먼저 인접한 칸 중 아무 냄새가 없는 칸의 방향으로 잡는다
boolean pass = false;
for (int d = 0; d < 4; d++) {//상하좌우순서대로라고 가정
int dir = sharks[i].look_preferArrow[sharks[i].sharkArrow][d];
//System.out.println(dir);
int mr = r + dirR[dir];
int mc = c + dirC[dir];
//System.out.println("check" + dir);
if (mr < 0 || mr >= N) continue;
if (mc < 0 || mc >= N) continue;
//System.out.println("check" + dir);
if (grid[mr][mc] == null) {
sharks[i].setR(mr);
sharks[i].setC(mc);
sharks[i].setSharkArrow(dir);
pass = true;
break;
}
}
if (pass) {
//System.out.println(i + "첫번쨰");
continue;
}
//그런 칸이 없으면 자신의 냄새가 있는 칸의 방향으로 잡는다.
for (int d = 0; d < 4; d++) {//상하좌우순서대로라고 가정
int dir = sharks[i].look_preferArrow[sharks[i].sharkArrow][d];
int mr = r + dirR[dir];
int mc = c + dirC[dir];
if (mr < 0 || mr >= N) continue;
if (mc < 0 || mc >= N) continue;
if (grid[mr][mc] != null &&
grid[mr][mc].sharkNum == sharks[i].snum) {
sharks[i].setR(mr);
sharks[i].setC(mc);
sharks[i].setSharkArrow(dir);
pass = true;
break;
}
}
//if (pass) System.out.println(i + "두번쨰");
}
for(int i=0; i<M; i++){
if(sharks[i] == null) continue;
int mr=sharks[i].r;
int mc=sharks[i].c;
if(alreadyFull[mr][mc] == 0) {
grid[mr][mc] = new Smell(sharks[i].snum, K + 1);
alreadyFull[mr][mc] = sharks[i].snum;
}
else{
sharks[i] = null;
delCount++;
//System.out.println(delCount);
}
}
//smell 1씩감소
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == null) {
//.out.print(0 + " ");
continue;
}
if (grid[i][j].timeDown()) {
//System.out.print(0 + " ");
grid[i][j] = null;//timedown에서 0이되면 객체소멸
}else{
//System.out.print(grid[i][j].timeLeft+ " ");
}
}
//System.out.println();
}
}
}
class Smell {
int sharkNum;
int timeLeft;
Smell(int snum, int t) {
sharkNum = snum;
timeLeft = t;
}
boolean timeDown() {
timeLeft = timeLeft - 1;
if (timeLeft == 0) return true;
return false;
}
}
class Shark {
int r, c, snum;
int[][] look_preferArrow = new int[4][4];
int sharkArrow = 0;
Shark(int r, int c, int sn) {
this.r = r;
this.c = c;
snum = sn;
}
void print() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) System.out.print(look_preferArrow[i][j] + " ");
System.out.println();
}
}
void setLook_preferArrow(int index, int jndex, int a) {
look_preferArrow[index][jndex] = a;
}
void setSharkArrow(int arrow) {
sharkArrow = arrow;
}
public void setR(int r) {
this.r = r;
}
public void setC(int c) {
this.c = c;
}
}
반응형
'코딩테스트 > Java' 카테고리의 다른 글
[JAVA] 백준 새로운게임2 (0) | 2021.10.20 |
---|---|
[JAVA] 백준 게리맨더링2 (0) | 2021.10.05 |
[JAVA] 백준 경사로 (0) | 2021.09.28 |
(2021 카카오) 합승 택시 요금 JAVA (0) | 2021.09.24 |
[JAVA] 백준 마법사 상어와 비바라기 (0) | 2021.09.23 |