곽로그
백준 19236 청소년 상어 (Java) 본문
반응형
* 해당 포스팅은 구버전
* alwaysbemoon.tistory.com/228
1. 아ㅏㅏ아아아 객체객체객체객체 .... 어렵다 객체
1) 객체를 매개변수로 넘길때는 반드시 복사
2) 객체 값을 변경시키고서 매개변수로 넘기면 다시 돌아왔을 때 초기값으로 함수수행하지 않고 변경된 값으로 수행 (이거때문에 하루 날림 ㅜㅜ)
- currentShark.x 를 변경하면 안됨
for(int d = 1;d<=4; d++) {
int moveSharkX = currentShark.x + dx[currentShark.direction]*d;
int moveSharkY = currentShark.y + dy[currentShark.direction]*d;
if(moveSharkX<0 || moveSharkX>= 4 ||moveSharkY<0 ||moveSharkY>=4) {
return;
}
else{
if(tempMap[moveSharkX][moveSharkY] == BLANK) {
continue;
}
else {
tempMap[currentShark.x][currentShark.y] =0;
System.out.println("("+currentShark.x+","+currentShark.y+")=0");
currentShark.x = moveSharkX;
currentShark.y= moveSharkY;
eatFish(currentShark,tempMap,tempFishList);
}
}
}
3) 객체배열은 꼭 초기화를 해야하고 null이 있으면 안돼요....그리고 객체를 복사할 때는 깊은복사를 해야합니다.
이거 자바책에서 그렇게 밑줄친건데 ㅜㅜ
public static void copyList(Fish[]source, Fish[] dest) {
for(int i=0;i<source.length;i++) {
dest[i] = source[i];
}
}
위가 아니라
아래
public static void copyList(Fish[]source, Fish[] dest) {
for(int i=1;i<source.length;i++) {
if(source[i]!=null) {
dest[i] = new Fish();
dest[i].x = source[i].x;
dest[i].y = source[i].y;
dest[i].number = source[i].number;
dest[i].direction =source[i].direction;
}
}
}
코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
class Fish{
int number;
int x;
int y;
int direction;
Fish(int number, int x , int y, int direction){
this.number = number;
this.x =x ;
this.y =y;
this.direction = direction;
}
Fish(){
}
@Override
public String toString() {
return "number:"+number+"("+x+","+y+"),d :"+direction;
}
}
public class Main{
public static int[][] map;
public static int SHARK = -1;
public static int BLANK = 0;
public static int[] dx = {-1,-1, 0, 1,1,1, 0,-1};
public static int[] dy = { 0,-1,-1,-1,0,1, 1, 1};
public static int maxFishSum = 0;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
map = new int[4][4];
Fish[] fishList = new Fish[17];
for(int r=0;r<4;r++) {
int c =0;
int fishNumber = 0;
int fishX = 0;
int fishY = 0;
int fishDirection = 0;
st = new StringTokenizer(br.readLine(), " ");
fishNumber = Integer.parseInt(st.nextToken());
fishDirection = Integer.parseInt(st.nextToken())-1;
fishX = r;
fishY = c;
fishList[fishNumber] = new Fish(fishNumber ,fishX, fishY,fishDirection);
map[fishX][fishY] =fishNumber;
c++;
fishNumber = Integer.parseInt(st.nextToken());
fishDirection = Integer.parseInt(st.nextToken())-1;
fishX = r;
fishY = c;
fishList[fishNumber] = new Fish(fishNumber ,fishX, fishY,fishDirection);
map[fishX][fishY] =fishNumber;
c++;
fishNumber = Integer.parseInt(st.nextToken());
fishDirection = Integer.parseInt(st.nextToken())-1;
fishX = r;
fishY = c;
fishList[fishNumber] = new Fish(fishNumber ,fishX, fishY,fishDirection);
map[fishX][fishY] =fishNumber;
c++;
fishNumber = Integer.parseInt(st.nextToken());
fishDirection = Integer.parseInt(st.nextToken())-1;
fishX = r;
fishY = c;
fishList[fishNumber] = new Fish(fishNumber ,fishX, fishY,fishDirection);
map[fishX][fishY] =fishNumber;
c++;
}
eatFish(0, 0 ,0,0,map,fishList);
bw.write(String.valueOf(maxFishSum));
bw.flush();
}
public static void eatFish(int sx, int sy, int sd, int sum, int[][] map, Fish[] fishList) {
int[][] tempMap = new int[4][4];
copyMap(map,tempMap);
Fish[] tempFishList = new Fish[17];
copyList(fishList,tempFishList);
int eatFishNumber = tempMap[sx][sy];
Fish eatFish = tempFishList[eatFishNumber];
//상어상태 업데이트
sd = eatFish.direction;
sum += eatFishNumber;
maxFishSum = Math.max(maxFishSum, sum);
//물고기리스트 업데이트
tempFishList[eatFishNumber] = null;
//map업데이트
tempMap[sx][sy] = SHARK;
/* 물고기 이동 */
for(int number = 1; number<tempFishList.length;number++) {
if(tempFishList[number]!=null) {
//이동할 물고기
Fish moveFish = tempFishList[number];
//이동 탐색
for(int d = moveFish.direction; d<moveFish.direction+8; d++) {
d = d%8;
int mx = moveFish.x + dx[d];
int my = moveFish.y +dy[d];
//경계 밖
if(mx<0 || mx>=4 || my<0 || my>=4) {
continue;
}
//경계 안
else {
if(tempMap[mx][my] == SHARK) {
continue;
}
else if(tempMap[mx][my] == BLANK) {
//빈칸으로 물고기 이동(물고기의 좌표 변경)
tempMap[moveFish.x][moveFish.y] =BLANK;
moveFish.x = mx;
moveFish.y = my;
moveFish.direction =d;
tempMap[mx][my] = number;
tempFishList[number] = moveFish;
break;
}
else {
//물고기 자리 변경
int changeFishNumber = tempMap[mx][my];
Fish changeFish = tempFishList[changeFishNumber];
//map 상태 업데이트
tempMap[moveFish.x][moveFish.y] = changeFishNumber;
tempMap[changeFish.x][changeFish.y] = number;
//물고기 상태 업데이트
int currentFishX = moveFish.x;
int currentFishY = moveFish.y;
int currentFishD = d;
int changeFishX = changeFish.x;
int changeFishY = changeFish.y;
tempFishList[number].x = changeFishX;
tempFishList[number].y = changeFishY;
tempFishList[number].direction =currentFishD;
tempFishList[changeFishNumber].x = currentFishX;
tempFishList[changeFishNumber].y = currentFishY;
break;
}
}
}
}
}
/* 상어이동 이동 */
for(int d = 1;d<=4; d++) {
int moveSharkX = sx + dx[sd]*d;
int moveSharkY = sy + dy[sd]*d;
if(moveSharkX<0 || moveSharkX>= 4 ||moveSharkY<0 ||moveSharkY>=4) {
break;
}
else{
if(tempMap[moveSharkX][moveSharkY] == BLANK) {
continue;
}
else {
tempMap[sx][sy] =0;
eatFish(moveSharkX,moveSharkY,sd,sum,tempMap,tempFishList);
}
}
}
}
public static void copyList(Fish[]source, Fish[] dest) {
for(int i=1;i<source.length;i++) {
if(source[i]!=null) {
dest[i] = new Fish();
dest[i].x = source[i].x;
dest[i].y = source[i].y;
dest[i].number = source[i].number;
dest[i].direction =source[i].direction;
}
}
}
public static void copyMap(int[][] source, int[][] dest) {
for(int r=0;r<4;r++) {
for(int c=0;c<4;c++)
dest[r][c] = source[r][c];
}
}
public static void printMap(int[][] m) {
for(int r=0;r<4;r++) {
for(int c=0;c<4;c++) {
System.out.print(m[r][c]+" ");
}
System.out.println();
}
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 1935, Java] 후위표기식2 (0) | 2020.10.26 |
---|---|
[백준1158, Java] 요세푸스 문제 (0) | 2020.10.20 |
[백준 15686, Java] 치킨배달 (0) | 2020.10.15 |
[백준 14499, Java] 주사위 굴리기 (0) | 2020.10.15 |
[백준 12100, Java] 2046(easy) (0) | 2020.10.14 |
Comments