Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Alex4386/701cff54d72537bd392771db3109ac88 to your computer and use it in GitHub Desktop.
Save Alex4386/701cff54d72537bd392771db3109ac88 to your computer and use it in GitHub Desktop.
Editted IJustWantedToPlayMinecraft using Recursion
# DP 써도 되긴 하는데 그냥
# Recursive 로 싸아악
# 색깔 코드
# A B C D E
colors = [ 0, 0, 0, 0, 0 ]
# 조건에 충족하는 지 확인하는 코드
def check_condition(colors):
return (
colors[0] != colors[1] and # A랑 B는 색깔이 같을 수 없다.
colors[0] != colors[2] and # A랑 C는 색깔이 같을 수 없다.
colors[0] != colors[3] and # A랑 D는 색깔이 같을 수 없다.
colors[0] != colors[4] and # A랑 E는 색깔이 같을 수 없다.
colors[1] != colors[2] and # B랑 C는 색깔이 같을 수 없다.
colors[2] != colors[3] and # C랑 D는 색깔이 같을 수 없다.
colors[3] != colors[4] # D랑 E는 색깔이 같을 수 없다.
)
# idx 번째 칸의 색깔을 칠합니다
def coloring_it(colors, idx):
# 조건 만족하는 거 세는거랑...
count = 0
# 전체 세는 거 변수 정의
total_count = 0
# 일단 복사는 해줘요. C로 치면 포인터가 같기 때문에.
# 사실 안해줘도 상관은 없는데, 확실하게 하기 위해서.
colors_tmp = colors.copy()
# 저 칸들에 칠할 수 있는 색깔의 최대 가짓수는
# 색을 칠할 수 있는 칸의 수!
#
# 색깔을 하나씩 칠해본다.
for color in range(len(colors_tmp)):
this_count = 0
this_total_count = 0
# 색깔을 칠하고,
colors_tmp[idx] = color
# 마지막 까지 칠했다면?
if (len(colors_tmp) - 1) == idx:
# 조건에 맞는지 확인해 보고
if check_condition(colors_tmp):
# 조건에 맞는 거 세는 변수에 하나 더하기
this_count += 1
# 조건에 안 맞아도 전체 변수에는 올리기
this_total_count += 1
else:
# 아니라면 다음번째 구하기
this_count, this_total_count = coloring_it(colors_tmp, idx+1)
# 이번 색깔 칠한 것을 전체 가짓수에 더하기
count += this_count
total_count += this_total_count
# 이번 횟수의 색깔 칠한것을 전 케이스로 내뱉기, 퉷!
return (count, total_count)
# 색깔~ 칠하기~ 놀이~ 스타트~
count, total_count = coloring_it(colors, 0)
print("전체 경우의 수: " + str(total_count) + "\n조건과 일치하는 경우의 수: " + str(count))
print("답: " + str(count))
/*
러스트로 코드 짜기~
게임 러스트 아니다.
*/
// 이건 주석 적기 귀찮아요~
fn check_condition(colors: Vec<i32>) -> bool {
colors[0] != colors[1] &&
colors[0] != colors[2] &&
colors[0] != colors[3] &&
colors[0] != colors[4] &&
colors[1] != colors[2] &&
colors[2] != colors[3] &&
colors[3] != colors[4]
}
fn color_it(colors: &mut Vec<i32>, i: i32) -> i32 {
let mut count: i32 = 0;
let max_color_count = colors.len();
for color_id in 0..max_color_count {
colors[i as usize] = color_id as i32;
if i == (colors.len() - 1) as i32 {
if check_condition((&colors).to_vec()) {
count += 1;
}
} else {
count += color_it(colors, i+1);
}
}
return count
}
fn main() {
let mut vec: Vec<i32> = vec!(0,0,0,0,0);
let count = color_it(&mut vec, 0);
println!("{}", count);
}
@hdavid0510
Copy link

장황한 코드를 보고 설마 포크가 없겠어... 싶어 들어왔는데 너무나 고퀄리티 주석이 함유된 코드네요. 심지어 전혀 생각지도 않았던 Recursive 구현법까지 잘 배우고 갑니다. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment