Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 0x0B_0x0C/김우정/N-Queen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ_9663 {
static boolean[] isUsed1 = new boolean[40];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

열, 오른쪽 대각선, 왼쪽 대각선에 퀸이 있는지의 여부를 3개의 배열로 표현하셨군요
저는 row 배열 하나 써서 객 행에 퀸의 위치(열)을 저장하며 같은 열에 있는지 (row[x] == row[i])대각선에 있는지 (Math.abs(row[x] - row[i]) == x - i)를 검사했습니다!

이 차이로 우정님 코드는 isUsed 배열을 통해 O(1), 저는 isAdjacent 함수로 각 행마다 퀸의 위치를 확인하기 때문에 O(n)이라서 우정님 코드가 더 효율적인 것 같네요!

static boolean[] isUsed2 = new boolean[40];
static boolean[] isUsed3 = new boolean[40];

static int count = 0;
static int n;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());

backTracking(0);

System.out.println(count);
}

public static void backTracking(int cur)
{
if(cur==n){
count++;
return;
}

for(int i=0; i<n; i++){
if(isUsed1[i]||isUsed2[i+cur]||isUsed3[cur-i+n-1])
continue;

isUsed1[i] = true;
isUsed2[i+cur] = true;
isUsed3[cur-i+n-1] = true;

backTracking(cur+1);
isUsed1[i] = false;
isUsed2[i+cur] = false;
isUsed3[cur-i+n-1] = false;
}
}
}
45 changes: 45 additions & 0 deletions 0x0B_0x0C/김우정/N과_M_(2).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_15650 {
static int n;
static int m;
static int[] arr;
static boolean[] isUsed;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());

arr = new int[m];
isUsed = new boolean[n+1];

backTracking(0);
}

public static void backTracking(int k){
if(k == m){
for(int i=0; i<m; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
return;
}

for(int i=(k>0?arr[k-1]+1:1); i<=n; i++)
{
if(isUsed[i]) continue;

arr[k]=i;
isUsed[i]=true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 arr.contains(i)isUsed 배열을 대체했는데,
contains() 함수는 O(n), isUsed 배열은 O(1)로 시간복잡도 면에서 제 코드보다 우정님 코드가 더 효율적으로 보이네요!


backTracking(k+1);
isUsed[i] = false;
}
}
}
37 changes: 37 additions & 0 deletions 0x0B_0x0C/김우정/Z.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_1074 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

int result = z(r, c, n);

System.out.println(result);

br.close();
}

public static int z(int r, int c, int n){
if(n==0)
return 0;

int half = 1<<n-1;

if(r<half && c<half)
return z(r, c, n-1);
else if(r<half && c>=half)
return half*half + z(r, c-half, n-1);
else if(r>=half && c<half)
return 2*half*half +z(r-half, c, n-1);
else
return 3*half*half +z(r-half, c-half, n-1);
}
}
60 changes: 60 additions & 0 deletions 0x0B_0x0C/김우정/별_찍기-10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ_2447 {
static char[][] result;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(br.readLine());

result = new char[input][input];

backTracking(0,0, input, false);

printResult(input);
}

public static void backTracking(int x, int y, int n, boolean blank){
if(blank){
for(int i=x; i<x+n; i++){
for(int j=y; j<y+n; j++){
result[i][j]=' ';
}
}
return;
}

if(n==1) {
result[x][y] = '*';
return;
}

int area=0;
for(int i=x; i<x+n; i+=n/3){
for(int j=y; j<y+n; j+=n/3){
area++;
if(area==5){
backTracking(i,j,n/3,true);
}
else {
backTracking(i, j, n / 3, false);
}
}
}
}

public static void printResult(int n){
StringBuilder sb = new StringBuilder();

for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
sb.append(result[i][j]);
}
sb.append("\n");
}

System.out.print(sb.toString());
}
}
32 changes: 32 additions & 0 deletions 0x0B_0x0C/김우정/재귀함수가_뭔가요?.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BOJ_17478 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int n = Integer.parseInt(br.readLine());
System.out.println("어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다.");

recursive(n, "");

br.close();
}

public static void recursive(int n, String str){
System.out.println(str+"\"재귀함수가 뭔가요?\"");
if(n==0) {
System.out.println(str+"\"재귀함수는 자기 자신을 호출하는 함수라네\"");
}
else {
System.out.println(str + "\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.");
System.out.println(str + "마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.");
System.out.println(str + "그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어.\"");

recursive(n - 1, str + "____");
}

System.out.println(str+"라고 답변하였지.");
}
}