반응형

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Location2589{
	
	int x;
	int y;
	int tc;
	
	public Location2589(int x, int y, int tc) {
		this.x = x;
		this.y = y;
		this.tc =tc;
	}
}

public class BaekjoonAlgo_2589 {

	static int n;
	static int m;
	static char[][] map;
	static int[][] visit;
	static int time;
	static Queue<Location2589> loc;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		n= sc.nextInt();
		m= sc.nextInt();
		map = new char[n][m];
		loc = new LinkedList<>();
		
		int maxTime = 0;
		
		for(int i =0;i<n;i++) {
			String str = sc.next();
			for(int j=0;j<m;j++) {
				map[i][j] = str.charAt(j);
			}
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				if(map[i][j] == 'L') {
					visit = new int[n][m];
					loc.offer(new Location2589(i, j, 0));
					visit[i][j] = 1;
					int val = GetBFS();
					if(val > maxTime) {
						maxTime = val;
					}
					
					//System.out.println("(" + i + "," + j + ") : " + time);
				}
			}
		}
		
		System.out.println(maxTime);
	}
	
	public static int GetBFS() {
		int[] xx = {-1, 0, 1, 0};
		int[] yy = {0, 1, 0, -1};
		int tc = 0;
		
		while(!loc.isEmpty()) {
			for(int i=0;i<loc.size();i++) {
				int x = loc.peek().x;
				int y = loc.peek().y;
				tc = loc.poll().tc;
				
				for(int j=0;j<4;j++) {
					int ax = x + xx[j];
					int ay = y + yy[j];
					
					if(ax>=0 && ay>=0 && ax<n && ay<m && map[ax][ay] == 'L' && visit[ax][ay] == 0) {
						loc.offer(new Location2589(ax, ay, tc+1));
						visit[ax][ay] = 1;
					}
				}
			}
		}
		
		return tc;
	}

}
반응형

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

[Baekjoon] #14502 연구소  (0) 2022.04.07
[Baekjoon] #7568 덩치  (0) 2022.03.15
[Baekjoon] #1978 소수 찾기  (0) 2022.03.12
[Baekjoon] #2571 수 정렬하기 2  (0) 2022.03.12
[Baekjoon] #1476 날짜 계산  (0) 2022.03.11
반응형

 

[ ✔ ] 소수 구하기

 - 소수 구하기 문제는 1을 제외한 2부터 나머지 나눔을 하면서 구할 수 있다.

 

import java.util.Scanner;

public class BaekjoonAlgo_1978 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int count= 0;
		
		for(int i=0;i<n;i++) {
			boolean isPrime = true;
			int num = sc.nextInt();
			
			if(num >1) {
				for(int j=2;j<num;j++) {
					if (num%j ==0) {
						isPrime = false;
						break;
					} 
				}			
				
				if(isPrime) count++; 				
			}
		}
		
		System.out.println(count);
	}

}
반응형

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

[Baekjoon] #7568 덩치  (0) 2022.03.15
[Baekjoon] #2589 보물섬  (0) 2022.03.13
[Baekjoon] #2571 수 정렬하기 2  (0) 2022.03.12
[Baekjoon] #1476 날짜 계산  (0) 2022.03.11
[Baekjoon] #2941 크로아티아 알파벳  (0) 2022.03.10
반응형

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class BaekjoonAlgo_2751 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		ArrayList<Integer> ar = new ArrayList<Integer>();
		StringBuilder sb = new StringBuilder();
		
		for(int i=0;i<n;i++) {
			ar.add(sc.nextInt());
		}
		
		Collections.sort(ar);
		
		for(int i=0;i<n;i++) {
			sb.append(ar.get(i)).append("\n");
		}
		
		System.out.println(sb);
	}

}
반응형

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

[Baekjoon] #2589 보물섬  (0) 2022.03.13
[Baekjoon] #1978 소수 찾기  (0) 2022.03.12
[Baekjoon] #1476 날짜 계산  (0) 2022.03.11
[Baekjoon] #2941 크로아티아 알파벳  (0) 2022.03.10
[Baekjoon] #1316 그룹 단어 체커  (0) 2022.03.08
반응형

import java.util.Scanner;

public class BaekjoonAlgo_1476 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		
		int year = 1;
		int E = sc.nextInt();
		int S = sc.nextInt();
		int M = sc.nextInt();
		
		while(true) {
			
			int tempE = year%15 == 0 ? 15 : year%15;
			int tempS = year%28 == 0 ? 28 : year%28;
			int tempM = year%19 == 0 ? 19 : year%19;
			
			if(tempE == E && tempS == S && tempM == M) {
				System.out.println(year);
				return;
			}
			year++;
		}
	}
}
반응형

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

[Baekjoon] #1978 소수 찾기  (0) 2022.03.12
[Baekjoon] #2571 수 정렬하기 2  (0) 2022.03.12
[Baekjoon] #2941 크로아티아 알파벳  (0) 2022.03.10
[Baekjoon] #1316 그룹 단어 체커  (0) 2022.03.08
[Baekjoon] #5622 다이얼  (0) 2022.03.08
반응형

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaekjoonAlgo_2941 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		String str = sc.next();
		int count = 0;
		
		String[] croAlpha = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
		
		for(int i=0;i<croAlpha.length;i++) {	
			
			Pattern p = Pattern.compile(croAlpha[i]);
			Matcher m = p.matcher(str);
			
			for (int j = 0; m.find(j); j = m.end()) {
			    count++;
			    str = str.replaceAll(croAlpha[i], " ");
			}
		}
		
		str = str.replaceAll(" ", "");
		char[] arr = str.toCharArray();
		
		for(int i=0;i<arr.length;i++) {
			count++;
		}
		
		System.out.println(count);
	}
}
반응형

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

[Baekjoon] #2571 수 정렬하기 2  (0) 2022.03.12
[Baekjoon] #1476 날짜 계산  (0) 2022.03.11
[Baekjoon] #1316 그룹 단어 체커  (0) 2022.03.08
[Baekjoon] #5622 다이얼  (0) 2022.03.08
[Baekjoon] #1152 단어의 개수  (0) 2022.03.07

+ Recent posts