Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이승헌 / 10월 4주차 / 월 목 #291

Merged
merged 6 commits into from
Oct 26, 2023
Merged

Conversation

olrlobt
Copy link
Contributor

@olrlobt olrlobt commented Oct 23, 2023


🎈boj boj12919 - A와 B 2


🗨 해결방법 :


문자열을 하나하나 뒤집게 되면 시간이 오래 걸릴 것이라고 생각하여,
최대한 숫자만으로 해결하려고 노력했습니다.

결과적으로는 오히려 더 느린 코드가 된 것 같아요 ..

startIndex == 1 이면, 거꾸로 본다는 의미이고
endIndex == 0 이면 원래 방향으로 본다는 의미입니다

📝메모 :


✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class boj12919 {

    static String S;
    static String SR;
    static String T;
    static int result;
    static int SLen;

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

        S = br.readLine();
        SR = new StringBuilder(S).reverse().toString();
        SLen = S.length();
        T = br.readLine();
        solve(0, T.length(), 0, 1);
        System.out.println(result > 0 ? 1 : 0);
    }

    private static void solve(int start, int end, int startIdx, int endIdx) {
        // dir == 0 앞 // dir == length 뒤
        if (result > 0 || SLen == end - start) {
            if (T.substring(start, end).equals(startIdx == 1 ? SR : S)) {
                result++;
                return;
            }
            return;
        }

        if (T.substring(start,end).charAt((end - start - 1) * endIdx) == 'A') {
            solve(start + startIdx, end - endIdx, startIdx, endIdx);
        }
        if (T.substring(start,end).charAt((end - start - 1) * startIdx) == 'B') {
            solve(start + endIdx, end - startIdx, endIdx, startIdx);
        }
    }


}


🎈boj 10159- 저울


🗨 해결방법 :


DFS로 자식들과 부모를 탐색해 주었습니다.

📝메모 :


✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static boolean[][] map;
    static boolean[] visited;

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

        int N = Integer.parseInt(br.readLine());
        int M = Integer.parseInt(br.readLine());

        map = new boolean[N + 1][N + 1];
        visited = new boolean[N + 1];
        for (int relation = 0; relation < M; relation++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            map[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = true;
        }

        for (int num = 1; num <= N; num++) {
            Arrays.fill(visited,false);
            sb.append(N - (topDown(num) + bottomUp(num)) + 1).append("\n");
        }
        System.out.println(sb);
    }

    private static int bottomUp(int num) {
        int sum = 1;

        for (int row = 1; row < map.length ; row++) {
            if(map[row][num] && !visited[row]){
                visited[row] = true;
                sum += bottomUp(row);
            }
        }

        return sum;

    }

    private static int topDown(int num) {
        int sum = 1;

        for (int column = 1; column < map.length ; column++) {
            if(map[num][column] && !visited[column]){
                visited[column] = true;
                sum += topDown(column);
            }
        }

        return sum;
    }


}



🎈boj 2467 - 용액


🗨 해결방법 :


투포인터

📝메모 :


✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

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

        int N = Integer.parseInt(br.readLine());
        int[] map = new int[N];

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int num = 0; num < N; num++) {
            map[num] = Integer.parseInt(st.nextToken());
        }
        int[] solve = solve(map);

        sb.append(map[solve[0]]).append(" ").append(map[solve[1]]);
        System.out.println(sb);
    }

    private static int[] solve(int[] map) {

        Arrays.sort(map);
        int sum = Integer.MAX_VALUE;
        int left = 0;
        int right = map.length - 1;
        int temp = 0;
        int[] result = new int[2];

        while (left < right) {

            temp = map[left] + map[right];

            if (Math.abs(temp) < sum) {
                sum = Math.abs(temp);
                result[0] = left;
                result[1] = right;
            }

            if (temp < 0) {
                left++;
            } else {
                right--;
            }
        }
        return result;
    }
}


🎈boj 16120 - PPAP


🗨 해결방법 :


PPAP 문자열의 규칙을 찾아 해결하였습니다.

📝메모 :


✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static final String IS_PPAP = "PPAP";
    static final String NOT_PPAP = "NP";

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

        System.out.println(solve(N) ? IS_PPAP : NOT_PPAP);
    }

    private static boolean solve(String N) {
        int len = N.length();
        int countA = 0;
        int countP = 0;
        int totalA = len / 3;

        if (len % 3 != 1) {
            return false;
        }
        if (len == 1) {
            return N.equals("P");
        }

        for (int idx = 0; idx < len - 1; idx++) {

            if (N.charAt(idx) == 'A') {
                countA++;
                if (countA * 2 > countP || N.charAt(idx + 1) == 'A') {
                    return false;
                }
            } else {
                countP++;
            }
        }

        return totalA == countA;
    }

}


🎈boj 14466 - 소가 길을 건넌 이유6


🗨 해결방법 :


bfs로 탐색을 해주고,
만난 소의 수를 조합으로 구하여
전체 경우에서 빼 주었습니다.

📝메모 :


road 를 처리하는 과정에서 4차원 배열을 사용하다보니 생각보다 많은 메모리를 사용하게 되었습니다.

✔코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static int[][] map;
    static boolean[][][][] road;
    static final int[] dx = {1, 0, -1, 0};
    static final int[] dy = {0, -1, 0, 1};


    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()) + 1;
        int K = Integer.parseInt(st.nextToken());
        int R = Integer.parseInt(st.nextToken());

        map = new int[N][N];
        road = new boolean[N][N][N][N];

        for (int row = 0; row < R; row++) {
            st = new StringTokenizer(br.readLine());
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            int r2 = Integer.parseInt(st.nextToken());
            int c2 = Integer.parseInt(st.nextToken());

            road[r][c][r2][c2] = true;
            road[r2][c2][r][c] = true;
        }

        for (int cow = 0; cow < K; cow++) {
            st = new StringTokenizer(br.readLine());
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            map[r][c] = -1;
        }

        int result = K * (K - 1); // 전체 경우

        for (int row = 1; row < map.length; row++) {
            for (int column = 1; column < map.length; column++) {
                if (map[row][column] != -1) {
                    continue;
                }
                result -= solve(row, column);
            }
        }
        System.out.println(result / 2);
    }

    private static int solve(int row, int column) {
        int count = 0;
        Queue<Node> queue = new ArrayDeque<>();
        queue.offer(new Node(row, column));

        while (!queue.isEmpty()) {

            Node curNode = queue.poll();

            for (int dir = 0; dir < 4; dir++) {
                int nextRow = curNode.row + dy[dir];
                int nextColumn = curNode.column + dx[dir];

                if (nextRow < 1 || nextColumn < 1 || nextRow >= map.length || nextColumn >= map.length
                        || map[nextRow][nextColumn] == 1 || road[curNode.row][curNode.column][nextRow][nextColumn]) {
                    continue;
                }
                if (map[nextRow][nextColumn] == -1) {
                    count++;
                }
                map[nextRow][nextColumn] = 1;
                queue.offer(new Node(nextRow, nextColumn));
            }
        }

        return count * (count - 1);
    }

    private static class Node {
        int row;
        int column;

        public Node(int row, int column) {
            this.row = row;
            this.column = column;
        }
    }
}

@olrlobt olrlobt changed the title 이승헌 / 10월 4주차 / 월 이승헌 / 10월 4주차 / 월 목 Oct 26, 2023
@olrlobt olrlobt requested a review from skagmltn7 October 26, 2023 11:04
@olrlobt olrlobt merged commit cbdb1b0 into SSAFY-10th-Seoul17:main Oct 26, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant