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

1991번: 트리 순회

첫째 줄에는 이진 트리의 노드의 개수 N(1 ≤ N ≤ 26)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 노드와 그의 왼쪽 자식 노드, 오른쪽 자식 노드가 주어진다. 노드의 이름은 A부터 차례대로 알파

www.acmicpc.net




문제 풀이(코드)
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void printPath(ArrayList<Integer> path) {
        for (int i = 0; i < path.size(); i++) {
            char cur = (char)(path.get(i) + 'A');
            System.out.print(cur);
        }
    }

    public static void preorder(int[][] tree, int row, ArrayList<Integer> path) {
        // 현재 노드 방문 처리
        path.add(tree[row][0]);
        // 왼쪽 노드로 이동
        if (tree[row][1] != -1) preorder(tree, tree[row][1], path);
        // 오른쪽 노드로 이동
        if (tree[row][2] != -1) preorder(tree, tree[row][2], path);
    }

    public static void inorder(int[][] tree, int row, ArrayList<Integer> path) {
        // 왼쪽 노드로 이동
        if (tree[row][1] != -1) inorder(tree, tree[row][1], path);
        // 현재 노드 방문 처리
        path.add(tree[row][0]);
        // 오른쪽 노드로 이동
        if (tree[row][2] != -1) inorder(tree, tree[row][2], path);
    }

    public static void postorder(int[][] tree, int row, ArrayList<Integer> path) {
        // 왼쪽 노드로 이동
        if (tree[row][1] != -1) postorder(tree, tree[row][1], path);
        // 오른쪽 노드로 이동
        if (tree[row][2] != -1) postorder(tree, tree[row][2], path);
        // 현재 노드 방문 처리
        path.add(tree[row][0]);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] tree = new int[n][3];

        for (int i = 0; i < n; i++) {
            int row = (int)(sc.next().charAt(0) - 'A');
            tree[row][0] = row;

            int left, right;
            for (int j = 1; j <= 2; j++) {
                char cur = sc.next().charAt(0);
                if (cur == '.') tree[row][j] = -1;
                else tree[row][j] = (int)(cur - 'A');
            }
        }

        ArrayList<Integer> preorderPath = new ArrayList<Integer>();
        preorder(tree, 0, preorderPath);

        ArrayList<Integer> inorderPath = new ArrayList<Integer>();
        inorder(tree, 0, inorderPath);

        ArrayList<Integer> postorderPath = new ArrayList<Integer>();
        postorder(tree, 0, postorderPath);

        printPath(preorderPath);
        System.out.println();
        printPath(inorderPath);
        System.out.println();
        printPath(postorderPath);
        System.out.println();
        
        sc.close();
    }
}

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

 

1956번: 운동

첫째 줄에 V와 E가 빈칸을 사이에 두고 주어진다. (2 ≤ V ≤ 400, 0 ≤ E ≤ V(V-1)) 다음 E개의 줄에는 각각 세 개의 정수 a, b, c가 주어진다. a번 마을에서 b번 마을로 가는 거리가 c인 도로가 있다는 의

www.acmicpc.net

 

 

문제 풀이(코드)
import java.util.*;

public class Main {
	static int[][] graph;
	static int v, e, min = Integer.MAX_VALUE;
	static final int INF = (int)1e9;
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		v = scan.nextInt();
		e= scan.nextInt();
		
		graph = new int[v+1][v+1];
		
		for(int i = 0; i <= v; i++)
			Arrays.fill(graph[i],INF);
		
		for(int i = 0; i < e; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			int c = scan.nextInt();
			
			graph[a][b] = Math.min(graph[a][b], c);
		}
		
		for(int k = 1; k <= v; k++)
			for(int i = 1; i <= v; i++)
				for(int j = 1; j <= v; j++)
					graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
		
		for(int i = 1; i <= v; i++)
			min = Math.min(min, graph[i][i]);
		
		if(min == INF)
			System.out.println(-1);
		else
			System.out.println(min);
		
		scan.close();
	}

}

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,

www.acmicpc.net

 

 

문제 조건

 

문제 풀이 (코드)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    private static int N;
    private static int M;
    private static int H;
    private static int box[][][];
    private static boolean visited[][][];
    private static int[] dx = {1, 0, -1, 0, 0, 0};
    private static int[] dy = {0, 1, 0, -1, 0, 0};
    private static int[] dz = {0, 0, 0, 0, 1, -1};
    private static int count = 0;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        M = scan.nextInt();
        N = scan.nextInt();
        H = scan.nextInt();

        box = new int[H + 1][N + 1][M + 1];
        visited = new boolean[H + 1][N + 1][M + 1];

        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= N; j++) {
                for (int k = 1; k <= M; k++) {
                    box[i][j][k] = scanner.nextInt();
                }
            }
        }

        bfs();

        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= N; j++) {
                for (int k = 1; k <= M; k++) {
                    if (box[i][j][k] == 0) {
                        System.out.println(-1);
                        return;
                    }
                }
            }
        }

        System.out.println(count);
        
        scan.close();
    }

    private static void bfs() {
        Queue<Point> queue = new LinkedList<>();

        for (int i = 1; i <= H; i++) {
            for (int j = 1; j <= N; j++) {
                for (int k = 1; k <= M; k++) {
                    if (box[i][j][k] == 1) {
                        queue.add(new Point(j, k, i, 0));
                        visited[i][j][k] = true;
                    }
                }
            }
        }

        while (!queue.isEmpty()) {
            Point point = queue.poll();

            count = Math.max(count, point.depth);

            for (int i = 0; i < 6; i++) {
                int x = point.x + dx[i];
                int y = point.y + dy[i];
                int z = point.z + dz[i];

                if (isInArea(x, y, z)) {
                    if (box[z][x][y] == 0 && !visited[z][x][y]) {
                        queue.add(new Point(x, y, z, point.depth + 1));
                        box[z][x][y] = 1;
                        visited[z][x][y] = true;
                    }
                }
            }
        }
    }

    private static boolean isInArea(int x, int y, int z) {
        return x > 0 && x <= N && y > 0 && y <= M && z > 0 && z <= H;
    }

    static class Point {
        private int x;
        private int y;
        private int z;
        private int depth;

        public Point(int x, int y, int z, int depth) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.depth = depth;
        }
    }
}

 

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

 

2606번: 바이러스

첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어

www.acmicpc.net

오늘 풀어볼 문제는 2606번 바이러스 문제이다.

2004년 한국정보올림피라드 지역본선 초등부 3번 문제라는데,

초등학생이 풀 수 있으니까 나도 풀 수 있다!

 

문제 조건

 

문제 풀이 (코드)
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	static ArrayList<Integer>[] network;
	static boolean[] visited;
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int n = scan.nextInt(); 
		network = new ArrayList[n + 1];
		visited = new boolean[n + 1];
		for(int i = 0; i < n + 1; i++) {
			network[i] = new ArrayList<>();
		}
		
		int l = scan.nextInt(); 
		for(int i= 0; i < l; i++){
			int u = scan.nextInt();
			int v = scan.nextInt();
			network[u].add(v);
			network[v].add(u);
		}
		
		DFS(1);

		int rslt = 0;
		for(boolean flag : visited){
			if(flag == true) {
				rslt++;
			}	
		}
		System.out.println(rslt - 1);
		
		scan.close();
	}
	
	private static void DFS(int u) {
		visited[u] = true;
		
		for(int v : network[u]){
			if(visited[v] == false) {
				DFS(v);
			}	
		}
	}

}

'알고리즘' 카테고리의 다른 글

[백준 BOJ][JAVA] 1956번: 운동  (0) 2022.02.05
[백준 BOJ][JAVA] 7569번: 토마토  (0) 2022.02.04
[백준 BOJ][JAVA] 10886번: 덱  (0) 2022.01.29
[백준 BOJ][JAVA] 2164번: 카드2  (0) 2022.01.28
[백준 BOJ][JAVA] 9012번: 괄호  (0) 2022.01.22

+ Recent posts