반응형
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 OrganicCabbage {

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

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

		int testCase = Integer.valueOf(br.readLine());

		for (int t = 0; t < testCase; t++) {

			String[] plantCabbageInfo = br.readLine().split(" ");
			int fieldCntHorizantal = Integer.valueOf(plantCabbageInfo[0]); // 행렬 가로 길이
			int fieldCntVertical = Integer.valueOf(plantCabbageInfo[1]);// 행렬 세로 길이
			int plantCabbageCnt = Integer.valueOf(plantCabbageInfo[2]);//행렬에 1이 있는 위치
			int[][] graph = new int[fieldCntHorizantal + 1][fieldCntVertical + 1];

			for (int i = 0; i < plantCabbageCnt; i++) {
				String[] comPairNum = String.valueOf(br.readLine()).split(" ");
				int a = Integer.parseInt(comPairNum[0]);
				int b = Integer.parseInt(comPairNum[1]);
				graph[a][b] = 1;
			}

			int count = 0;
			for (int j = 0; j < fieldCntVertical ; j++) {
				for (int i = 0; i < fieldCntHorizantal ; i++) {
					if (graph[i][j] == 1) {
						//행렬을 돌면서 처음 1이 나온 경우 count
						count++;
						
						//점화식
						//1로 시작하면 동서남북 검사 후 동서남북에 걸리면 -1로 변환
						//1주변에 1이 없을 경우 재귀 탈출
						dfs(graph, i, j);
					}
				}
			}
			
			bw.write(String.valueOf(count)+"\n");
		}

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

	}

	static void dfs(int[][] graph, int i, int j) {
		int m = graph.length; //가로 행렬 크기
		int n = graph[0].length; // 세로 행렬 크기
		
		//행렬에서 0이거나 마지막에 있는 값일 경우 재귀 탈출
		if (i < 0 || i >= m || j < 0 || j >= n || graph[i][j] != 1)
			return;
		
		//처음 1이 나오고 근처에 있는 1일 경우 -1로 바꿈
		graph[i][j] = -1;

		dfs(graph, i - 1, j);// 위
		dfs(graph, i + 1, j);// 아래
		dfs(graph, i, j - 1);// 왼쪽
		dfs(graph, i, j + 1);// 오른쪽
	}

}
728x90

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

[백준] 토마토  (0) 2021.06.13
[백준] 주식투자  (0) 2021.06.13
[백준] 숨바꼭질  (0) 2021.06.13
[백준] ATM  (0) 2021.06.13
[백준] 쇠막대기  (0) 2021.06.13
반응형
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.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class HideSeek {

	static int N;
	static int K;
	static int[] check = new int[100001];

	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[] inputNumbers = br.readLine().split(" ");
		N = Integer.valueOf(inputNumbers[0]);
		K = Integer.valueOf(inputNumbers[1]);
		 int[] Min = new int[100005];
		 //배열의 값을 모두 -1로 초기화
		 //미방문
	     Arrays.fill(Min, -1); 

		bw.write(String.valueOf(bfs(N, K, Min)));

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

	}

	  public static int bfs(int N, int K, int[] Min) {
	        int nextN = N;
	        int[] status = new int[3];
	        
	        Queue<Integer> q = new LinkedList<Integer>();
	        q.add(nextN);
	        //N과 K가 같을 경우 0을 리턴
	        Min[nextN] = 0;
	 
	        //다음과 같이 처리하면 N과 K가 같을 경우도 함께 처리됨
	        while (!q.isEmpty() && nextN != K) {
	 
	            nextN = q.poll();
	            //다음에 이동할 좌표들
	            status[0] = nextN - 1;     //뒤로 한칸 4
	            status[1] = nextN + 1;    //앞으로 한칸 6
	            status[2] = nextN * 2;    //순간이동 10
	 
	            for (int i = 0; i < 3; i++) {
	                //배열을 벗어나지 않았는지 확인
	                if (status[i] >= 0 && status[i] <= 100000) {
	                	
	                    //이전에 방문했는지 확인
	                    if (Min[status[i]] == -1) {
	                        //처음 간 곳이라면 큐에 넣고 상태를 전 위치값 +1 을 해준다.
	                    	//선입선출이므로 해당 위치에 먼저 도착한놈이 무조건 빠르거나 같다.
	                        q.add(status[i]);
	                        //이동한 위치의 이전 위치의 값을 저장
	                        Min[status[i]] = Min[nextN] + 1;
	 
	                    }
	                }
	            }
	        }
	        //각 위치에 도달하는 최소값 저장
	        for(int mins: Min)
	        System.out.print(mins+" ");
	        
	        return Min[K];
	    }

}
728x90

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

[백준] 주식투자  (0) 2021.06.13
[백준] 유기농 배추  (0) 2021.06.13
[백준] ATM  (0) 2021.06.13
[백준] 쇠막대기  (0) 2021.06.13
[백준] 잃어버린 괄호  (0) 2021.06.13
반응형
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;
import java.util.Comparator;
import java.util.List;

public class ATM {

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

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

		int personCount = Integer.valueOf(br.readLine());
		String[] delayTime = br.readLine().split(" ");

		List<Person> personList = new ArrayList<Person>();
		
		for (int i = 0; i < personCount; i++) 
			personList.add(new Person(i, Integer.valueOf(delayTime[i])));

		//사람 별 지연시간 오름차수 정렬
		personList.sort(new Comparator<Person>() {
			@Override
			public int compare(Person arg0, Person arg1) {
				int age0 = arg0.getDealyTime();
				int age1 = arg1.getDealyTime();
				if (age0 == age1)
					return 0;
				else if (age0 > age1)
					return 1;
				else
					return -1;
			}
		});

		//중첩 시간 합
		int result = 0;
		int sum = 0;
		for (Person per : personList) {
			sum += per.getDealyTime();
			result += sum;
		}
		
		bw.write(String.valueOf(result));

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

}

class Person {
	Integer index;
	Integer dealyTime;

	public Person(Integer index, Integer dealyTime) {
		super();
		this.index = index;
		this.dealyTime = dealyTime;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public Integer getDealyTime() {
		return dealyTime;
	}

	public void setDealyTime(Integer dealyTime) {
		this.dealyTime = dealyTime;
	}
}
728x90

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

[백준] 유기농 배추  (0) 2021.06.13
[백준] 숨바꼭질  (0) 2021.06.13
[백준] 쇠막대기  (0) 2021.06.13
[백준] 잃어버린 괄호  (0) 2021.06.13
[백준] 1로 만들기  (0) 2021.06.13

+ Recent posts