https://www.acmicpc.net/problem/15686

 

15686번: 치킨 배달

크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸

www.acmicpc.net

M개의 치킨집을 골라 도시의 치킨거리 합을 최소로 해야합니다.

인덱스를 이용하여 집의 좌표와 치킨집의 좌표를 각각 저장해놓습니다.

그 다음 모든 가능한 경우를 구해야하기 때문에 조합으로 고른 치킨집의 인덱스를 pick 배열에 저장하여

M개를 골랐으면 집과 고른 치킨집의 거리를 N^2으로 구하였습니다.

 

고른 인덱스의 다음 인덱스를 재귀로 보내야하는데 계속 실수하는 바람에 시간초과 한번 떴습니다 ㅠㅠ... 왜 매번 이런 실수를 반복하는지..

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int list[][], house[][], chicken[][], pick[][];
    static int N, M, hsize, csize, ans = Integer.MAX_VALUE;
 
    static void func(int idx, int cnt) {
        if (cnt == M) {
            int sum = 0;
            for (int i = 0; i < hsize; i++) {
                int dis = Integer.MAX_VALUE;
                for (int j = 0; j < M; j++) {
                    int x = house[i][0- pick[j][0];
                    int y = house[i][1- pick[j][1];
                    dis = Math.min(dis, Math.abs(x) + Math.abs(y));
                }
                sum += dis;
            }
 
            ans = Math.min(ans, sum);
            return;
        }
 
        for (int i = idx; i < csize; i++) {
            pick[cnt][0= chicken[i][0];
            pick[cnt][1= chicken[i][1];
            func(i + 1, cnt + 1);
            pick[cnt][0= 0;
            pick[cnt][1= 0;
        }
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        list = new int[N][N];
        house = new int[100][2];
        chicken = new int[13][2];
        pick = new int[13][2];
 
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                list[i][j] = Integer.parseInt(st.nextToken());
                if (list[i][j] == 1) {
                    house[hsize][0= i;
                    house[hsize++][1= j;
                } else if (list[i][j] == 2) {
                    chicken[csize][0= i;
                    chicken[csize++][1= j;
                }
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func(00);
        System.out.println(ans);
    }
}
cs

https://www.acmicpc.net/problem/1339

 

1339번: 단어 수학

첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대

www.acmicpc.net

그리디로 해결 가능한 문제입니다.

 

알파벳이 ABC 이렇게 들어오면 앞에 있는 A부터 100의자리를 부여받고, 마지막에 있는 C는 1의자리를 받습니다.

이런 점을 이용하여 int배열을 알파벳 크기만큼 선언하여 뒤에서부터 C에 1을 더하고, B에 10을 더하고 A에 100을 더하는 방식으로 하였습니다.

 

이후에 더한 배열을 정렬하여 큰 수부터 9를 부여하는 식으로 값을 더해 출력하면 됩니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int num[] = new int[26];
    static int ans;
 
    static void func() {
        Arrays.sort(num);
        int n = 9;
        for (int i = 25; num[i] != 0; i--) {
            ans += (num[i] * n);
            n--;
        }
        
        System.out.println(ans);
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
 
        while (N-- > 0) {
            int tmp = 1;
            st = new StringTokenizer(br.readLine());
            char ch[] = st.nextToken().toCharArray();
            for (int i = ch.length - 1; i >= 0; i--) {
                num[ch[i] - 'A'+= tmp;
                tmp *= 10;
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
    }
}
cs

'algorithm > Greedy' 카테고리의 다른 글

boj 8980 택배  (0) 2021.02.16
boj 11000 강의실 배정  (0) 2021.02.16
boj 1931 회의실 배정  (0) 2021.02.16
boj 2839 설탕 배달  (0) 2021.02.16
boj 11399 ATM  (0) 2021.01.29

https://www.acmicpc.net/problem/2580

 

2580번: 스도쿠

스도쿠는 18세기 스위스 수학자가 만든 '라틴 사각형'이랑 퍼즐에서 유래한 것으로 현재 많은 인기를 누리고 있다. 이 게임은 아래 그림과 같이 가로, 세로 각각 9개씩 총 81개의 작은 칸으로 이루

www.acmicpc.net

스도쿠가 주어지고 빈칸을 채우는 문제입니다.

 

일단 스도쿠를 완성하기 위한 조건으로

3*3 구역, 가로, 세로에 모두 다른 숫자가 와야합니다.

 

우선 빈칸이 0으로 되어있기때문에 ArrayList에 빈칸의 좌표를 모두 넣습니다.

그런 다음 이 빈칸들을 가지고 백트래킹을 돌립니다.

 

현재 좌표에서 1~9를 모두 넣어보고 조건에 맞으면 다음 좌표로 넘어가는 식으로 계속 탐색해주시면 되겠습니다.

 

모든 빈칸을 다 채웠을 때 출력해주시면 됩니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static char div[][] = { { 'a''a''a''b''b''b''c''c''c' },
            { 'a''a''a''b''b''b''c''c''c' }, { 'a''a''a''b''b''b''c''c''c' },
            { 'd''d''d''e''e''e''f''f''f' }, { 'd''d''d''e''e''e''f''f''f' },
            { 'd''d''d''e''e''e''f''f''f' }, { 'g''g''g''h''h''h''i''i''i' },
            { 'g''g''g''h''h''h''i''i''i' }, { 'g''g''g''h''h''h''i''i''i' } };
    static boolean cols[][] = new boolean[9][10];
    static boolean rows[][] = new boolean[9][10];
    static boolean visit[][] = new boolean[9][10];
    static int list[][] = new int[9][9];
    static ArrayList<int[]> e = new ArrayList<>();
    static boolean chk;
 
    static void dfs(int idx) {
        if (idx == e.size()) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    System.out.print(list[i][j] + " ");
                }
                System.out.println();
            }
            chk = true;
            return;
        }
 
        int x = e.get(idx)[0];
        int y = e.get(idx)[1];
        for (int i = 1; i <= 9; i++) {
            if (rows[x][i] || cols[y][i] || visit[div[x][y] - 'a'][i])
                continue;
 
            rows[x][i] = true;
            cols[y][i] = true;
            visit[div[x][y] - 'a'][i] = true;
            list[x][y] = i;
            dfs(idx + 1);
            if (chk)
                return;
            list[x][y] = 0;
            rows[x][i] = false;
            cols[y][i] = false;
            visit[div[x][y] - 'a'][i] = false;
        }
    }
 
    static void input() throws Exception {
        for (int i = 0; i < 9; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 9; j++) {
                list[i][j] = Integer.parseInt(st.nextToken());
                if (list[i][j] == 0) {
                    e.add(new int[] { i, j });
                } else {
                    cols[j][list[i][j]] = true;
                    rows[i][list[i][j]] = true;
                    visit[div[i][j] - 'a'][list[i][j]] = true;
                }
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        dfs(0);
    }
}
cs

'algorithm > dfs' 카테고리의 다른 글

boj 15650 N 과 M (2)  (0) 2021.01.26
boj 15649 N 과 M (1)  (0) 2021.01.26
boj 1759 암호 만들기  (0) 2021.01.22
boj 1062 가르침  (0) 2021.01.22
boj 9663 N-Queen  (0) 2021.01.22

https://www.acmicpc.net/problem/1759

 

1759번: 암호 만들기

첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다.

www.acmicpc.net

간단한 백트래킹 문제입니다.

 

출력이 오름차순이므로 입력으로 주어지는 알파벳을 오름차순으로 먼저 정렬해줍니다.

그 다음 재귀를 통해 알파벳을 추가할 때마다 모음인지 자음인지 확인하여 갯수를 갱신시켜주면서 재귀를 돌려줍니다.

가능한 암호조합을 모두 찾아낸 후에 이 조합이 모음 1개, 자음 2개 이상을 포함하고 있는 경우에만 출력합니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static StringBuffer sb = new StringBuffer();
    static char list[] = new char[16];
    static int L, C, a, b;
 
    static void func(int idx, int cnt, String str) {
        if (cnt == L) {
            if (a < 1 || b < 2)
                return;
            sb.append(str).append("\n");
            return;
        }
 
        if (idx >= C)
            return;
 
        for (int i = idx; i < C; i++) {
            if (list[i] == 'a' || list[i] == 'e' || list[i] == 'i' || list[i] == 'o' || list[i] == 'u')
                a++;
            else
                b++;
 
            func(i + 1, cnt + 1, str + list[i]);
 
            if (list[i] == 'a' || list[i] == 'e' || list[i] == 'i' || list[i] == 'o' || list[i] == 'u')
                a--;
            else
                b--;
        }
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        L = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
 
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < C; i++) {
            list[i] = st.nextToken().charAt(0);
        }
        Arrays.sort(list, 0, C);
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func(00"");
        System.out.print(sb.toString());
    }
}
cs

'algorithm > dfs' 카테고리의 다른 글

boj 15650 N 과 M (2)  (0) 2021.01.26
boj 15649 N 과 M (1)  (0) 2021.01.26
boj 2580 스도쿠  (0) 2021.01.22
boj 1062 가르침  (0) 2021.01.22
boj 9663 N-Queen  (0) 2021.01.22

https://www.acmicpc.net/problem/1920

 

1920번: 수 찾기

첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들

www.acmicpc.net

가장 기본적인 이분탐색 알고리즘으로 해결 가능한 문제입니다.

 

주의할 점은 처음에 주어지는 배열의 크기인 N과

수가 배열안에 존재하는지 확인하는 배열의 크기인 M

둘의 크기가 다르다는 것입니다..

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int list[];
    static int ques[];
    static int N, M;
 
    static int binarysearch(int l, int r, int x) {
        if (l > r)
            return 0;
 
        int m = (l + r) / 2;
        if (list[m] == x)
            return 1;
        else {
            if (list[m] > x)
                return binarysearch(l, m - 1, x);
            else
                return binarysearch(m + 1, r, x);
        }
    }
 
    static void func() {
        for (int i = 1; i <= M; i++) {
            System.out.println(binarysearch(1, N, ques[i]));
        }
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        list = new int[N + 1];
        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= N; i++) {
            list[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(list);
 
        st = new StringTokenizer(br.readLine());
        M = Integer.parseInt(st.nextToken());
        ques = new int[M + 1];
        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= M; i++) {
            ques[i] = Integer.parseInt(st.nextToken());
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
    }
}
cs

'algorithm > binarysearch' 카테고리의 다른 글

boj 2110 공유기 설치  (0) 2021.04.13
boj 7453 합이 0인 네 정수  (0) 2021.01.22
boj 2143 두 배열의 합  (0) 2021.01.22
boj 2805 나무 자르기  (0) 2021.01.22
boj 17124 두 개의 배열  (0) 2021.01.22

https://www.acmicpc.net/problem/1039

 

1039번: 교환

첫째 줄에 정수 N과 K가 주어진다. N은 1,000,000보다 작거나 같은 자연수이고, K는 10보다 작거나 같은 자연수이다.

www.acmicpc.net

작년에 풀었을 때는 못풀었었는데 생각보다 간단한 bfs로 해결 가능한 문제였습니다.

 

이 문제는 swap(i번째 숫자, j번째 숫자)를 K번만큼 하였을 때 만들 수 있는 가장 큰 수를 출력하지만 K번의 연산을 할 수 없으면 -1을 출력해야합니다.

 

이 때, 바꾼수가 0으로 시작하면 안되기때문에 이에 대한 예외처리만 해주면 됩니다.

여기서 K번의 연산 후에 0으로 시작하는 수가 아닌 중간에도 0으로 시작하는 수가 나오면 안된다는 점을 주위하여야 합니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static boolean visit[][] = new boolean[1000001][11];
    static int N, K, ans = -1;
 
    static void bfs() {
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[] { N, 0 });
        visit[N][0= true;
 
        while (!q.isEmpty()) {
            int xx = q.peek()[0];
            char x[] = Integer.toString(xx).toCharArray();
            int cnt = q.peek()[1];
            q.remove();
 
            if (cnt == K) {
                ans = Math.max(ans, xx);
                continue;
            }
 
            char newx[];
            for (int i = 0; i < x.length; i++) {
                for (int j = i + 1; j < x.length; j++) {
                    if (i == 0 && x[j] == '0')
                        continue;
 
                    newx = x.clone();
                    char temp = newx[i];
                    newx[i] = newx[j];
                    newx[j] = temp;
 
                    int y = Integer.parseInt(String.valueOf(newx));
                    if (!visit[y][cnt + 1]) {
                        q.add(new int[] { y, cnt + 1 });
                        visit[y][cnt + 1= true;
                    }
                }
            }
        }
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
    }
 
    public static void main(String[] args) throws Exception {
        input();
        bfs();
        System.out.println(ans);
    }
}
cs

'algorithm > bfs' 카테고리의 다른 글

boj 17142 연구소 3  (0) 2021.01.22
boj 2636 치즈  (0) 2021.01.22
boj 3055 탈출  (0) 2021.01.22
boj 2234 성곽  (0) 2021.01.22
boj 17472 다리 만들기 2  (0) 2021.01.22

https://www.acmicpc.net/problem/1103

 

1103번: 게임

줄에 보드의 세로 크기 N과 가로 크기 M이 주어진다. 이 값은 모두 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 보드의 상태가 주어진다. 쓰여 있는 숫자는 1부터 9까지의 자연수 또는

www.acmicpc.net

그냥 dfs돌리다가 시간초과난 적이 있어서 dp를 같이 사용하였습니다.

 

보드에 적혀있는 숫자에 맞춰서 동전을 움직여서 최대 몇 번 움직일 수 있는지 구하는 문제입니다.

중간에 있는 구멍은 무시해도 되고, 도착지점이 구멍인 경우에는 동전이 빠지므로 이동하지 못합니다.

dfs로 모든 경우를 다 돌아보고 횟수를 구하면 됩니다.

예외처리로 무한루프가 있는 경우 -1을 출력해야하는데, dfs를 돌다가 방문처리된 곳에 다시 한 번 가게 되면 무한루프 판정으로 -1을 리턴하게 구현하였습니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static char list[][] = new char[50][50];
    static boolean visit[][] = new boolean[50][50];
    static int dp[][] = new int[50][50];
    static int direct[][] = { { 01 }, { 10 }, { 0-1 }, { -10 } };
    static int N, M, ans;
 
    static int dfs(int x, int y) {
        if (visit[x][y])
            return ans = -1;
        visit[x][y] = true;
        if (dp[x][y] != -1)
            return dp[x][y];
 
        dp[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nx = x + direct[i][0* (list[x][y] - '0');
            int ny = y + direct[i][1* (list[x][y] - '0');
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= M)
                continue;
            if (list[nx][ny] == 'H')
                continue;
 
            dp[x][y] = Math.max(dp[x][y], dfs(nx, ny) + 1);
            if (ans == -1)
                return ans;
            visit[nx][ny] = false;
        }
 
        return dp[x][y];
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
 
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
 
            list[i] = st.nextToken().toCharArray();
        }
 
        for (int i = 0; i < N; i++) {
            Arrays.fill(dp[i], -1);
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        ans = dfs(00);
        if (ans != -1)
            ans++;
        System.out.println(ans);
    }
}
cs

'algorithm > dp' 카테고리의 다른 글

boj 9657 돌 게임 3  (0) 2021.01.27
boj 2096 내려가기  (0) 2021.01.22
boj 12852 1로 만들기 2  (0) 2021.01.22
boj 16195 1, 2, 3 더하기 9  (0) 2021.01.22
boj 15993 1, 2, 3 더하기 8  (0) 2021.01.22

https://www.acmicpc.net/problem/1062

 

1062번: 가르침

첫째 줄에 단어의 개수 N과 K가 주어진다. N은 50보다 작거나 같은 자연수이고, K는 26보다 작거나 같은 자연수 또는 0이다. 둘째 줄부터 N개의 줄에 남극 언어의 단어가 주어진다. 단어는 영어 소문

www.acmicpc.net

모든 경우를 다 확인하여 계산하는 백트래킹 문제입니다.

 

먼저, 단어의 시작은 "anta"로 시작되고, 끝은 "tica"로 끝납니다.

따라서 시작과 끝에 사용되는 알파벳인 'a', 'c', 'i', 'n', 't'는 무조건 가르쳐야하는 알파벳입니다.

M이 5보다 작으면 무조건 모든 단어를 읽을 수 없게 되는것이고,

M이 5보다 크거나 같을 때 백트래킹을 이용하여 해결하면 됩니다.

 

이제 모든 알파벳을 대상으로 백트래킹을 돌립니다.

 

여기서 i를 0부터 돌려버리면 abcdint, adcbint와 같은 중복이 발생하므로 next문자부터 돌려줍니다.

 

가르친 알파벳의 갯수가 M과 같으면 단어를 읽을 수 있지 확인하고, ans을 갱신시켜주고 최종적인 답을 출력합니다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static char list[][] = new char[50][16];
    static boolean alpha[] = new boolean[26];
    static int N, M, ans;
 
    static void init() {
        alpha['a' - 'a'= true;
        alpha['c' - 'a'= true;
        alpha['i' - 'a'= true;
        alpha['n' - 'a'= true;
        alpha['t' - 'a'= true;
    }
 
    static void dfs(int idx, int cnt) {
        if (cnt == M) {
            int readCnt = 0;
            for (int i = 0; i < N; i++) {
                boolean chk = true;
                for (int j = 0; j < list[i].length; j++) {
                    if (!alpha[list[i][j] - 'a']) {
                        chk = false;
                        break;
                    }
                }
 
                if (chk)
                    readCnt++;
            }
 
            ans = Math.max(ans, readCnt);
            return;
        }
 
        for (int i = idx; i < 26; i++) {
            if (alpha[i])
                continue;
 
            alpha[i] = true;
            dfs(i + 1, cnt + 1);
            alpha[i] = false;
        }
    }
 
    static void func() {
        if (M < 5) {
            System.out.println(0);
            return;
        }
 
        init();
        dfs(05);
        System.out.println(ans);
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            list[i] = st.nextToken().toCharArray();
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
    }
}
cs

'algorithm > dfs' 카테고리의 다른 글

boj 15650 N 과 M (2)  (0) 2021.01.26
boj 15649 N 과 M (1)  (0) 2021.01.26
boj 2580 스도쿠  (0) 2021.01.22
boj 1759 암호 만들기  (0) 2021.01.22
boj 9663 N-Queen  (0) 2021.01.22

 

https://www.acmicpc.net/problem/3055

 

3055번: 탈출

사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제

www.acmicpc.net

두 종류의 bfs을 사용하여 해결하였습니다.

하나는 물에 대한 bfs,

하나는 고슴고치에 대한 bfs입니다.

 

[물이 흐르는 조건]

1. 돌을 통과할 수 없다.

2. 비버의 소굴로 이동할 수 없다.

 

[고슴도치가 이동하는 조건]

1. 돌을 통과할 수 없다.

2. 물로 차있는 구역으로 이동할 수 없다.

3. 물이 찰 예정인 칸으로 이동할 수 없다.

 

물이 찰 예정인 칸으로 이동할 수 없다는 말은 물과 고슴도치가 같은 시간에 같은 장소에 갈 수 없다는 것입니다.

 

따라서 물에 대한 bfs로 물이 먼저 흐르게 하고, 그 다음에 고슴도치에 대한 bfs로 고슴도치를 이동시키면 되는 문제입니다.

 

이 과정을 무한반복하여 고슴도치가 비버의 소굴로 이동하면 그 시간을 출력하고,

과정을 반복하던 도중에 고슴도치의 좌표가 들어있는 큐가 비어있으면 탈출에 실패했다는 것이니, "KAKTUS"를 출력합니다.

 

C++ 소스 추가하였습니다.

 

 

[C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <queue>
using namespace std;
 
queue<pair<intint> > q;
queue<pair<intint> > wq;
int direct[4][2= { {0,1},{1,0},{0,-1},{-1,0} };
bool visit[51][51], wvisit[51][51], chk;
char list[51][51];
int N, M, ex, ey;
 
void waterbfs() {
    int qsize = wq.size();
    while (qsize--) {
        int x = wq.front().first;
        int y = wq.front().second;
        wq.pop();
 
        for (int i = 0; i < 4; i++) {
            int nx = x + direct[i][0];
            int ny = y + direct[i][1];
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
            if (wvisit[nx][ny] || list[nx][ny] == 'D' || list[nx][ny] == 'X'continue;
 
            wq.push({ nx,ny });
            wvisit[nx][ny] = true;
            list[nx][ny] = '*';
        }
    }
}
 
void bfs() {
    int qsize = q.size();
    while (qsize--) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        for (int i = 0; i < 4; i++) {
            int nx = x + direct[i][0];
            int ny = y + direct[i][1];
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
            if (visit[nx][ny] || list[nx][ny] == 'X' || list[nx][ny] == '*'continue;
 
            if (list[nx][ny] == 'D') {
                chk = true;
                return;
            }
            q.push({ nx,ny });
            visit[nx][ny] = true;
        }
    }
}
 
void func() {
    for (int T = 1; ; T++) {
        waterbfs();
        bfs();
 
        if (chk) {
            cout << T << '\n';
            return;
        }
        else if (q.empty()) {
            cout << "KAKTUS\n";
            return;
        }
    }
}
 
void input() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> list[i][j];
            if (list[i][j] == 'S') {
                q.push({ i,j });
                visit[i][j] = true;
            }
            else if (list[i][j] == 'D') {
                ex = i;
                ey = j;
            }
            else if (list[i][j] == '*') {
                wq.push({ i,j });
                visit[i][j] = true;
            }
        }
    }
}
 
int main() {
    cin.tie(NULL); cout.tie(NULL);
    ios::sync_with_stdio(false);
 
    input();
    func();
 
    return 0;
}
cs

 

[Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static char list[][] = new char[50][50];
    static boolean qvisit[][] = new boolean[50][50];
    static boolean wvisit[][] = new boolean[50][50];
    static int N, M, ex, ey;
    static Queue<int[]> q = new LinkedList<>();
    static Queue<int[]> w = new LinkedList<>();
    static int direct[][] = { { 01 }, { 10 }, { 0-1 }, { -10 } };
 
    static void waterbfs() {
        int size = w.size();
        while (size-- > 0) {
            int x=w.peek()[0];
            int y=w.peek()[1];
            w.remove();
            
            for(int i=0; i<4; i++) {
                int nx=x+direct[i][0];
                int ny=y+direct[i][1];
                
                if(nx<0 || ny<0 || nx>=|| ny>=M)
                    continue;
                if(wvisit[nx][ny] || list[nx][ny]=='X' || list[nx][ny]=='D')
                    continue;
                
                w.add(new int[] {nx,ny});
                wvisit[nx][ny]=true;
            }
        }
    }
 
    static boolean bfs() {
        int size=q.size();
        while(size-->0) {
            int x=q.peek()[0];
            int y=q.peek()[1];
            q.remove();
            
            for(int i=0; i<4; i++) {
                int nx=x+direct[i][0];
                int ny=y+direct[i][1];
                
                if(nx<0 || ny<0 || nx>=|| ny>=M)
                    continue;
                if(qvisit[nx][ny] || wvisit[nx][ny] || list[nx][ny]=='X')
                    continue;
                if(nx==ex && ny==ey) 
                    return true;
                
                q.add(new int[] {nx,ny});
                qvisit[nx][ny]=true;
            }
        }
        return false;
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
 
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            list[i] = st.nextToken().toCharArray();
            for (int j = 0; j < M; j++) {
                char ch = list[i][j];
                if (ch == 'S') {
                    q.add(new int[] { i, j });
                    qvisit[i][j] = true;
                } else if (ch == '*') {
                    w.add(new int[] { i, j });
                    wvisit[i][j] = true;
                } else if (ch == 'D') {
                    ex = i;
                    ey = j;
                }
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        for(int T=1; ; T++) {
            waterbfs();
            if(bfs()) {
                System.out.println(T);
                break;
            }
            
            if(q.isEmpty()) {
                System.out.println("KAKTUS");
                break;
            }
        }
    }
}
 
cs

'algorithm > bfs' 카테고리의 다른 글

boj 17142 연구소 3  (0) 2021.01.22
boj 2636 치즈  (0) 2021.01.22
boj 1039 교환  (0) 2021.01.22
boj 2234 성곽  (0) 2021.01.22
boj 17472 다리 만들기 2  (0) 2021.01.22

 

https://www.acmicpc.net/problem/3425

 

3425번: 고스택

각각의 입력값에 대해서, 해당하는 프로그램을 수행한 뒤, 출력값을 출력하면 된다. 출력값이란 스택에 저장되어 있는 숫자이다. 만약, 프로그램 에러가 발생하거나, 모든 수행이 종료됐을 때

www.acmicpc.net

[이 문제에서 수행하는 연산]

NUM X : X를 스택의 가장 위에 저장

POP : 스택 가장 위의 숫자를 제거

INV : 첫 번째 수의 부호를 변경

DUP : 첫 번째 수를 스택의 가장위에 한번 더 저장

SWP : 첫 번째 수와 두 번째 수의 위치를 변경

ADD : 첫 번째 수 + 두 번째 수

SUB : 두 번째 수 - 첫 번째 수

MUL : 첫 번째 수 * 두 번째 수

DIV : 두 번째 수 / 첫 번째 수

MOD : 두 번째 수 % 첫 번째 수

 

위의 연산들이 랜덤으로 주어지고, 입력값 V가 주어질 때마다 이 연산들을 수행한 결과를 출력합니다.

 

문제에서 요구하는 조건을 종합하면

1. 숫자가 부족해서 연산수행을 하지 못할 때

2. DIV, MOD 연산 시 0으로 나눴을 때

3. 연산 결과의 절댓값이 109 보다 클 때

4. 모든 연산이 종료되었을 때 스택에 저장되어 있는 숫자가 1개가 아닐 때

라고 할 수 있고, 그럼 출력값의 조건으로는

 

모든 연산이 종료되었을 때 스택에 저장되어 있는 숫자가 1개이고 절댓값이 109보다 작거나 같을 때 입니다.

 

나머지는 연산이 요구하는대로 계산해 주시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br;
    static StringTokenizer st;
    static String type;
    static ArrayList<String[]> list = new ArrayList<>();
    static Stack<long[]> stack = new Stack<>();
    static long N;
    static final long INF = 1000000000;
 
    static boolean func() {
        stack.push(new long[] {N});
        for (String[] arr : list) {
            if ("NUM".equals(arr[0])) {
                stack.push(new long[] {Long.parseLong(arr[1])});
            } else if ("POP".equals(arr[0])) {
                if (stack.isEmpty())
                    return false;
                stack.pop();
            } else if ("INV".equals(arr[0])) {
                if (stack.isEmpty())
                    return false;
                long a = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {-a});
            } else if ("DUP".equals(arr[0])) {
                if (stack.isEmpty())
                    return false;
                stack.push(stack.peek());
            } else if ("SWP".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num1});
                stack.push(new long[] {num2});
            } else if ("ADD".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num1+num2});
            } else if ("SUB".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num2-num1});
            } else if ("MUL".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num1*num2});
            } else if ("DIV".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                if (num1 == 0)
                    return false;
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num2/num1});
            } else if ("MOD".equals(arr[0])) {
                if (stack.size() < 2)
                    return false;
                long num1 = stack.peek()[0];
                stack.pop();
                if (num1 == 0)
                    return false;
                long num2 = stack.peek()[0];
                stack.pop();
                stack.push(new long[] {num2%num1});
            }
        }
        
        return true;
    }
 
    static void numinput() throws Exception {
        boolean chk = false;
        st = new StringTokenizer(br.readLine());
 
        int tc = Integer.parseInt(st.nextToken());
        while (tc-- > 0) {
            st = new StringTokenizer(br.readLine());
 
            N = Long.parseLong(st.nextToken());
            chk = func();
            if (!chk || stack.size() != 1) {
                System.out.println("ERROR");
            } else {
                if(Math.abs(stack.peek()[0])>INF)
                    System.out.println("ERROR");    
                else
                    System.out.println(stack.peek()[0]);
            }
 
            while (!stack.isEmpty())
                stack.pop();
        }
        
        while(!list.isEmpty())
            list.remove(0);
    }
 
    static void typeinput() throws Exception {
        while (true) {
            st = new StringTokenizer(br.readLine());
 
            type = st.nextToken();
            if ("NUM".equals(type)) {
                String num = st.nextToken();
                list.add(new String[] { type, num });
            } else if ("END".equals(type) || "QUIT".equals(type)) {
                break;
            } else {
                list.add(new String[] { type });
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            typeinput();
            if ("QUIT".equals(type))
                break;
            numinput();
            st = new StringTokenizer(br.readLine());
            System.out.println();
        }
    }
}
 
cs

'algorithm > data-structure' 카테고리의 다른 글

boj 5639 이진 검색 트리  (0) 2021.01.24
boj 1991 트리 순회  (0) 2021.01.22
boj 11003 최솟값 찾기  (0) 2021.01.22
boj 1517 버블 소트  (0) 2021.01.22
boj 2517 달리기  (0) 2021.01.22

https://www.acmicpc.net/problem/2234

 

2234번: 성곽

첫째 줄에 두 정수 n, m이 주어진다. 다음 m개의 줄에는 n개의 정수로 벽에 대한 정보가 주어진다. 벽에 대한 정보는 한 정수로 주어지는데, 서쪽에 벽이 있을 때는 1을, 북쪽에 벽이 있을 때는 2를,

www.acmicpc.net

저는 두 번의 bfs를 사용하였습니다.

한번은 각 방의 넘버링을 위한 bfs, 다른 한번은 각 방에 인접한 방을 찾기 위한 bfs입니다.

 

최종으로 구해야 할 것이

1. 이 성에 있는 방의 개수

2. 가장 넓은 방의 넓이

3. 하나의 벽을 제거하여 얻을 수 있는 가장 넓은 방의 크기

이렇게 됩니다.

저는 1, 2번은 각 방의 넘버링으로 해결하였고, 3번은 각 방에 인접한 방을 찾아서 해결하였습니다.

 

 

이 문제에서 입력으로 주어지는 숫자는 벽에 대한 정보입니다.

서쪽에 벽이 있으면 1, 북쪽에 벽이 있으면 2, 동쪽에 벽이 있으면 4, 남쪽에 벽이 있으면 8을 더한 값이 주어집니다.

만약에 벽이 없으면 하나도 더해지지 않은 0이 주어질 것이고, 사방에 모두 벽이 있으면 1 + 2 + 4 + 8 = 15가 주어질 것입니다.

 

1, 2, 4, 8을 더한 값이 주어지기 때문에 먼저 비트마스킹을 떠올렸습니다.

벽을 체크하기 위해 wall 배열에 해당 방향에 대한 값을 넣었고,

해당 방향을 체크할 때 해당하는 값인 wall[i]과 list[x][y]를 비교하여

list[x][y] & wall[i]이 0이면 벽이 없는 것이고, 0이 아니면 벽이 있는 것이라고 보시면 되겠습니다.

 

bfs & 비트마스킹으로 각 방에 넘버링을 하였고, 넘버링을 하면서 방의 크기도 같이 구하였습니다.

그 다음 bfs를 한번 더 돌려서 인접한 방 번호를 sideroom에 넣었고 방의 크기 + 인접한 방의 크기를 비교하여 최댓값을 출력하였습니다.

(sideroom을 set으로 한 이유는 중복체크를 하기 위함입니다.)

 

 

[C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
 
set<int> sideroom[2501];
int N, M, wall[4= { 4812 };
int list[50][50], direct[4][2= { {0,1}, {1,0}, {0,-1}, {-1,0} };
int roomnum, maxroomsize, Room[50][50], Roomsize[2501];
bool visit[50][50];
 
void sidechk(int sx, int sy, int num) {
    queue<pair<intint> > q;
    q.push({ sx, sy });
    visit[sx][sy] = true;
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        for (int i = 0; i < 4; i++) {
            int nx = x + direct[i][0];
            int ny = y + direct[i][1];
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
            if (Room[nx][ny] != num) {
                sideroom[num].insert(Room[nx][ny]);
                continue;
            }
            if (visit[nx][ny]) continue;
 
            q.push({ nx, ny });
            visit[nx][ny] = true;
        }
    }
}
 
void bfs(int sx, int sy, int num) {
    queue<pair<intint> > q;
    q.push({ sx, sy });
    visit[sx][sy] = true;
    for (int t = 1; ; t++) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        Room[x][y] = num;
 
        for (int i = 0; i < 4; i++) {
            if (wall[i] & list[x][y]) continue;
            int nx = x + direct[i][0];
            int ny = y + direct[i][1];
 
            if (visit[nx][ny]) continue;
            q.push({ nx, ny });
            visit[nx][ny] = true;
        }
 
        if (q.empty()) {
            Roomsize[num] = t;
            maxroomsize = max(maxroomsize, t);
            break;
        }
    }
}
 
void solve() {
    int maxroomsum = 0;
    cout << roomnum << '\n' << maxroomsize << '\n';
    for (int i = 1; i <= roomnum; i++) {
        set<int>:: iterator iter;
        for (iter = sideroom[i].begin(); iter != sideroom[i].end(); iter++) {
            int s = *iter;
 
            maxroomsum = max(maxroomsum, Roomsize[i] + Roomsize[s]);
        }
    }
 
    cout << maxroomsum << '\n';
}
 
void func() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (visit[i][j]) continue;
            bfs(i, j, ++roomnum);
        }
    }
    memset(visit, falsesizeof(visit));
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (visit[i][j]) continue;
            sidechk(i, j, Room[i][j]);
        }
    }
}
 
void input() {
    cin >> M >> N;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> list[i][j];
        }
    }
}
 
int main() {
    cin.tie(NULL); cout.tie(NULL);
    ios::sync_with_stdio(false);
 
    input();
    func();
    solve();
 
    return 0;
}
cs

 

 

[Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int list[][] = new int[51][51];
    static int div[][] = new int[51][51];
    static boolean visit[][] = new boolean[51][51];
    static ArrayList<Integer> roomarea = new ArrayList<>();
    static Set<Integer> s[];
    static int wall[] = { 4812 };
    static int direct[][] = { { 01 }, { 10 }, { 0-1 }, { -10 } };
    static int N, M, roomcnt, maxarea, cnt, ans;
 
    static void sidechk(int sx, int sy) {
        Deque<int[]> dq = new ArrayDeque<>();
        dq.add(new int[] { sx, sy });
        visit[sx][sy] = true;
        while (!dq.isEmpty()) {
            int x = dq.peek()[0];
            int y = dq.poll()[1];
 
            for (int i = 0; i < 4; i++) {
                int nx = x + direct[i][0];
                int ny = y + direct[i][1];
 
                if (nx < 0 || ny < 0 || nx >= N || ny >= M)
                    continue;
                if (visit[nx][ny])
                    continue;
 
                if (div[x][y] != div[nx][ny])
                    s[div[x][y]].add(div[nx][ny]);
                else {
                    dq.add(new int[] { nx, ny });
                    visit[nx][ny] = true;
                }
            }
        }
    }
 
    static void solveroom(int sx, int sy) {
        Deque<int[]> dq = new ArrayDeque<>();
        dq.add(new int[] { sx, sy });
        visit[sx][sy] = true;
        while (!dq.isEmpty()) {
            int x = dq.peek()[0];
            int y = dq.poll()[1];
            div[x][y] = roomcnt;
            cnt++;
 
            for (int i = 0; i < 4; i++) {
                int nx = x + direct[i][0];
                int ny = y + direct[i][1];
 
                if (nx < 0 || ny < 0 || nx >= N || ny >= M)
                    continue;
                if (visit[nx][ny] || (wall[i] & list[x][y]) > 0)
                    continue;
 
                dq.add(new int[] { nx, ny });
                visit[nx][ny] = true;
            }
        }
    }
 
    static void func() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (visit[i][j])
                    continue;
                solveroom(i, j);
                roomarea.add(cnt);
                roomcnt++;
                maxarea = Math.max(maxarea, cnt);
                cnt = 0;
            }
        }
 
        s = new HashSet[roomcnt];
        for (int i = 0; i < roomcnt; i++)
            s[i] = new HashSet<>();
 
        for (int i = 0; i < N; i++)
            Arrays.fill(visit[i], false);
 
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (visit[i][j])
                    continue;
 
                sidechk(i, j);
            }
        }
    }
 
    static void solve() {
        for (int i = 0; i < roomcnt; i++) {
            Iterator<Integer> iter = s[i].iterator();
            while (iter.hasNext()) {
                ans = Math.max(ans, roomarea.get(i) + roomarea.get(iter.next()));
            }
        }
 
        System.out.println(roomcnt + "\n" + maxarea + "\n" + ans);
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());
 
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                list[i][j] = Integer.parseInt(st.nextToken());
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
        solve();
    }
}
cs

'algorithm > bfs' 카테고리의 다른 글

boj 17142 연구소 3  (0) 2021.01.22
boj 2636 치즈  (0) 2021.01.22
boj 1039 교환  (0) 2021.01.22
boj 3055 탈출  (0) 2021.01.22
boj 17472 다리 만들기 2  (0) 2021.01.22

 

https://www.acmicpc.net/problem/17472

 

17472번: 다리 만들기 2

첫째 줄에 지도의 세로 크기 N과 가로 크기 M이 주어진다. 둘째 줄부터 N개의 줄에 지도의 정보가 주어진다. 각 줄은 M개의 수로 이루어져 있으며, 수는 0 또는 1이다. 0은 바다, 1은 땅을 의미한다.

www.acmicpc.net

BFS + MST 로 해결하는 문제입니다.

 

먼저 섬의 번호를 구분할 수 있게 표시하고,

섬의 한 지점에서 한방향으로 탐색을 하여 길이가 2이상인 다리를 놓을수 있는지 확인합니다.

 

놓을 수 있으면 시작 섬 번호, 도착 섬 번호, 길이를 벡터에 push_back을 하고,

길이가 2 미만이거나 놓을 수 없으면 빠져나와줍니다.

(여기서 chk 배열을 사용한건

섬 1에서 섬 2까지 연결하는데 같은길이의 다리를 2개 이상 놓을 수 있는 경우와

섬 1에서 섬 2까지 연결하는 것과 섬 2에서 섬 1까지 연결하는 것이 중복되기 때문에 사용하였습니다.)

 

이제 시작 섬 번호, 도착 섬 번호, 길이 정보가 담겨있는 벡터를 길이가 작은 순서대로 sort를 한 후에

크루스칼 알고리즘에서 작은 간선을 선택하는 방식처럼 작은 길이를 선택해주면서 parent 배열을 갱신합니다.

(parent 배열에는 해당 번호 섬의 root가 되는 섬의 번호가 저장되어 있으며, 어떤 섬과 연결되어 있는지 알 수 있습니다.)

 

ans가 0이면 -1을 출력하고 끝내면 되고, 아니면 답을 출력하면 되는데

중요한 것은 모든섬을 연결해야 한다는 것입니다. (그거 때문에 왜틀렸는지 오랫동안 고민해서 ㅠㅠ)

섬이 최대 6개이므로 선형으로 root가 같은지 체크를 한 후에 정답을 출력합니다.

 
1 6
1 0 0 1 0 1
 
//ans : -1
cs

 

문제풀이 하면서 찾은 반례 던지고 갑니다.

 

 

[C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
 
typedef struct edge {
    int s;
    int e;
    int d;
}edge;
 
vector<edge> v;
int parent[11], N, M, ans, areadiv;
int list[10][10], direct[4][2= { {0,1}, {1,0}, {0,-1}, {-1,0} };
bool visit[10][10], chk[10][10][10];
 
bool cmp(edge a, edge b) {
    return a.d < b.d;
}
 
int find_parent(int u) {
    if (parent[u] == u) return u;
 
    return parent[u] = find_parent(parent[u]);
}
 
void union_find(int a, int b) {
    int aroot = find_parent(a);
    int broot = find_parent(b);
 
    parent[max(aroot, broot)] = min(aroot, broot);
}
 
void func() {
    for (int i = 0; i < v.size(); i++) {
        int s = v[i].s;
        int e = v[i].e;
        
        int sroot = find_parent(s);
        int eroot = find_parent(e);
 
        if (sroot == eroot) continue;
 
        union_find(sroot, eroot);
        ans += v[i].d;
    }
 
    if (!ans) {
        cout << "-1\n";
        return;
    }
 
    int root = find_parent(1);
    for (int i = 2; i <= areadiv; i++) {
        int next = find_parent(i);
        if (root != next) {
            ans = -1;
            break;
        }
    }
    cout << ans << '\n';
}
 
void dist(int x, int y, int di, int sn) {
    int xx = x;
    int yy = y;
    int cnt = 0;
 
    while (1) {
        int nx = xx + direct[di][0];
        int ny = yy + direct[di][1];
        
        if (nx < 0 || ny < 0 || nx >= N || ny >= M) break;
        if (list[nx][ny] == sn) break;
        if (!list[nx][ny]) {
            cnt++;
            xx = nx;
            yy = ny;
        }
        else if (list[nx][ny] != sn) {
            if (cnt < 2break;
            if (chk[sn][list[nx][ny]][cnt] || chk[list[nx][ny]][sn][cnt]) break;
            chk[sn][list[nx][ny]][cnt] = true;
            chk[list[nx][ny]][sn][cnt] = true;
            v.push_back({ sn, list[nx][ny], cnt });
            break;
        }
    }
 
    return;
}
 
void find_dist() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (list[i][j]) {
                for (int k = 0; k < 4; k++) {
                    dist(i, j, k, list[i][j]);
                }
            }
        }
    }
 
    sort(v.begin(), v.end(), cmp);
}
 
void bfs(int sx, int sy, int num) {
    queue<pair<intint> > q;
    q.push({ sx,sy });
    visit[sx][sy] = true;
 
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        list[x][y] = num;
 
        for (int i = 0; i < 4; i++) {
            int nx = x + direct[i][0];
            int ny = y + direct[i][1];
 
            if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
            if (!list[nx][ny] || visit[nx][ny]) continue;
 
            q.push({ nx,ny });
            visit[nx][ny] = true;
        }
    }
}
 
void input() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> list[i][j];
         }
    }
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (visit[i][j] || !list[i][j]) continue;
 
            bfs(i, j, ++areadiv);
        }
    }
    for (int i = 1; i <= areadiv; i++) {
        parent[i] = i;
    }
}
 
int main() {
    cin.tie(NULL); cout.tie(NULL);
    ios::sync_with_stdio(false);
 
    input();
    find_dist();
    func();
 
    return 0;
}
cs

 

 

[Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.StringTokenizer;
 
public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static ArrayList<int[]> edge = new ArrayList<>();
    static int list[][] = new int[11][11];
    static boolean visit[][] = new boolean[11][11];
    static boolean chk[][][] = new boolean[7][7][11];
    static int direct[][] = { { 01 }, { 10 }, { 0-1 }, { -10 } };
    static int parent[] = new int[7];
    static int N, M, cnt, div, ans;
 
    static int find(int v) {
        if (parent[v] == v)
            return v;
        return parent[v] = find(parent[v]);
    }
 
    static void Union(int u, int v, int w) {
        int a = find(u);
        int b = find(v);
 
        if (a == b)
            return;
 
        parent[b] = a;
        cnt++;
        ans += w;
    }
 
    static void solve() {
        Collections.sort(edge, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2- o2[2];
            }
        });
 
        for (int i = 0; i < edge.size(); i++) {
            int x = edge.get(i)[0];
            int y = edge.get(i)[1];
            int dis = edge.get(i)[2];
 
            Union(x, y, dis);
        }
 
        if (cnt != div - 1)
            System.out.println(-1);
        else
            System.out.println(ans);
    }
 
    static void getDist(int sx, int sy, int d, int num) {
        int x = sx;
        int y = sy;
        int cnt = 0;
        while (true) {
            x += direct[d][0];
            y += direct[d][1];
            cnt++;
 
            if (x < 1 || y < 1 || x > N || y > M)
                break;
            if (list[x][y] == num)
                break;
            if (list[x][y] == 0)
                continue;
            if (list[x][y] != num) {
                if (cnt <= 2)
                    break;
                if (chk[num][list[x][y]][cnt])
                    break;
                chk[num][list[x][y]][cnt] = true;
                chk[list[x][y]][num][cnt] = true;
                edge.add(new int[] { num, list[x][y], cnt - 1 });
                return;
            }
        }
    }
 
    static void dist() {
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                if (list[i][j] != 0) {
                    for (int k = 0; k < 4; k++) {
                        getDist(i, j, k, list[i][j]);
                    }
                }
            }
        }
    }
 
    static void init() {
        for (int i = 1; i <= div; i++) {
            parent[i] = i;
        }
    }
 
    static void bfs(int sx, int sy) {
        Deque<int[]> dq = new ArrayDeque<>();
        dq.add(new int[] { sx, sy });
        visit[sx][sy] = true;
        while (!dq.isEmpty()) {
            int x = dq.peek()[0];
            int y = dq.poll()[1];
 
            list[x][y] = div;
 
            for (int i = 0; i < 4; i++) {
                int nx = x + direct[i][0];
                int ny = y + direct[i][1];
 
                if (nx < 1 || ny < 1 || nx > N || ny > M)
                    continue;
                if (visit[nx][ny] || list[nx][ny] == 0)
                    continue;
 
                dq.add(new int[] { nx, ny });
                visit[nx][ny] = true;
            }
        }
    }
 
    static void func() {
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                if (visit[i][j] || list[i][j] == 0)
                    continue;
 
                div++;
                bfs(i, j);
            }
        }
 
        init();
    }
 
    static void input() throws Exception {
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= M; j++) {
                list[i][j] = Integer.parseInt(st.nextToken());
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        input();
        func();
        dist();
        solve();
    }
}
cs

'algorithm > bfs' 카테고리의 다른 글

boj 17142 연구소 3  (0) 2021.01.22
boj 2636 치즈  (0) 2021.01.22
boj 1039 교환  (0) 2021.01.22
boj 3055 탈출  (0) 2021.01.22
boj 2234 성곽  (0) 2021.01.22

+ Recent posts