1. Problem
2. My Solution
import sys
sys.setrecursionlimit(100000)
def dfs(x,y):
if 0 <= x < h and 0 <= y < w and land_sea[x][y] == 1:
land_sea[x][y] = 0
for dx,dy in d:
nx = x + dx
ny = y + dy
dfs(nx,ny)
while(True):
w,h = map(int,sys.stdin.readline().rstrip().split())
if w == 0 and h == 0:
break
land_sea = []
count = 0
d = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
for i in range(h):
land_sea.append(list(map(int,sys.stdin.readline().rstrip().split())))
for i in range(h):
for j in range(w):
if land_sea[i][j] == 1:
dfs(i,j)
count += 1
print(count)
import sys
from collections import deque
def bfs(x,y):
queue = deque()
queue.append((x,y))
land_sea[x][y] = 0
while(queue):
x,y = queue.popleft()
for dx,dy in d:
nx = x + dx
ny = y + dy
if 0 <= nx < h and 0 <= ny < w and land_sea[nx][ny] == 1:
queue.append((nx,ny))
land_sea[nx][ny] = 0
while(True):
w,h = map(int,sys.stdin.readline().rstrip().split())
if w == 0 and h == 0:
break
land_sea = []
count = 0
d = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
for i in range(h):
land_sea.append(list(map(int,sys.stdin.readline().rstrip().split())))
for i in range(h):
for j in range(w):
if land_sea[i][j] == 1:
bfs(i,j)
count += 1
print(count)
1. Problem
2. My Solution
import sys
sys.setrecursionlimit(1000000)
def dfs(v):
visited[v] = True # 방문 표시
for u in graph[v]: # 정점 v와 인접한 정점들을 하나씩 살펴보면서
if visited[u] == False: # 만약 방문하지 않았다면 dfs로 방문
dfs(u)
n, m = map(int,sys.stdin.readline().rstrip().split())
graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
count = 0
for i in range(m): # 해당 정점과 인접한 정점들을 저장
u, v = map(int,sys.stdin.readline().rstrip().split())
graph[u].append(v)
graph[v].append(u)
for i in range(1,n+1):
if visited[i] == False:
dfs(i)
count += 1
print(count)
import sys
from collections import deque
def bfs(v):
queue = deque()
queue.append(v)
visited[v] = True
while(queue):
v = queue.popleft()
for u in graph[v]:
if visited[u] == False:
queue.append(u)
visited[u] = True
n, m = map(int,sys.stdin.readline().rstrip().split())
graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
count = 0
for i in range(m): # 해당 정점과 인접한 정점들을 저장
u, v = map(int,sys.stdin.readline().rstrip().split())
graph[u].append(v)
graph[v].append(u)
for i in range(1,n+1):
if visited[i] == False:
bfs(i)
count += 1
print(count)
4. Learned
1. Problem
2. My Solution
import sys
import math
from itertools import combinations
n = int(sys.stdin.readline())
player = set([i for i in range(1,n+1)])
s = []
res = math.inf
for _ in range(n):
s.append(list(map(int,sys.stdin.readline().rstrip().split())))
# 팀 나누기
for i in range(2, (n//2)+1):
possible_team = list(combinations(player,i))
# 중복되는 경우를 피하기 위함
if n % i == 0:
possible_team = possible_team[:len(possible_team)//2]
for start_team in possible_team:
start_s = 0
link_s = 0
link_team = player - set(start_team)
start_player = list(combinations(start_team,2))
link_player = list(combinations(link_team,2))
for p1, p2 in start_player:
start_s += (s[p1-1][p2-1] +s[p2-1][p1-1])
for p1, p2 in link_player:
link_s += (s[p1-1][p2-1] +s[p2-1][p1-1])
diff = abs(start_s - link_s)
res = min(res, diff)
print(res)
1 2 3 4 멤버가 있을 때
1 2 vs 3 4
1 3 vs 2 4
1 4 vs 2 3
2 3 vs 1 4 // 여기서부터 겹침
2 4 vs 3 4
3 4 vs 1 2
import sys
import math
from itertools import combinations
def sum(team):
temp = 0
for i in range(len(team)-1):
for j in range(i + 1, len(team)):
temp += stat[team[i]][team[j]]
return temp
n = int(sys.stdin.readline())
player = [i for i in range(n)]
stat = [[0] * n for _ in range(n)]
S = []
res = math.inf
for _ in range(n):
S.append(list(map(int,sys.stdin.readline().rstrip().split())))
for i in range(n):
for j in range(n):
if not stat[i][j] or not stat[j][i]:
stat[i][j] = stat[j][i] = S[i][j] + S[j][i]
for i in range(2, (n//2)+1):
possible_team = list(combinations(player,i))
if n % i == 0:
possible_team = possible_team[:len(possible_team)//2]
for start in possible_team:
link = list(set(player) - set(start))
diff = abs(sum(start) - sum(link))
res = min(res, diff)
print(res)
4. Learned
1. Problem
2. My Solution
import sys
word = sys.stdin.readline().rstrip().upper()
alphabet_cnt = [0] * 26
for i in word:
alphabet_cnt[ord(i) - 65] += 1
max_cnt = max(alphabet_cnt)
if alphabet_cnt.count(max_cnt) > 1:
print("?")
else:
print(chr(alphabet_cnt.index(max_cnt)+65))
3. Learned