Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Alex4386/701cff54d72537bd392771db3109ac88 to your computer and use it in GitHub Desktop.

Select an option

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);
}
@Alex4386
Copy link
Author

Alex4386 commented Jan 4, 2021

P.S. ์ € PS ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ผ๋ถ€๋Ÿฌ ์ตœ์ ํ™” ์•ˆํ•œ๊ฑฐ์˜ˆ์š”.

@Alex4386
Copy link
Author

Alex4386 commented Jan 4, 2021

image

@Alex4386
Copy link
Author

Alex4386 commented Jan 5, 2021

2021-01-05: Rust ์–ธ์–ด ๊ตฌํ˜„์ฒด ์ถ”๊ฐ€.
์—ฌ๋Ÿฌ๋ถ„๋„ Rust ํ•˜์„ธ์š”

@hdavid0510
Copy link

์žฅํ™ฉํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์„ค๋งˆ ํฌํฌ๊ฐ€ ์—†๊ฒ ์–ด... ์‹ถ์–ด ๋“ค์–ด์™”๋Š”๋ฐ ๋„ˆ๋ฌด๋‚˜ ๊ณ ํ€„๋ฆฌํ‹ฐ ์ฃผ์„์ด ํ•จ์œ ๋œ ์ฝ”๋“œ๋„ค์š”. ์‹ฌ์ง€์–ด ์ „ํ˜€ ์ƒ๊ฐ์ง€๋„ ์•Š์•˜๋˜ Recursive ๊ตฌํ˜„๋ฒ•๊นŒ์ง€ ์ž˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค. ๐Ÿ˜

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