發表文章

目前顯示的是 10月, 2020的文章

Tree Search(樹狀搜尋)

一、什麼是 Tree Search(樹狀搜尋)? 在人工智慧(AI)與演算法中,許多問題都可以表示成一棵樹(圖一): 起點(A) / | \ B C D /|\ | / \ E F G H I J 每個節點(Node)代表一種狀態(State)。 例如: 迷宮中的位置 棋局的盤面 路徑規劃中的城市 遊戲中的決策 搜尋演算法的目的: 從起點找到目標節點(Goal Node) 二、Breadth First Search (BFS) 核心思想 先搜尋離起點最近的節點。 一層一層往外擴展。 Level 0: A Level 1: B C D Level 2: E F G H I J 搜尋順序: A B C D E F G H I J 圖一結果: A → B → C → D → E → F → G → H → I → J 使用資料結構 Queue(佇列) FIFO: First In First Out 先進先出 例如: Queue: A 取出A 加入B,C,D Queue: B,C,D BFS特性 優點 如果邊權重相同: BFS一定找到最短路徑。 缺點 需要大量記憶體。 假設每個節點有10個子節點: 深度5: 10^5 = 100000 需要保存很多節點。 時間複雜度 O(V + E) V = Vertex(節點數) E = Edge(邊數) 三、Depth First Search (DFS) 核心思想 一路往下走到底。 不能走才回頭。 A | B | E 然後: A | B | F 搜尋順序 圖一結果: A B E F G C H D I J 使用資料結構 Stack(堆疊) LIFO Last In First Out 後進先出 例如: push(B) push(C) push(D) pop() => D DFS特性 優點 記憶體需求小。 只需保存: 目前路徑 即可。 缺點 可能找到很差的解。 例如: A ├── Goal └── 巨大子樹 DFS可能先跑完整個巨大子樹。 時間複雜度 O(V+E)...

MakeCode Microbit 遊戲設計:簡易射擊遊戲(Microbit Game: Simple Shooting Game)

圖片
本篇文章將講解如何製作一個簡易射擊遊戲。 一、遊戲功能如下(Game Design): 砲台在畫面下方移動。The turret moves between the bottom of LED matrix. 按A鍵砲台燈向左移動。 The turret moves to left when button A is pressed. 按B鍵砲台燈向右移動。The turret moves to right when button B is pressed. 按A+B鍵發射子彈。Firing when button A and button B are pressed. 敵機隨機在最上方的位置出現。 The enemy moves between the top of LED matrix. 子彈打到敵機時,得一分。Score 1 point when the enemy is hit by a bullet. 二、積木程式(Blocks Code) 遊戲啟動時(On Start): 敵機隨機在最上方的位置出現。 The enemy moves between the top of LED matrix. 砲台在畫面下方移動。The turret moves between the bottom of LED matrix. 按A鍵砲台燈向左移動。 The turret moves to left when button A is pressed. 按B鍵砲台燈向右移動。The turret moves to right when button B is pressed. 按A+B鍵發射子彈。Firing when button A and button B are pressed. 子彈打到敵機時,得一分,。Score 1 point when the enemy is hit by a bullet. 三、結果(The Results) 影片(Demo Video): 請自由發揮額外功能,例如玩家生命等。 More Ideas examples: player's life...etc.  範例網址(Example Code): https://makecode.microbit.org/_EEAd7hdq8hav 若您覺得文章寫得不錯,請點選文章上的廣...

Solutions to Lightbot

圖片
底下是  LightBot : Code Hour  的參考解法 The following pictures are possible solutions to LightBot : Code Hour . Level 1-1: Level 1-2: Level 1-3: Level 1-4: Level 1-5: Level 1-6: Level 1-:7 Level 1-8: Level 2-1: Level 2-2: Level 2-3: Level 2-4: Level 2-5: Level 2-6: Level 3-1: Level 3-2: Level 3-:3 Level 3-4: Level 3-5: Level 3-6: 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

C語言練習題:迴圈的魅力(C language exercise: Loop's charisma)

圖片
練習一:奇數數列 使用迴圈輸出 1 到 100 之間的奇數。 Exercise 1: Odd Sequence Print the odd numbers between 1 to 100. 練習一參考解法: Exercise 1 solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <stdio.h> #include <stdlib.h> int main () { int i; // FOR loop 1 for (i = 1 ; i <= 100 ; i += 2 ) printf( "%d, " , i); printf( " \n\n " ); // FOR loop 2 for (i = 1 ; i <= 100 ; i ++ ) if (i % 2 ) printf( "%d, " , i); printf( " \n\n " ); // WHILE loop i = 1 ; while ( i <= 100 ) { if (i % 2 ) printf( "%d, " , i); i ++ ; } printf( " \n\n " ); // DO-WHILE loop i = 1 ; do { if (i % 2 ) printf( "%d, " , i); i ++ ; } while (i <= 100 ); return 0 ; } 練習二:範圍 輸...

C語言練習題:條件判斷(C language exercise: If Condition)

圖片
練習一:入學許可 如果您的在校成績有高於80分,就可以申請自己第一志願的大學。現在請設計一程式可讀取使用者的在校成績,若使用者可以申請第一志願的大學就輸出「YES」,否則輸出「NO」。 Exercise 1: College Admission If you got GPA higher than 4.0 in transcript then you can apply for  your first choice. Now write a C program read GPA from the keyboard and print "YES" if you can apply otherwise print "NO". 練習一參考解法: Exercise 1 solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> int main () { float gpa; printf( "Please enter your GPA:" ); scanf( "%f" , & gpa); if (gpa < 4.0 ) printf( "NO \n " ); else printf( "YES \n " ); return 0 ; } 練習二:APCS成績級分 撰寫一個可輸入APCS成績的C語言程式,此程式會依據下表輸出對應的級分。 級分 分數範圍 五 90~100 四 70~89 三 50~69 二 30~49 一 ...