{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Week06.ipynb","provenance":[],"authorship_tag":"ABX9TyPocqP0AGab+0jPCFK8ore7"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# 第六週上課內容"],"metadata":{"id":"9xoK4OIjIfZA"}},{"cell_type":"markdown","source":["###GitHub 教材參考資料\n","\n","1. [https://github.com/htchen/i2p-nthu/blob/master/程式設計一/function/function.md](https://github.com/htchen/i2p-nthu/blob/master/%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88%E4%B8%80/function)\n","\n","2. [https://github.com/htchen/i2p-nthu/tree/master/程式設計一/Recursive](https://github.com/htchen/i2p-nthu/tree/master/%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88%E4%B8%80/Recursive)\n"],"metadata":{"id":"05G1Fqg6GRZX"}},{"cell_type":"markdown","source":["##Warm up\n","\n","函數呼叫的過程\n","\n","`f(g(h(x)))`\n","\n","\n","\n","階乘的遞迴定義\n","\n","```\n","n! = n * (n-1)!, if n > 0\n","\n","0! = 1, otherwise\n","```\n","\n"],"metadata":{"id":"8iMmA38h7WlI"}},{"cell_type":"code","source":["%%writefile test.c\n","#include <stdio.h>\n","\n","long long factorial(long long );\n","\n","int main(void)\n","{\n","    int n;\n","    \n","    scanf(\"%d\", &n);\n","    printf(\"%lld\\n\", factorial(n));\n","    return 0;\n","}\n","\n","long long factorial(long long n) \n","{\n","  if (n > 0) {\n","    return n * factorial(n-1);\n","  } else {\n","    return 1;\n","  }\n","}"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8WRvyvJtM9rr","executionInfo":{"status":"ok","timestamp":1647949001636,"user_tz":-480,"elapsed":314,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"7d46acd6-a35d-47b3-ea28-302471db6b5f"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Overwriting test.c\n"]}]},{"cell_type":"code","source":["%%shell\n","gcc test.c\n","./a.out"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"lluQMLZYNiuc","executionInfo":{"status":"ok","timestamp":1647949049430,"user_tz":-480,"elapsed":4556,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"fc3088f4-2c95-4cde-c525-1adef9c1233c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["20\n","2432902008176640000\n"]},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{},"execution_count":27}]},{"cell_type":"markdown","source":["##Example 1"],"metadata":{"id":"xKQTDbepnNNi"}},{"cell_type":"code","source":["%%writefile E06_01.c\n","/*\n","宣告 double power(double n, int p); ，然後使用下面的方式來做遞迴:\n","(0) 先檢查 p 是否等於 0，是的話就傳回答案 1; 若 p 不等於 0 則先算出 ans = power(n, p/2);\n","(1) 如果 p 是偶數，則 return ans*ans;\n","(2) 如果 p 是奇數，則 return n*ans*ans;\n","這裡有使用到整數除法無條件捨去的特性。\n","*/\n","#include <stdio.h>\n","double power(double n, int p);\n","\n","int main(void)\n","{\n","  double x;\n","  int t;\n","  scanf(\"%lf\", &x);\n","  scanf(\"%d\", &t);\n","  printf(\"%f\\n\", power(x, t));\n","\n","  return 0;\n","}\n","\n","double power(double n, int p)\n","{\n","  double ans;\n","  if (p == 0) {\n","      return 1.0;\n","  }\n","  ans = power(n, p/2);\n","  if (p%2 == 0) {\n","      return ans*ans;\n","  } else {\n","      return n*ans*ans;\n","  }\n","}\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"0mw6JWUW0AcH","executionInfo":{"status":"ok","timestamp":1647949805616,"user_tz":-480,"elapsed":312,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"da3b769f-6302-48ee-d3e6-b048e0c6455d"},"execution_count":31,"outputs":[{"output_type":"stream","name":"stdout","text":["Overwriting E06_01.c\n"]}]},{"cell_type":"code","source":["%%shell\n","gcc E06_01.c -o E06_01\n","./E06_01"],"metadata":{"id":"GUvRctw8a1Mx","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1647949617648,"user_tz":-480,"elapsed":6133,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"7404977e-434f-4d2c-ebd3-cd99184675fb"},"execution_count":30,"outputs":[{"output_type":"stream","name":"stdout","text":["2.3\n","5\n","64.363430\n"]},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{},"execution_count":30}]},{"cell_type":"markdown","source":["#Example 2\n"],"metadata":{"id":"jUvTa6IsbHE0"}},{"cell_type":"code","source":["%%writefile E06_02.c\n","/*\n","用 recursive 方式，產生下面的輸出\n","Input: 10 \n","Output: ((((1+2)*3+4)*5+6)*7+8)*9+10=4555 \n","Input: 11 \n","Output: (((((1+2)*3+4)*5+6)*7+8)*9+10)*11=50105\n","*/\n","\n","#include <stdio.h>\n","int show_parenthesis(int n);\n","\n","int main(void)\n","{\n","  int n;\n","  int ans;\n","  scanf(\"%d\", &n);\n","  ans = show_parenthesis(n);\n","  printf(\"=%d\\n\", ans);\n","  return 0;\n","}\n","\n","int show_parenthesis(int n)\n","{\n","  int ans = 1;\n","  if (n>1) {\n","    if (n%2) {\n","      printf(\"(\");\n","    }\n","    ans = show_parenthesis(n-1);\n","    if (n%2) {\n","      printf(\")*%d\",n);  \n","      ans = ans * n;      \n","    } else {\n","      printf(\"+%d\", n);\n","      ans = ans + n;\n","    }\n","  } else {\n","      printf(\"1\");\n","  }\n","  return ans;\n","}"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"zzerIzxQbDDy","executionInfo":{"status":"ok","timestamp":1647951643283,"user_tz":-480,"elapsed":313,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"c27903fb-7d50-4ded-9512-3e0065447a48"},"execution_count":76,"outputs":[{"output_type":"stream","name":"stdout","text":["Overwriting E06_02.c\n"]}]},{"cell_type":"code","source":["%%shell\n","gcc E06_02.c -o E06_02\n","./E06_02"],"metadata":{"id":"I5DNPoyJbdxQ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1647951650502,"user_tz":-480,"elapsed":3605,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"7e450f14-9c87-4c95-d808-33835f902865"},"execution_count":77,"outputs":[{"output_type":"stream","name":"stdout","text":["9\n","((((1+2)*3+4)*5+6)*7+8)*9=4545\n"]},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{},"execution_count":77}]},{"cell_type":"markdown","source":["##Example 3 \n","\n","Tower of Honoi\n","DHTML demo\n","http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm"],"metadata":{"id":"dHQBaha-cAUQ"}},{"cell_type":"code","source":["%%writefile E06_03.c\n","#include <stdio.h>\n","void hanoi(char from, char to, char buffer, int n_disks);\n","\n","int main(void)\n","{\n","\tint n;\n","\tprintf(\"Number of disks: \");\n","\tscanf(\"%d\", &n);\n","\thanoi('1', '3', '2', n);\n","\n","\treturn 0;\n","}\n","\n","/* move n disks from a to b using c as a buffer */\n","void hanoi(char a, char b, char c, int n)\n","{\n","\tif (n == 0) {\t\t\n","\t\treturn;\n","\t}\n","\thanoi(a, c, b, n-1); /* move the top n-1 disks from a to c */\n","\tprintf(\"Move the top disk from Tower %c to Tower %c\\n\", a, b); /* move the last disk from a to b */\n","\thanoi(c, b, a, n-1); /* move the top n-1 disks from c to b */\n","}\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-idcCj0USzhZ","executionInfo":{"status":"ok","timestamp":1647954028789,"user_tz":-480,"elapsed":312,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"749aa0bf-6046-4b16-9acc-b5b5855fdaba"},"execution_count":78,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing E06_03.c\n"]}]},{"cell_type":"code","source":["%%shell\n","gcc E06_03.c -o E06_03\n","./E06_03"],"metadata":{"id":"ixZxf1vbTUXY","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1647954049386,"user_tz":-480,"elapsed":8061,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"0f024f4a-d525-4138-e48d-8439fa398d36"},"execution_count":79,"outputs":[{"output_type":"stream","name":"stdout","text":["Number of disks: 3\n","Move the top disk from Tower 1 to Tower 3\n","Move the top disk from Tower 1 to Tower 2\n","Move the top disk from Tower 3 to Tower 2\n","Move the top disk from Tower 1 to Tower 3\n","Move the top disk from Tower 2 to Tower 1\n","Move the top disk from Tower 2 to Tower 3\n","Move the top disk from Tower 1 to Tower 3\n"]},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{},"execution_count":79}]},{"cell_type":"code","source":["%%shell\n","gcc E06_03.c -o E06_03\n","./E06_03"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QK5gvg33uTBs","executionInfo":{"status":"ok","timestamp":1647954376501,"user_tz":-480,"elapsed":7132,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"bfcaa2ca-ae85-4ffd-877c-0ca876d207bf"},"execution_count":82,"outputs":[{"output_type":"stream","name":"stdout","text":["Number of disks: 4\n","Move the top disk from Tower 1 to Tower 2\n","Move the top disk from Tower 1 to Tower 3\n","Move the top disk from Tower 2 to Tower 3\n","Move the top disk from Tower 1 to Tower 2\n","Move the top disk from Tower 3 to Tower 1\n","Move the top disk from Tower 3 to Tower 2\n","Move the top disk from Tower 1 to Tower 2\n","Move the top disk from Tower 1 to Tower 3\n","Move the top disk from Tower 2 to Tower 3\n","Move the top disk from Tower 2 to Tower 1\n","Move the top disk from Tower 3 to Tower 1\n","Move the top disk from Tower 2 to Tower 3\n","Move the top disk from Tower 1 to Tower 2\n","Move the top disk from Tower 1 to Tower 3\n","Move the top disk from Tower 2 to Tower 3\n"]},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{},"execution_count":82}]},{"cell_type":"markdown","source":["#第七週上課內容"],"metadata":{"id":"L05SdU5f7OLE"}},{"cell_type":"markdown","source":["##Example 4\n","用遞迴方式計算最大公因數"],"metadata":{"id":"mviKuWaapY32"}},{"cell_type":"code","source":["%%writefile E06_04.c\n","#include <stdio.h>\n","int gcd(int a, int b);\n","int main(void)\n","{\n","\tint x, y;\n","\tscanf(\"%d%d\", &x, &y);\n","\tprintf(\"gcd(%d, %d) = %d\\n\", x, y, gcd(x, y));\n","     return 0;\n","}\n","int gcd(int a, int b)\n","{\n","\tif (b==0)\n","\t\treturn a;\n","\telse\n","\t\treturn gcd(b, a%b);\n","}"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"7ZODSFC-pmP2","executionInfo":{"status":"ok","timestamp":1647919041761,"user_tz":-480,"elapsed":585,"user":{"displayName":"HT Chen","photoUrl":"https://lh3.googleusercontent.com/a/default-user=s64","userId":"17748361917871513601"}},"outputId":"fc1f24f6-ec90-441d-9352-501da7dcfd9d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Writing E06_04.c\n"]}]},{"cell_type":"code","source":["%%shell\n","gcc E06_04.c -o E06_04\n","./E06_04"],"metadata":{"id":"HMqFdYymQIzj"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["##Example 5\n","三座城堡問題：\n","在 3x3 的棋盤上擺三個城堡。\n","城堡只能走水平或垂直方向，可以吃掉在同一條水平或垂直線上的其他棋子。所以每個 row 只能擺一個城堡。\n","\n","修改底下的程式碼，使得最後只輸出滿足互不衝突、互不吃掉對方的擺法。"],"metadata":{"id":"Mp5lAOxDodYd"}},{"cell_type":"code","source":["%%writefile E06_05.c\n","#include <stdio.h>\n","\n","int board[5][5];\n","void place(int row);\n","void show_board(void)\n","{\n","\tint i, j;\n","\tfor (i=1; i<=3; i++) {\n","\t\tfor (j=1; j<=3; j++) {\n","\t\t\tprintf(\" %d\", board[i][j]);\n","\t\t}\n","\t\tprintf(\"\\n\");\n","\t}\n","\tprintf(\"\\n\");\n","}\n","int main(void)\n","{\n","\tplace(1);\n","\treturn 0;\n","}\n","void place(int row)\n","{\n","\tint i;\n","\tif (row>3) {\n","\t\tshow_board();\n","\t} else {\n","\t\tfor (i=1; i<=3; i++) {\n","\t\t\tboard[row][i] = 1;\n","\t\t\tplace(row+1);\n","\t\t\tboard[row][i] = 0;\n","\t\t}\n","\t}\n","}"],"metadata":{"id":"LZ8575uZo5hG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["%%shell\n","gcc E06_05.c -o E06_05\n","./E06_05"],"metadata":{"id":"f1ClwjOzsdsQ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["三座城堡 sample code https://gist.github.com/htchen/e86a7d4293d6b33e2dce\n"],"metadata":{"id":"XK_DGBYvpFt1"}},{"cell_type":"markdown","source":["##Exercise 1\n","\n","**Four Queens**\n","\n","原版的經典問題是將八個皇后放在西洋棋棋盤上 [Wikipedia](http://en.wikipedia.org/wiki/Eight_queens_puzzle)\n","\n","任兩個皇后都不能在同一水平線或垂直線或45度線上，否則就會吃掉對方。也就是說，棋盤中任何橫列、直行、以及斜線都只能有一個皇后。\n","\n","我們先考慮四個皇后的情況。\n","請修改範例程式，算出所有可能的放置方式。\n","\n","譬如，四個皇后放在4x4的棋盤上，只有下面兩種擺法：\n","```\n","O@OO\n","OOO@\n","@OOO\n","OO@O\n","..........\n","OO@O\n","@OOO\n","OOO@\n","O@OO\n","..........\n","```\n","\n","上面第一種擺法可以用 `1, 3, 0, 2` 表示，\n","代表 `row 0` 的皇后放在 `column 1` 的位置，`row 1` 皇后放在 `column 3`，`row 2` 皇后放在 `column 0`，`row 3` 皇后放在 `column 2`。\n","\n","同理，第二種擺法可以用 `2, 0, 3, 1` 表示。\n","\n","請修改範例程式，讓它能輸出上面的結果，並且試著把 `NQ` 改成 `6`，看看能否得到下面四種擺法。\n","```\n","O@OOOO\n","OOO@OO\n","OOOOO@\n","@OOOOO\n","OO@OOO\n","OOOO@O\n","..........\n","OO@OOO\n","OOOOO@\n","O@OOOO\n","OOOO@O\n","@OOOOO\n","OOO@OO\n","..........\n","OOO@OO\n","@OOOOO\n","OOOO@O\n","O@OOOO\n","OOOOO@\n","OO@OOO\n","..........\n","OOOO@O\n","OO@OOO\n","@OOOOO\n","OOOOO@\n","OOO@OO\n","O@OOOO\n","..........\n","```\n","\n","以底下的程式碼為基礎，把缺少的部分補上："],"metadata":{"id":"2HgZZI7vpHFE"}},{"cell_type":"code","source":["%%writefile W06_01.c\n","#include <stdio.h>\n","#define NQ 4\n","\n","/* q[i] 記錄的是在第 i 列 (row) 出現的皇后，要擺在第幾行 (column) */\n","/* 譬如，q[] 的內容如果是 {2, 0, 3, 1}，表示四個皇后分別放在棋盤的 (0,2), (1,0), (2,3), (3,1) 四個位置 */\n","\n","int q[NQ] = {0};\n","\n","void place(int row);\n","int valid(int row, int col);\n","void display(void);\n","\n","int main(void)\n","{\n","\n","\tplace(0);\n","\treturn 0;\n","}\n","\n","/*\n","判斷目前的狀況下，如果在 row, col 位置放入新的皇后\n","是否會和之前的皇后衝突\n","如果是合法的放置方式 return 1;\n","如果有衝突 return 0;\n","*/\n","int valid(int row, int col)\n","{\n","\tint i;\n","\tfor (i=0; i<=row-1; i++) {\n","\t\t/*\n","\t\tif ( ??? )\n","\t\treturn 0;\n","\t\t */\n","\t}\n","\treturn 1;\n","}\n","\n","void display(void)\n","{\n","\t/* ??? */\n","}\n","\n","void place(int row)\n","{\n","\tint j;\n","\tif (row == NQ) {\n","\t\tdisplay();\n","\t} else {\n","\t\tfor (j=0; j<NQ; j++) {\n","\t\t\tif (valid(row, j)) {\n","\t\t\t\t/* ??? */\n","\t\t\t}\n","\t\t}\n","\t}\n","}\n"],"metadata":{"id":"1rqwHlHmrEi9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["%%shell\n","gcc W06_01.c -o W06_01\n","./W06_01"],"metadata":{"id":"3yZlwmmjsiKP"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["四個皇后 sample code https://gist.github.com/htchen/f0082a35ff9d04231cfd"],"metadata":{"id":"vFIdo3PBrPPI"}},{"cell_type":"markdown","source":["##Exercise 2\n","\n","**Make Change**\n","\n","輸入不同面值的銅板，然後輸入一個金額，將全部可能的找零方式列出。\n","\n","譬如有 1 元、5元、10元銅板，\n","要湊出 17 元，如果將可能的組合表示成 `\"(1元個數,5元個數,10元個數)\"`，總共會有下列幾種方法：\n","```\n","(2,1,1)\n","(2,3,0)\n","(7,0,1)\n","(7,2,0)\n","(12,1,0)\n","(17,0,0)\n","```\n","\n","我們可以用 *遞迴* 方式來看待這個問題：\n","首先依序考慮使用 1元、5元、10元的情況。\n","假設現在我們手邊有 `M` 元需要換成銅板，\n","從 1 元開始考慮，\n","我們可以把問題區分成兩種可能：\n","1. 使用 1 元，因此狀態變成手邊有 `M-1` 元，然後試著繼續用 1 元、5元、10元來湊。\n","2. 完全不使用 1，因此狀態變成手邊還是有 `M` 元，但是只考慮用 5元 和 10元來湊。\n","\n","以上兩種可能都可以讓問題簡化，\n","第一種是讓錢變少，另一種則是讓需要考慮的銅板面值變少。\n","\n","用這樣的方式繼續簡化下去，\n","只會需要可慮幾種結束方式：\n","1. 手邊剩下 0 元，表示成功湊出組合。\n","2. 手邊剩下 `M` 元，`M<0`，表示這樣的組合不可能湊出需要的金額。\n","3. 沒有任何可用的銅板面額可供組合。\n","\n","其餘情況就繼續遞迴。\n","\n","底下是未完成的程式碼："],"metadata":{"id":"7co6TxbjrUL2"}},{"cell_type":"code","source":["%%writefile W06_02.c\n","#include <stdio.h>\n","#define MAXNV 5\n","int values[MAXNV];\n","int numbers[MAXNV];\n","\n","void show(int nv);\n","void change(int amount, int smallest, int nv);\n","\n","int main(void)\n","{\n","\tint nv, i;\n","\tint money;\n","\n","\tscanf(\"%d\", &nv);\n","\tif (nv>MAXNV || nv<1) return 0;\n","\n","\tfor (i=0; i<nv; i++) {\n","\t\tscanf(\"%d\", &values[i]);\n","\t}\n","\n","\tscanf(\"%d\", &money);\n","\tchange(money, 0, nv);\n","\n","\treturn 0;\n","}\n","\n","void show(int nv)\n","{\n","\tint i;\n","\tprintf(\"(%d\", numbers[0]);\n","\tfor (i=1; i<nv; i++) {\n","\t\tprintf(\",%d\", numbers[i]);\n","\t}\n","\tprintf(\")\\n\");\n","}\n","\n","void change(int amount, int smallest, int nv)\n","{\n","\tif (smallest<nv) {\n","\t\tif (amount == 0) {\n","\t\t\tshow(nv);\n","\t\t} else if (amount > 0) {\n","\t\t\t/* your code here */\n","\n","\t\t}\n","\t}\n","}"],"metadata":{"id":"bNrwKmsysNfy"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["測試底下的輸入輸出範例。\n","\n","輸入:\n","``` \n","3\n","1 5 10\n","17\n","```\n","第一個數字代表有幾種不同面值的銅板，\n","接下來就是對應的銅板面值，\n","最後一個數字是要找零的金額。\n","\n","輸出:\n","```\n","(2,1,1)\n","(2,3,0)\n","(7,0,1)\n","(7,2,0)\n","(12,1,0)\n","(17,0,0)\n","```"],"metadata":{"id":"sr3iiC0Ys2zq"}},{"cell_type":"code","source":["%%shell\n","gcc W06_02.c -o W06_02\n","./W06_02"],"metadata":{"id":"puvNtlHWssLK"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["換銅板 sample code https://gist.github.com/htchen/1613e9c8835f90b7b009"],"metadata":{"id":"WixOMTcEtF5p"}},{"cell_type":"markdown","source":["##Exercise 3\n","\n","**N Queens**\n","\n","將 `N` 個皇后放在西洋棋棋盤上，\n","棋盤中任何橫列、直行、以及斜線都只能有一個皇后，\n","算出有幾種合法的放置方式，若無解則答案為 `0`。\n"],"metadata":{"id":"eAEfEJkitIw9"}},{"cell_type":"code","source":["%%writefile W06_03.c\n","#include <stdio.h>\n","#define MAX 12\n","/* q[i] 記錄的是在第 i 列 (row) 出現的皇后，要擺在第幾行 (column) */\n","/* 譬如，q[] 的內容如果是 {2, 0, 3, 1}，表示四個皇后分別放在棋盤的\n","(0,2), (1,0), (2,3), (3,1) 四個位置 */\n","int q[MAX] = {0};\n","int N;\n","void place(int row);\n","int valid(int row, int col);\n","int main(void)\n","{\n","\tscanf(\"%d\", &N);\n","\tplace(0);\n","\t/* printf(???); */\n","\treturn 0;\n","}\n","/* 判斷目前的狀況下，如果在 row, col 位置放入新的皇后 是否會和之前的皇后衝突 如果是合法的放置方式 return 1; 如果有衝突 return 0; */\n","int valid(int row, int col)\n","{\n","\tint i;\n","\tfor (i=0; i<=row-1; i++) {\n","\t\t/* if ( ??? ) return 0; */\n","\t}\n","\treturn 1;\n","}\n","void place(int row)\n","{\n","\tint j;\n","\tif (row == N) {\n","\t\t/* ??? */\n","\t} else {\n","\t\tfor (j=0; j<N; j++) {\n","\t\t\tif (valid(row, j)) {\n","\t\t\t\t/* ??? */\n","\t\t\t}\n","\t\t}\n","\t}\n","}"],"metadata":{"id":"CUD4U1iTuWSa"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["*Input*\n","```\n","N\n","N 為 1~12 的正整數\n","```\n","\n","*Output*\n","```\n","有幾種擺法\n","(不須加換行)\n","```\n","\n","*Sample Input*\n","```\n","8\n","```\n","\n","*Sample Output*\n","```\n","92\n","```"],"metadata":{"id":"t8e2YmcUujMQ"}},{"cell_type":"code","source":["%%shell\n","gcc W06_03.c -o W06_03\n","./W06_03"],"metadata":{"id":"dn61T8gVudiD"},"execution_count":null,"outputs":[]}]}