Skip to content

Commit 1483c33

Browse files
committed
Merge branch 'week12'
2 parents cd53fcd + 6a163ef commit 1483c33

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
private static int N;
7+
private static int count = 0;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
N = Integer.parseInt(br.readLine());
12+
dfs(0, 11);
13+
System.out.println("-1");
14+
}
15+
16+
public static void dfs(long num, int index) {
17+
if(index == 0) {
18+
if(count == N) {
19+
System.out.println(num);
20+
System.exit(0);
21+
}
22+
count++;
23+
return;
24+
}
25+
long k = (num == 0) ? 10 : num % 10;
26+
for(long i=0; i<k; i++) {
27+
dfs(num*10 + i, index-1);
28+
}
29+
}
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
private static int N, M;
7+
private static boolean[][] friend;
8+
private static int[][] relation;
9+
private static boolean[] visited;
10+
private static int answer = 0;
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
N = Integer.parseInt(st.nextToken());
16+
M = Integer.parseInt(st.nextToken());
17+
friend = new boolean[N+1][N+1];
18+
relation = new int[M][2];
19+
visited = new boolean[N+1];
20+
for(int i=0; i<M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int u = Integer.parseInt(st.nextToken());
23+
int v = Integer.parseInt(st.nextToken());
24+
friend[u][v] = true;
25+
friend[v][u] = true;
26+
relation[i][0] = u;
27+
relation[i][1] = v;
28+
}
29+
dfs(0, 0);
30+
System.out.println((answer == N) ? answer : answer + 1);
31+
}
32+
33+
public static void dfs(int start, int count) {
34+
answer = Math.max(answer, count);
35+
36+
for(int i=start; i<M; i++) {
37+
int friend1 = relation[i][0];
38+
int friend2 = relation[i][1];
39+
if(!visited[friend1] && !visited[friend2]) {
40+
visited[friend1] = true;
41+
visited[friend2] = true;
42+
dfs(i, count+2);
43+
visited[friend1] = false;
44+
visited[friend2] = false;
45+
}
46+
}
47+
}
48+
}

week12/BOJ_2661(좋은수열).java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
5+
private static int N;
6+
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
N = Integer.parseInt(br.readLine());
10+
dfs("1");
11+
}
12+
13+
public static void dfs(String strNum) {
14+
if(strNum.length() == N) {
15+
System.out.println(strNum);
16+
System.exit(0);
17+
return;
18+
}
19+
20+
for(int j=1; j<=3; j++) {
21+
String str = strNum + j;
22+
if(check(str)) {
23+
dfs(str);
24+
}
25+
}
26+
}
27+
28+
public static boolean check(String strNum) {
29+
int len = strNum.length();
30+
for(int i=1; i<=len/2; i++) {
31+
String str1 = strNum.substring(len-i, len);
32+
String str2 = strNum.substring(len-i*2, len-i);
33+
if(str1.equals(str2)) {
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
private int[][] visited;
5+
private int oilNum;
6+
private Map<Integer, Integer> oilMap;
7+
private int[][] land;
8+
9+
public int solution(int[][] _land) {
10+
int answer = 0;
11+
land = _land;
12+
visited = new int[land.length][land[0].length];
13+
oilNum = 1;
14+
oilMap = new HashMap<>();
15+
for(int i=0; i<land.length; i++) {
16+
for(int j=0; j<land[0].length; j++) {
17+
if(land[i][j] == 1 && visited[i][j] == 0) {
18+
bfs(i, j);
19+
}
20+
}
21+
}
22+
for(int i=0; i<land[0].length; i++) {
23+
boolean[] check = new boolean[oilNum];
24+
int count = 0;
25+
for(int j=0; j<land.length; j++) {
26+
int num = visited[j][i];
27+
if(num > 0 && !check[num]) {
28+
check[num] = true;
29+
count += oilMap.get(num);
30+
}
31+
}
32+
answer = Math.max(answer, count);
33+
}
34+
return answer;
35+
}
36+
37+
public void bfs(int startH, int startW) {
38+
int[] dh = {0, 1, 0, -1};
39+
int[] dw = {-1, 0, 1, 0};
40+
Queue<int[]> q = new LinkedList<>();
41+
q.offer(new int[]{startH, startW});
42+
visited[startH][startW] = oilNum;
43+
int count = 1;
44+
while(!q.isEmpty()) {
45+
int h = q.peek()[0];
46+
int w = q.peek()[1];
47+
q.poll();
48+
for(int i=0; i<4; i++) {
49+
int nextH = h + dh[i];
50+
int nextW = w + dw[i];
51+
if(nextH >= 0 && nextH < land.length && nextW >= 0 && nextW < land[0].length) {
52+
if(land[nextH][nextW] == 1 && visited[nextH][nextW] == 0) {
53+
visited[nextH][nextW] = oilNum;
54+
q.offer(new int[]{nextH, nextW});
55+
count++;
56+
}
57+
}
58+
}
59+
}
60+
oilMap.put(oilNum, count);
61+
oilNum++;
62+
}
63+
}

0 commit comments

Comments
 (0)