본문 바로가기

CodingTest

(238)
[ BOJ / 파이썬 ] 15657 N과M(9) (*다시 체크 요망, 덜 이해한듯*) | 제출 2 | # 백트래킹 사용한 풀이 import sys input = sys.stdin.readline N, M = map(int, input().split()) nums = sorted(list(map(int, input().split()))) keep = [] visit = [0]*N def recur(x): if x == M: print(*keep) return overlap = 0 # 이 부분 직접 해결한 것 아님. 다시 확인해보기 for i in range(N): if not visit[i] and overlap != nums[i]: visit[i] = 1 keep.append(nums[i]) overlap = nums[i] recur(x + 1) visit[i] = 0 keep.pop()..
[ BOJ / 파이썬 ] 15657 N과M(8) | 제출 1 | # 중복조합 메서드 사용 import sys input = sys.stdin.readline from itertools import combinations_with_replacement N, M = map(int, input().split()) nums = sorted(list(map(int, input().split()))) for item in combinations_with_replacement(nums, M): print(*item) : 중복을 허용해서 ( 본인 포함 ) 순서를 고려해서 뽑는 경우의 수 : 중복조합 메서드 사용한 풀이 | 제출 2 | : 백트래킹 구현 실패 .. 🤕 # 백트래킹 import sys input = sys.stdin.readline from iterto..
[ BOJ / 파이썬 ] 15656 N과M(7) | 제출 1 | # 백트래킹 import sys input = sys.stdin.readline N, M = map(int, input().split()) nums = sorted(list(map(int, input().split()))) keep = [] def recur(x): if x == M: print(*keep) return for i in range(len(nums)): keep.append(nums[i]) recur(x+1) keep.pop() recur(0) : 백트래킹 이용한 풀이 : nums의 모든 값마다 nums의 처음부터 끝까지 짝 지어줘야하기에 for문의 인덱스 범위를 전체로 설정했다. : 중복을 허용하며 본인과도 겹쳐도 되기에 중복 여부를 확인하는 과정이 없다. | 제출 2 |..
[ BOJ / 파이썬 ] 15655 N과 M (6) | 제출 1 | # 백트래킹 import sys input = sys.stdin.readline #from itertools import permutations N, M = map(int, input().split()) nums = sorted(list(map(int, input().split()))) keep = [] def recur(x): if len(keep) == M: print(" ".join(list(map(str, keep)))) return for i in range(x, len(nums)): if nums[i] not in keep: keep.append(nums[i]) recur(i+1) keep.pop() recur(0) : 으아 또 이틀 쉬고 보니까 한 번에 구조가 안 읽혔다. 차..
[ BOJ / 파이썬 ] 15654 N과M (5) | 제출 1 | import sys input = sys.stdin.readline n,m = map(int, input().split()) nums = list(map(int, input().split())) nums.sort() group = [] def find(): if len(group) == m: print(' '.join(map(str,group))) return for i in range(n): if not nums[i] in group: group.append(nums[i]) find() group.pop() find() 앞선 문제들과 비교했을 때, 1부터 n까지 넣어주는 것에서 숫자배열을 차례차례 넣어주는 것으로 바뀐 것이어서 그렇게 어렵지는 않았다. 다만, 이 문제부터 봤으면 까다로웠..
[ BOJ / 파이썬 ] 15652 N과 M(4) | 제출 1 | # 백트래킹으로 구현 import sys input = sys.stdin.readline n,m = map(int, input().split()) group = [] result = [] def find(): if len(group) == m: if not sorted(group) in result: result.append(sorted(group)) return for i in range(1, n+1): group.append(i) find() group.pop() find() for r in result: print(' '.join(list(map(str, r)))) : 역시 사람은 겸손해야한다는 것을 느끼게 된 문제였다. : 이전 문제랑 답이 같은데? 뭐 고칠것 있나 해서 그냥 냈는..
[ BOJ / 파이썬 ] 15651 N과 M(3) | 제출 1 | # 백트래킹으로 구현 import sys input = sys.stdin.readline n,m = map(int, input().split()) group = [] def find(): if len(group) == m: print(' '.join(list(map(str, group)))) return for i in range(1, n+1): group.append(i) find() group.pop() find() : 우선 백트래킹 구현이다. : N과M 시리즈로 푸는 중인데, 절대 안 잊겠다 싶다. 너무 좋다 이런 반복 훈련 : 이번에는 중복 허용해서 출력하는 것이다. 아래 쪽에 백트래킹 구현 부분에서 새로운 값인 i를 넣을 때, 기존 그룹에 동일값이 있는지 체크하는 부분을 빼주기만..
[ BOJ / 파이썬 ] 15650 N과 M (2) | 제출 1 | import sys input = sys.stdin.readline from itertools import combinations n,m = map(int, input().split()) result = list(combinations(range(1,n+1), m)) for r in sorted(result): print(' '.join(list(map(str,r)))) : 파이썬의 combinations 라이브러리를 사용한 풀이입니다. : combinations는 중복 없는 순열을 찾는 라이브러리입니다. : 출력 시에 각 순열의 아이템을 str 형태로 바뀐 뒤 문자열로 붙여 출력하게 했습니다. combinations 중복조합: 중복을 허용하지 않고 뽑음. 조합이므로 순서는 상관 없음. ..
[ BOJ / 파이썬 ] 2630 색종이 자르기 + 도형 자르고 그리는 재귀 문제 공략 | 제출 1 | import sys input = sys.stdin.readline n = int(input()) paper = [list(map(int, input().split())) for _ in range(n)] paper_color = { 0: 0, 1: 0} def check_paper(x, y, n): if n == 1: paper_color[paper[x][y]] += 1 return isSame = True for i in range(n): if not isSame: break for j in range(n): if paper[x][y] != paper[x+i][y+j]: isSame = False break if isSame: paper_color[paper[x][y]] += 1 el..
[ BOJ / 파이썬 ] 2448 별 찍기 * 다시 풀기 * https://ssu-gongdoli.tistory.com/79 https://hongcoding.tistory.com/90 > 나중에 참고할 풀이 블로그 너무 어렵다.. 왜 머리가 안 돌아가지.? 이 문제 뭐지ㅠ (22.08.25) | 제출 1 | import sys input = sys.stdin.readline n = int(input()) template = [[' ',' ','*',' ',' ',' '], [' ','*',' ','*',' ',' '], ['*','*','*','*','*',' ']] def recursive(x, y, n): if n == 3: for i in range(n): for j in range(n): result[x+i][y+j] = template[i][j] ret..