PS/BaekJoon

15649๋ฒˆ: N๊ณผ M (1) ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด www.acmicpc.net ๐Ÿ€ ๋ฌธ์ œ ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด ๐Ÿ€ ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 8) ๐Ÿ€ ์ถœ๋ ฅ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๐Ÿ€ ์˜ˆ์ œ..
๐ŸŒฑ ๋ฌธ์ œ 1260๋ฒˆ: DFS์™€ BFS ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ www.acmicpc.net ๐ŸŒฑ ํ•ด๊ฒฐ๊ณผ์ • 1. DFS ์™€ BFS์˜ ๊ฐœ๋…์„ ๊ณต๋ถ€ํ•œ ์ดํ›„ 2. ๊ทธ์— ๋งž๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ๐ŸŒฑ DFS (๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) ์žฌ๊ท€๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— BFS ๋ณด๋‹ค ๋Š๋ฆฐ ๊ฒƒ์ด ํŠน์ง• BFS์™€ ๋‹ฌ๋ฆฌ ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌ ์‹œ๊ฐ„ ๋ณต์žก๋„ LinkedList : O(N + E), N: ์ •์ ์ˆ˜, E: ๊ฐ„์„  ์ˆ˜ ํ–‰๋ ฌ: O(N^2) ๐ŸŒฑ BFS (๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰) ์ตœ๋‹จ ๊ฒฝ๋กœ, ์ž„์˜์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์žฌ๊ท€ X, Ite..
๋ฌธ์ œ 6588๋ฒˆ: ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ, n = a + b ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ, a์™€ b๋Š” ํ™€์ˆ˜ ์†Œ์ˆ˜์ด๋‹ค. ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž๋Š” ๊ณต๋ฐฑ ํ•˜๋‚˜๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ ์žˆ๋‹ค. ๋งŒ์•ฝ, n์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด, b-a๊ฐ€ ๊ฐ€์žฅ ํฐ www.acmicpc.net import math import sys # ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐฏ์ˆ˜ maxValue = 1000000 lists = [True] * (maxValue + 1) lists[0] = lists[1] = False # ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด for i in range(2, int(math.sqrt(maxValue) + 1)): if lists[i]: # 1. ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋‚˜์„œ ์ถ”๊ฐ€ํ•œ ๋‚ด์šฉ => ํ•œ ๋ฒˆ ํ™•์ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์‹œ ํ™•์ธํ•˜์ง€ ์•Š์•„๋„๋จ ์•„๋‹ˆ๋ฉด ๊ฐ™..
1244๋ฒˆ: ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ ์ฒซ์งธ ์ค„์—๋Š” ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๋Š” 100 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์Šค์œ„์น˜์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ผœ์ ธ ์žˆ์œผ๋ฉด 1, ๊บผ์ ธ์žˆ์œผ๋ฉด 0์ด๋ผ๊ณ  ํ‘œ์‹œํ•˜๊ณ  ์‚ฌ์ด์— ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ www.acmicpc.net ๋ฌธ์ œ 1๋ถ€ํ„ฐ ์—ฐ์†์ ์œผ๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ์Šค์œ„์น˜๋“ค์ด ์žˆ๋‹ค. ์Šค์œ„์น˜๋Š” ์ผœ์ ธ ์žˆ๊ฑฐ๋‚˜ ๊บผ์ ธ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค. ์— ์Šค์œ„์น˜ 8๊ฐœ์˜ ์ƒํƒœ๊ฐ€ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ‘1’์€ ์Šค์œ„์น˜๊ฐ€ ์ผœ์ ธ ์žˆ์Œ์„, ‘0’์€ ๊บผ์ ธ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•™์ƒ ๋ช‡ ๋ช…์„ ๋ฝ‘์•„์„œ, ํ•™์ƒ๋“ค์—๊ฒŒ 1 ์ด์ƒ์ด๊ณ  ์Šค์œ„์น˜ ๊ฐœ์ˆ˜ ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋‚˜๋ˆ„์–ด์ฃผ์—ˆ๋‹ค. ํ•™์ƒ๋“ค์€ ์ž์‹ ์˜ ์„ฑ๋ณ„๊ณผ ๋ฐ›์€ ์ˆ˜์— ๋”ฐ๋ผ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์Šค์œ„์น˜๋ฅผ ์กฐ์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค. ๋‚จํ•™์ƒ์€ ์Šค์œ„์น˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž๊ธฐ๊ฐ€ ๋ฐ›์€ ์ˆ˜์˜ ๋ฐฐ์ˆ˜์ด๋ฉด, ๊ทธ ์Šค์œ„์น˜์˜ ์ƒ..
1002๋ฒˆ: ํ„ฐ๋ › ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฅ˜์žฌ๋ช…์ด ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฌดํ•œ๋Œ€์ผ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๊ฐ€์žฅ ๋ฐœ์ƒํ–ˆ๋˜ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•œ ์›์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ์ ‘์ ์ด 2๊ฐœ์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ ๋ฐ”๋กœ ์œ„์— ์ž‘์„ฑํ•˜๋‹ค๋ณด๋‹ˆ ์ ‘์ ์ด ๋ฌดํ•œ๋Œ€์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ ‘์ ์ด 1๊ฐœ, 2๊ฐœ๋กœ ํŒŒ์•…๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค. ๋”ฐ๋ผ์„œ ์ ‘์ ์ด ๋ฌดํ•œ๋Œ€์˜ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ์ฒ˜์Œ ํ™•์ธํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์กฐ๊ฑด๋ฌธ ๋งจ ์•ž ๋ถ€๋ถ„์— ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokeni..
import java.util.Scanner; public class Main { static int a; static int[] array; static boolean[] check; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); a = scanner.nextInt(); scanner.close(); array = new int[a]; check = new boolean[a]; dsf(0); } private static void dsf(int count) { if(count == a) { for(int i = 0; i
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { // attribute static String[] array; static boolean[] check; static ArrayList ans; public static void main(String[] args) { try { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(bf.readLine()); // ๋ถ€๋“ฑํ˜ธ ๊ฐœ์ˆ˜ arr..
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; // ์ˆœ์—ด์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ํ›„์— ์ ˆ๋Œ€ ๊ฐ’์œผ๋กœ ๋”ํ•ด์ค€๋‹ค. public class Main { static int[] array; static int[] array2; static boolean[] check; static int max; static int index; public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamRead..
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(true) { boolean answer = true; String a = scanner.next(); if(a.equals("0")) break; for(int i = 0; i< a.length()/2; i++) { if(a.charAt(i)!=a.charAt(a.length()-i-1)) { answer = false; } } if(answer) System.out.println("yes"); else System.out.println("no"); } scanne..
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Stack stack = new Stack(); StringBuilder sb = new StringBuilder(); try { int N = Integer.parseInt(br.readLine())..
chaerlo127
'PS/BaekJoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)