회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
어떻게 시간을 줄여야 할까요
9663번 - N-Queen
rkdtjddbs0347
2년 전
0
재귀함수를 이용해서 최대한 구현해봤는데 혹시 이 구문 내에서 수정을 통해 시간을 줄일 방법이 있을까요?
#include <iostream> using namespace std; int **chess; int sum = 0; bool check(int n, int col, int row){ for(int i = 0; i < n; i++){ if(chess[i][row]) return false; if(chess[col][i]) return false; } int col_s, row_s; if(col < row){ col_s = col - col; row_s = row - col; } else{ col_s = col - row; row_s = row - row; } for(int i = col_s, j = row_s; i < n and j < n; i++, j++){ if(chess[i][j]) return false; } if(col < n - 1 - row){ col_s = col - col; row_s = row + col; } else{ col_s = col - (n - 1 - row); row_s = row + (n - 1 - row); } for(int i = col_s, j = row_s; i < n and j >= 0; i++, j--){ if(chess[i][j]) return false; } return true; } void n_queen(int n, int col, int row, int cnt){ if(cnt == n){ sum++; return; } for(int i = col; i < n; i++){ for(int j = row; j < n; j++){ if(!chess[i][j]){ if(check(n, i, j)){ chess[i][j] = 1; n_queen(n, i, j, cnt+1); chess[i][j] = 0; } } } row = 0; } return; } int main(){ int n; cin >> n; chess = new int*[n]; for(int i = 0; i < n; i++) chess[i] = new int[n]{0, }; n_queen(n, 0, 0, 0); cout << sum << endl; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
rkdtjddbs0347 2년 전
재귀함수를 이용해서 최대한 구현해봤는데 혹시 이 구문 내에서 수정을 통해 시간을 줄일 방법이 있을까요?