반응형
dackyy
대기의 연대기
dackyy
전체 방문자
오늘
어제
  • 분류 전체보기 (49)
    • java (7)
    • 코딩테스트 (23)
    • python (10)
    • Network (2)
    • Web (7)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • java
  • 제어문
  • 비교 연산자
  • switch
  • 반복문
  • 배열생성
  • 증감 연산자
  • 배열
  • TLS
  • 시프트 연산자
  • 조건문
  • 연산
  • SSL
  • 기본 자료형
  • 논리 연산자
  • 비트 연산자
  • 자료형
  • 산술 연산자
  • 참조 자료형
  • https

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
dackyy

대기의 연대기

[JAVA] 원형 Queue를 사용한 너비우선탐색(BFS) Queue제네릭 사용 X (미로탈출)
코딩테스트

[JAVA] 원형 Queue를 사용한 너비우선탐색(BFS) Queue제네릭 사용 X (미로탈출)

2022. 7. 15. 16:17
반응형

미로는 정해진 값으로 사용했으며 원형 queue를 구현하여 queue 크기를 5로 설정하고 구현했습니다.

package Day3;

import java.util.ArrayList;
import java.util.Arrays;

public class MiroQueue {
    static int[][] map = {{0,0,0,-1,0,0,0,-1}, {0,-1,0,-1,0,-1,0,-1}, {0,-1,0,-1,-1,-1,0,-1}, {0,0,0,0,0,0,0,-1}, {-1,-1,-1,0,-1,-1,-1,-1}, {0,0,-1,0,-1,0,0,-1}, {-1,0,0,0,-1,0,-1,-1}, {-1,-1,-1,0,0,0,0,0}};
    static String[] queue = new String [4];
    static ArrayList<String> log = new ArrayList<String>();
    static int front, rear, num;
    static String start = "0,0";
    static String exit = "7,7";
    static String temp = "";
    public static void main(String[] args) throws OverflowIntQueueException {
        front = 0;
        rear = 0;
        num = 0;
        disp();
        enQueue(start);
        while(!exit.equals(temp)){
            search(deQueue());
            System.out.print("queue :" + Arrays.toString(queue)+"\n");
            System.out.print("현재위치 : "+temp+" ");
        }
    }
    static void search(String str) throws OverflowIntQueueException {
        String [] temp = str.split(",");
        int x = Integer.parseInt(temp[0]);
        int y = Integer.parseInt(temp[1]);
        for(int i = x-1;i<=x+1;i+=2){
            valid(i,y);
        }
        for(int j = y-1;j<=y+1;j+=2){
            valid(x,j);
        }
    }
    static void valid(int x, int y) throws OverflowIntQueueException {
        String tmp = x + "," + y;
        if(x <0 || y <0) { }
        else if(x > 7 || y > 7) { }
        else if (map[x][y] == -1){ }
        else if (Arrays.asList(queue).contains(tmp)){ }
        else{
            if(!log.contains(tmp)) {
                temp = tmp;
                enQueue(tmp);
            }
        }

    }
    static void disp(){
        for (int[] ints : map) {
            for (int anInt : ints) {
                if (anInt == -1)
                    System.out.print(" ■");
                else {
                    System.out.print(" □");
                }
            }
            System.out.println();
        }
    }
    static void enQueue(String n) throws OverflowIntQueueException{
        if (num >= queue.length)
            throw new OverflowIntQueueException();
        queue[rear++] = n;
        num++;
        if (rear == queue.length-1)
            rear = 0;
    }
    static String deQueue() throws OverflowIntQueueException{
        if (num <= 0)
            throw new OverflowIntQueueException();
        String n = queue[front];
        queue[front++] = null;
        num--;
        if(front == queue.length-1)
            front = 0;
        log.add(n);
        return n;
    }
}
package Day3;

public class OverflowIntQueueException extends Exception {
    public OverflowIntQueueException() {}
}
반응형

'코딩테스트' 카테고리의 다른 글

[JAVA] 단순 선택정렬  (0) 2022.07.18
[JAVA] Stack를 사용한 깊이우선 탐색 (DFS) Stack제네릭 사용 X  (0) 2022.07.15
[JAVA] 후위연산자를 이용한 + - 연산 (제네릭 Stack 사용)  (0) 2022.07.14
[JAVA] 후위연산자를 이용한 + - 연산 ( 제네릭 X)  (0) 2022.07.14
[JAVA] 팩토리얼 재귀함수  (0) 2022.07.13
    '코딩테스트' 카테고리의 다른 글
    • [JAVA] 단순 선택정렬
    • [JAVA] Stack를 사용한 깊이우선 탐색 (DFS) Stack제네릭 사용 X
    • [JAVA] 후위연산자를 이용한 + - 연산 (제네릭 Stack 사용)
    • [JAVA] 후위연산자를 이용한 + - 연산 ( 제네릭 X)
    dackyy
    dackyy

    티스토리툴바