-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from yejincode/week13
feat : 3월 2주차 - 예진
- Loading branch information
Showing
2 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package yejin.song; | ||
|
||
import java.io.*; | ||
|
||
public class BOJ_NQueen { | ||
static int N; | ||
static int count; | ||
static int[][] map; | ||
public static void main(String[] args) throws IOException{ | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
N = Integer.parseInt(br.readLine()); | ||
count = 0; | ||
map = new int[N][N]; | ||
|
||
//dfs | ||
// N*N map 만든 후, map[0][i] 선택했을 때, 위치하면 안되는 곳 map[][] -> ture 같이 만들어서 접근 못하게 하기 | ||
// 재귀한 다음에는 map true-> false로 다시 바꿔주기 | ||
|
||
// 다음 map[1][i] 접근해서 접근가능하면 동일하게 반복 | ||
// 한 줄씩 끝날 때마다, 남은 줄 중 다 x가 있어서 가지 못하는 줄이 있는지 검사 -> 접근 못하면 return | ||
// depth == N 될 때까지 선택완료 했다면, count++; | ||
|
||
dfs(0); | ||
|
||
System.out.println(count); | ||
|
||
} | ||
|
||
static void dfs(int depth){ // depth == start_index | ||
// depth 종료조건 여부 체크 | ||
if(depth == N){ | ||
count ++; | ||
return; | ||
} | ||
|
||
for (int j = 0; j < N; j++){ | ||
if(map[depth][j] == 0){ // 접근 가능하면 | ||
// 자기 포함한 모든 가로, 세로, 대각선 노드 ++; | ||
changeMap(true,depth,j); | ||
|
||
// 재귀 함수 호출 | ||
dfs(depth+1); | ||
|
||
// 다시 자기 포함한 모든 가로, 세로, 대각선 노드 --; | ||
changeMap(false,depth,j); | ||
} | ||
} | ||
|
||
} | ||
|
||
static void changeMap(boolean status, int x , int y){ | ||
if(status){ | ||
// 가로 세로 | ||
for(int i =0 ; i<N; i++){ | ||
map[x][i]++; | ||
map[i][y]++; | ||
} | ||
map[x][y]--; | ||
// 대각선 | ||
|
||
int x_max = Math.max(N-x,x); | ||
int y_max = Math.max(N-y,y); | ||
int search_max = Math.max(x_max, y_max); | ||
|
||
for(int i = 1; i < search_max; i++){ | ||
int plus = i; | ||
int minus = -i; | ||
|
||
if (x + plus >= 0 && x + plus < N && y + plus >= 0 && y + plus < N) map[x+plus][y+plus]++; | ||
if (x + plus >= 0 && x + plus < N && y + minus >= 0 && y + minus < N) map[x+plus][y+minus]++; | ||
if (x + minus >= 0 && x + minus < N && y + plus >= 0 && y + plus < N) map[x+minus][y+plus]++; | ||
if (x + plus >= 0 && x + plus < N && y + minus >= 0 && y + minus < N) map[x+plus][y+minus]++; | ||
} | ||
|
||
}else { | ||
for(int i =0 ; i<N; i++){ | ||
map[x][i]--; | ||
map[i][y]--; | ||
} | ||
map[x][y]++; | ||
// 대각선 | ||
|
||
for(int i = 1; i < N; i++){ | ||
int plus = i; | ||
int minus = -i; | ||
|
||
if (x + plus >= 0 && x + plus < N && y + plus >= 0 && y + plus < N) map[x+plus][y+plus]--; | ||
if (x + plus >= 0 && x + plus < N && y + minus >= 0 && y + minus < N) map[x+plus][y+minus]--; | ||
if (x + minus >= 0 && x + minus < N && y + plus >= 0 && y + plus < N) map[x+minus][y+plus]--; | ||
if (x + plus >= 0 && x + plus < N && y + minus >= 0 && y + minus < N) map[x+plus][y+minus]--; | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package yejin.song; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.StringTokenizer; | ||
|
||
public class BOJ_치킨배달 { | ||
static int N; | ||
static int M; | ||
static ArrayList<int[]> home; | ||
static ArrayList<int[]> chicken; | ||
static ArrayList<int[]> comb; | ||
|
||
static int[][] dist; | ||
|
||
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()); | ||
home = new ArrayList<>(); | ||
chicken = new ArrayList<>(); | ||
comb = new ArrayList<>(); | ||
|
||
|
||
// 1(집)의 배열 따로 담기 | ||
// 2(치킨집)의 배열 따로 담기 | ||
for (int i = 0 ; i<N; i++){ | ||
st = new StringTokenizer(br.readLine(), " "); | ||
for (int j = 0; j< N; j++){ | ||
int value = Integer.parseInt(st.nextToken()); | ||
// 0이면 반복문 탈출하도록 | ||
if (1 == value) { | ||
home.add(new int[]{i,j}); | ||
} | ||
else if (2 == value){ | ||
chicken.add(new int[]{i,j}); | ||
} | ||
} | ||
} | ||
|
||
// 모든 치킨집-집 거리 구하기 | ||
dist = new int[chicken.size()][home.size()]; | ||
for (int i = 0; i< chicken.size(); i++){ | ||
for (int j = 0; j< home.size(); j++){ | ||
|
||
int chic[] = chicken.get(i); | ||
int hom[] = home.get(j); | ||
|
||
int sero = Math.abs(chic[0] - hom[0]); | ||
int garo= Math.abs(chic[1] - hom[1]); | ||
|
||
dist[i][j] = sero + garo; | ||
} | ||
} | ||
|
||
// 치킨집 인덱스 기준으로 M개 뽑기 (조합) | ||
int[] result = new int[M]; | ||
combination(chicken.size(), result,0,0); | ||
|
||
// 각 조합별 최소거리 결과 담을 배열 | ||
int result_dist[] = new int[comb.size()]; | ||
|
||
// comb.size()만큼 돌면서 최소 거리 구하기 | ||
for (int i = 0; i< comb.size(); i++){ | ||
int pick[] = comb.get(i); | ||
int min_dist[] = new int[home.size()]; | ||
Arrays.fill(min_dist, 100); //큰 수로 초기화 | ||
|
||
// 각 집 인덱스 (0,...) 별로 dist 돌아가며 비교 | ||
for (int z = 0 ; z< home.size(); z++){ | ||
for(int j = 0; j< pick.length; j++){ | ||
int index = pick[j]; | ||
int dist_value = dist[index][z]; | ||
min_dist[z] = Math.min(min_dist[z], dist_value); | ||
} | ||
} | ||
result_dist[i] = Arrays.stream(min_dist).sum(); | ||
} | ||
|
||
// 각 조합 별로 구한 최소 거리를 오름차순 정렬 -> 즉, 최종적인 최소 거리 구함. | ||
Arrays.sort(result_dist); | ||
|
||
System.out.println(result_dist[0]); | ||
} | ||
|
||
static void combination(int index_size, int[] result, int start, int depth){ | ||
// 종료 조건 | ||
if (depth == result.length){ | ||
int[] copy = Arrays.copyOf(result, result.length); | ||
comb.add(copy); | ||
return; | ||
} | ||
|
||
for (int i = start; i < index_size ; i++){ // 인덱스만큼 반복하기 | ||
result[depth] = i; | ||
combination(index_size, result, i+1, depth+1); | ||
} | ||
} | ||
} | ||
|