static 변수 수정이 안되네요

조회수 479회

전체 코드구요

package recursion;
public class study09 {
    private static int N = 8;
    static int[] cols = new int[N];

    static boolean queens(int level) {
        System.out.println("queens 호출 / level = " + level);
        for (int i = 0; i < N; i++) {// 열의 이동을위한 포문
            System.out.println("1");
            cols[level] = i;// 퀸 위치 지정
            System.out.println("2");
            if (level != 0) {// 처음 위치는 비교할대상이 없음으로 지정만 하고
                System.out.println("3");
                boolean position = true;
                for (int j = 0; j < level; j++) {
                    System.out.println("4");
                    for (int k = 0; k < cols.length; k++) {
                        System.out.println("cols[" + k + "] = " + cols[k]);
                    }
                    if (cols[level] == cols[j]) {
                        System.out.println("5");
                        position = false;
                        break;
                    }
                    System.out.println(Math.abs(level - j) + ", " + Math.abs(cols[level] - cols[j]));
                    if (Math.abs(level - j) == Math.abs(cols[level] - cols[j])) {
                        System.out.println("6");
                        position = false;
                        break;
                    }

                }
                if (position == false) {
                    continue;
                }
                if (level == N - 1) {
                    System.out.println("7");
                    for (int k = 0; k < cols.length; k++) {
                        System.out.println("8");
                        System.out.println("cols[" + k + "] = " + cols[k]);
                    }
                    return true;
                }
                if (queens(level + 1)) {
                    System.out.println("9");
                    return true;
                }
            } else {// level 0(처음)일때
                if (level == N - 1) {
                    System.out.println("10");
                    return true;
                }
                if (queens(level + 1)) {
                    System.out.println("11");
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println("메인시작");
        for (int i = 0; i < cols.length; i++) {
            study09.cols[i] = -1;
        }
        for (int i = 0; i < cols.length; i++) {
            System.out.println(study09.cols[i]);
        }
        boolean result = queens(0);
    }
}

메인에서 queens(0) 을 실행하기전에

System.out.println("메인시작");
        for (int i = 0; i < cols.length; i++) {
            study09.cols[i] = -1;
        }
        for (int i = 0; i < cols.length; i++) {
            System.out.println(study09.cols[i]);
        }

이 구문이 무시된상태로 실행이 되네요 왜 안되는지 설명 부탁드려요

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)