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 답변
-
해당 구문이 실행 안된다는건 어떻게 판단하셨나요? 제가 돌려 보니 딱히 문제가 없는데 말이죠.
아마 콘솔 버퍼를 초과하는 로그 때문에 시작 부분의 로그가 날아가서 "메인시작"이라는 로그가 안보이니 해당 구문도 실행되지 않는다고 판단한 것 같네요.
댓글 입력