{"id":28906,"student_id":10,"content":"var end = grid[31][23];\nvar s = grid[0][0];\ns.num = 0;\nvar expoler = [s];\n\nforever(function () {\n \n if (expoler.length == 0) return;\n \n expoler.sort((a, b) =\u003e {\n return s.num + distanceAtoB(a, end) - b.num - distanceAtoB(b, end);\n });\n \n var e = expoler.pop();\n e.color = 'lightblue';\n for (var x = 0; x\u003ce.connects.length; x++) {\n var d = e.connects[x];\n if (e.num + 1 \u003c d.num) {\n d.num = e.num + 1;\n d.color = 'pink';\n d.from = e;\n expoler.push(d);\n }\n }\n \n if (expoler.indexOf(end) != -1) {\n var back = end.from;\n while(back.from != undefined) {\n back.color = 'red';\n back = back.from;\n }\n back.color = 'red';\n expoler = [];\n }\n\n});\n\nfunction distanceAtoB(a, b) {\n var x = a.x - b.x;\n var y = a.y - b.y;\n return Math.floor(Math.sqrt(x*x + y*y)/W);\n}","created_at":"2018-05-16T00:18:39.802+08:00","updated_at":"2019-11-02T04:10:11.078+08:00","name":"A*","language":"javascript","screenshot":{"url":"https://cdn3.koding.school/uploads/project/screenshot/28906/31afe3eb5aeb9ee4a70938429513aba6.jpg"},"parent_id":28905,"plugin":"var W = 20;\nvar grid = [];\n\nfor (var x = 0; x \u003c 640/W; x++) {\n grid[x] = [];\n for (var y = 0; y \u003c 480/W; y++) {\n grid[x][y] = {\n color: Math.random() \u003c 0.4 ? 'black': 'white',\n num: Infinity,\n connects: [],\n x: x,\n y: y,\n }\n }\n}\n\ngrid[31][23].color = 'white';\ngrid[0][0].color = 'white';\n\nfor (var x = 0; x \u003c 640/W; x++) {\n for (var y = 0; y \u003c 480/W; y++) {\n for (var a = -1; a \u003c 2; a++) {\n for (var b = -1; b \u003c 2; b++) {\n if (a == 0 \u0026\u0026 b == 0) continue;\n if (x + a \u003c 0 || x + a \u003e= 32) continue;\n if (y + b \u003c 0 || y + b \u003e= 23) continue;\n if (grid[x + a][y + b].color == 'white') {\n grid[x][y].connects.push(grid[x + a][y + b]);\n }\n }\n }\n if (x - 1 \u003e= 0 \u0026\u0026 grid[x - 1][y].color == 'white')\n \n if (y - 1 \u003e= 0 \u0026\u0026 grid[x][y - 1].color == 'white')\n grid[x][y].connects.push(grid[x][y - 1]);\n if (x + 1 \u003c 640/W \u0026\u0026 grid[x + 1][y].color == 'white')\n grid[x][y].connects.push(grid[x + 1][y]);\n if (y + 1 \u003c 480/W \u0026\u0026 grid[x][y + 1].color == 'white')\n grid[x][y].connects.push(grid[x][y + 1]);\n }\n}\n\nforever(function() {\n for (var x = 0; x \u003c grid.length; x++) {\n for (var y = 0; y \u003c grid[x].length; y++) {\n pen.fillColor = grid[x][y].color;\n pen.drawRect(x*W, y*W, W, W);\n }\n }\n});","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":329,"hashid":"kdms8j4d","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦