반응형

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

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는

www.acmicpc.net

package com.ji.beakjoon.dfs;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 안전 영역 
 * @author ji
 *
 */
public class SafeZoneDfs {

	static int[][] safeZoneMap;//안전구역 정보
	static int safeZoneMapSize = 0; //안전구역 맵 크기
	static boolean[][] DFSisVisited;
	static List<Integer> list;//각 수위 경계값에 따른 결과 값 저장
	static int[] dx = {1, -1, 0, 0}; //상하좌우위아래
	static int[] dy = {0, 0, 1, -1}; 
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		safeZoneMapSize = Integer.valueOf(br.readLine()); // 행렬 가로 길이
		safeZoneMap = new int[safeZoneMapSize][safeZoneMapSize];
		DFSisVisited = new boolean[safeZoneMapSize][safeZoneMapSize];
		
		int max = 0;
		for (int i = 0; i < safeZoneMapSize; i++) {
			String[] complexGraphInfo = String.valueOf(br.readLine()).split(" ");
			for (int j = 0; j < safeZoneMapSize; j++) {
				safeZoneMap[i][j] = Integer.valueOf(complexGraphInfo[j]);
				if(max < safeZoneMap[i][j])
					max = safeZoneMap[i][j];
			}
		}
		
		list = new ArrayList<>();
		
		for(int depth=0; depth<=max ; depth++) {
			int cnt = 0; 
			for (int i = 0; i < safeZoneMapSize; i++) {
				for (int j = 0; j < safeZoneMapSize; j++) {
					if(safeZoneMap[i][j] > depth && !DFSisVisited[i][j]) {
						cnt++;
						dfs(i, j, depth);
					}
					
				}
			}
			
			//dfs 방문정보 초기화!!!!
			for(boolean a[]:DFSisVisited)
				Arrays.fill(a, false);
			list.add(cnt);
		}
		
		int maxResult = Collections.max(list);
		bw.write(String.valueOf(maxResult) );

		br.close();
		bw.flush();
		bw.close();

	}

	public static void dfs(int x, int y, int depth) {
		DFSisVisited[x][y] = true;
		
		for(int i=0; i < 4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			
			if(nx >=0 && ny >=0 && nx < safeZoneMap.length && ny< safeZoneMap.length) {
				if(safeZoneMap[nx][ny] > depth && !DFSisVisited[nx][ny])
					dfs(nx, ny, depth);
			}
			
		}

	}

}
728x90

'[개발관련] > 코테준비' 카테고리의 다른 글

[백준] 구간합구하기5  (0) 2021.07.11
[백준] 동전1  (0) 2021.07.11
[백준] 알파벳 찾기  (0) 2021.06.20
[백준] 트리의 부모찾기  (0) 2021.06.20
[백준] 트리순회  (0) 2021.06.20
반응형

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

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net

제 풀이도 나름 풀었다고 생각했는데 

다른 스터디원은 indexOf 메서드를 활용하여 매우 간편하게 풀었더군요. 

제 답이 틀린건 아니지만 해당 메서드를 생각했더라면 더 간단히 풀었겠다는 생각을 했습니다.

풀이 과정에는 정답이 없으니 ... 하지만 indexof를 생각 못한 저에게 현타가 잠깐왔다가 회복한 상태입니다. ㅎㅎㅎ

package com.ji.beakjoon;

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

public class Alphabet {

	/**
	 * 알파벳 찾기
	 * 
	 * @param args
	 * @throws IOException
	 */
	static int lowerCaseRangeSize = 122 - 97;
	static int lowerCaseStartIndex = 97;
	static int lowerCaseEndIndex = 122;

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		String input = br.readLine();
		
		int[] resultArr = new int[lowerCaseRangeSize+1];
		for (int j = lowerCaseStartIndex; j <= lowerCaseEndIndex; j++) {

			int alphabetCnt = 0;
			for (int i = 0; i < input.length(); i++) {

				if (input.charAt(i) == j) {
					alphabetCnt++;

					//연속 문자열 첫번째 인덱스만 저장
					if (alphabetCnt == 1)
						resultArr[j - lowerCaseStartIndex] = i;
				}

			}
			
			if (alphabetCnt == 0)
				resultArr[j - lowerCaseStartIndex] = -1;
			
		}

		for (int index : resultArr)
			bw.write(String.valueOf(index) + " ");

		br.close();
		bw.flush();
		bw.close();

	}

}

 

 

728x90

'[개발관련] > 코테준비' 카테고리의 다른 글

[백준] 동전1  (0) 2021.07.11
[백준] 안전영역  (0) 2021.07.11
[백준] 트리의 부모찾기  (0) 2021.06.20
[백준] 트리순회  (0) 2021.06.20
[백준] 구슬탈출 4 for JAVA  (0) 2021.06.20
반응형

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

 

11725번: 트리의 부모 찾기

루트 없는 트리가 주어진다. 이때, 트리의 루트를 1이라고 정했을 때, 각 노드의 부모를 구하는 프로그램을 작성하시오.

www.acmicpc.net

package com.ji.beakjoon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class SearchParentsTree {

	static int nodeNum, edgeNum;
//	static int[][] graph;
	static ArrayList<Integer> ad[];
	static boolean[] DFSisVisited;
	static int result[];

	public static void main(String[] args) throws IOException {

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		nodeNum = Integer.valueOf(br.readLine());
		DFSisVisited = new boolean[nodeNum + 1];
//		graph = new int[nodeNum + 1][nodeNum + 1];
		result = new int[nodeNum + 1];
		ad = new ArrayList[nodeNum +1];
		
		for(int i=1; i<=nodeNum; i++)
			ad[i] = new ArrayList<>();
		
		for (int i = 0; i < nodeNum - 1; i++) {
			String[] edgeNumbers = br.readLine().split(" ");
			int a = Integer.valueOf(edgeNumbers[0]);
			int b = Integer.valueOf(edgeNumbers[1]);
			ad[a].add(b);
			ad[b].add(a);
			
		}

		//메모리 초과!!!
//		for (int i = 0; i < nodeNum - 1; i++) {
//			String[] edgeNumbers = br.readLine().split(" ");
//			int a = Integer.valueOf(edgeNumbers[0]);
//			int b = Integer.valueOf(edgeNumbers[1]);
//			graph[a][b] = b;
//			graph[b][a] = a;
//		}

		dfs(1);
		for (int i = 2; i <= nodeNum; i++)
			bw.write(result[i] + "\n");

		bw.flush();
		bw.close();
	}
	
	static void dfs(int node) {
		// 방문한 노드 번호에 대한 boolean 처리
		DFSisVisited[node] = true;
		for(int index : ad[node]) {
			if(!DFSisVisited[index]) {
				result[index] = node;
				dfs(index);
			}
		}
	}

}
728x90

'[개발관련] > 코테준비' 카테고리의 다른 글

[백준] 안전영역  (0) 2021.07.11
[백준] 알파벳 찾기  (0) 2021.06.20
[백준] 트리순회  (0) 2021.06.20
[백준] 구슬탈출 4 for JAVA  (0) 2021.06.20
[백준] 문자열 폭발  (0) 2021.06.13

+ Recent posts