Last active
August 29, 2015 14:14
-
-
Save haje01/84d7b6e604af75502a9e to your computer and use it in GitHub Desktop.
동적계획법 보강 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:ebff29a8f3187bc67f1472ae907507305cf2248c100122035a964c710ef9fb1a" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"n = 5\n", | |
"for i in range(n):\n", | |
" for j in range(i+1, n):\n", | |
" for k in range(j+1, n):\n", | |
" print i, j, k" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 1 2\n", | |
"0 1 3\n", | |
"0 1 4\n", | |
"0 2 3\n", | |
"0 2 4\n", | |
"0 3 4\n", | |
"1 2 3\n", | |
"1 2 4\n", | |
"1 3 4\n", | |
"2 3 4\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"elements = 5\n", | |
"\n", | |
"def pick(n, picked, to_pick):\n", | |
" if to_pick == 0:\n", | |
" print ' '.join(str(s) for s in picked)\n", | |
" return\n", | |
" \n", | |
" smallest = 0 if len(picked) == 0 else picked[-1] + 1\n", | |
" for i in range(smallest, n):\n", | |
" picked.append(i)\n", | |
" pick(elements, picked, to_pick - 1)\n", | |
" picked.pop()\n", | |
"\n", | |
"pick(elements, [], 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 1 2\n", | |
"0 1 3\n", | |
"0 1 4\n", | |
"0 2 3\n", | |
"0 2 4\n", | |
"0 3 4\n", | |
"1 2 3\n", | |
"1 2 4\n", | |
"1 3 4\n", | |
"2 3 4\n" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"cities = 'abcd'\n", | |
"distances = {'ab': 4, 'bc': 3, 'cd': 8, 'bd': 6, 'ad': 2, 'ac': 7}\n", | |
"\n", | |
"def get_path_length(path):\n", | |
" routes = [sorted(r) for r in zip(path, path[1:])]\n", | |
" l = sum([distances[''.join(r)] for r in routes])\n", | |
" print l\n", | |
" return l\n", | |
"\n", | |
"def shortest_path(cur_path):\n", | |
" if len(cur_path) == len(cities):\n", | |
" print cur_path\n", | |
" return get_path_length(cur_path)\n", | |
" ret = 987654321\n", | |
" for city in cities:\n", | |
" if city in cur_path:\n", | |
" continue\n", | |
" cur_path.append(city)\n", | |
" ret = min(ret, shortest_path(cur_path))\n", | |
" cur_path.pop()\n", | |
" return ret\n", | |
"\n", | |
"shortest_path([])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['a', 'b', 'c', 'd']\n", | |
"15\n", | |
"['a', 'b', 'd', 'c']\n", | |
"18\n", | |
"['a', 'c', 'b', 'd']\n", | |
"16\n", | |
"['a', 'c', 'd', 'b']\n", | |
"21\n", | |
"['a', 'd', 'b', 'c']\n", | |
"11\n", | |
"['a', 'd', 'c', 'b']\n", | |
"13\n", | |
"['b', 'a', 'c', 'd']\n", | |
"19\n", | |
"['b', 'a', 'd', 'c']\n", | |
"14\n", | |
"['b', 'c', 'a', 'd']\n", | |
"12\n", | |
"['b', 'c', 'd', 'a']\n", | |
"13\n", | |
"['b', 'd', 'a', 'c']\n", | |
"15\n", | |
"['b', 'd', 'c', 'a']\n", | |
"21\n", | |
"['c', 'a', 'b', 'd']\n", | |
"17\n", | |
"['c', 'a', 'd', 'b']\n", | |
"15\n", | |
"['c', 'b', 'a', 'd']\n", | |
"9\n", | |
"['c', 'b', 'd', 'a']\n", | |
"11\n", | |
"['c', 'd', 'a', 'b']\n", | |
"14\n", | |
"['c', 'd', 'b', 'a']\n", | |
"18\n", | |
"['d', 'a', 'b', 'c']\n", | |
"9\n", | |
"['d', 'a', 'c', 'b']\n", | |
"12\n", | |
"['d', 'b', 'a', 'c']\n", | |
"17\n", | |
"['d', 'b', 'c', 'a']\n", | |
"16\n", | |
"['d', 'c', 'a', 'b']\n", | |
"19\n", | |
"['d', 'c', 'b', 'a']\n", | |
"15\n" | |
] | |
}, | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 39, | |
"text": [ | |
"9" | |
] | |
} | |
], | |
"prompt_number": 39 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment