回答編集履歴
2
修正
answer
CHANGED
@@ -49,8 +49,8 @@
|
|
49
49
|
nodes [sy, sx].cost = 0;
|
50
50
|
while (true) {
|
51
51
|
Node currNode = null;
|
52
|
-
for (int y = 0; y <
|
52
|
+
for (int y = 0; y < h; y++) {
|
53
|
-
for (int x = 0; x <
|
53
|
+
for (int x = 0; x < w; x++) {
|
54
54
|
Node node = nodes [y, x];
|
55
55
|
if (node.done || node.cost == -1)
|
56
56
|
continue;
|
1
サンプルを追加
answer
CHANGED
@@ -1,1 +1,94 @@
|
|
1
|
-
A*より簡単な[ダイクストラ法](http://www.deqnotes.net/acmicpc/dijkstra/)がおすすめです。
|
1
|
+
A*より簡単な[ダイクストラ法](http://www.deqnotes.net/acmicpc/dijkstra/)がおすすめです。
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
サンプルを書いてみました。
|
6
|
+
ご参考ください。
|
7
|
+
```cshapr
|
8
|
+
using System.Collections;
|
9
|
+
using System.Collections.Generic;
|
10
|
+
using UnityEngine;
|
11
|
+
|
12
|
+
public class DijkstraSample : MonoBehaviour {
|
13
|
+
class Node {
|
14
|
+
public int x;
|
15
|
+
public int y;
|
16
|
+
public bool wall;
|
17
|
+
public int cost;
|
18
|
+
public bool done;
|
19
|
+
public Node prev;
|
20
|
+
|
21
|
+
public Node(int x, int y, bool wall) {
|
22
|
+
this.x = x;
|
23
|
+
this.y = y;
|
24
|
+
this.wall = wall;
|
25
|
+
cost = -1;
|
26
|
+
done = false;
|
27
|
+
prev = null;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
void Start() {
|
32
|
+
int w = 5;
|
33
|
+
int h = 5;
|
34
|
+
int[,] map = new int[,] {
|
35
|
+
{ 0, 1, 0, 0, 0 },
|
36
|
+
{ 0, 0, 0, 1, 0 },
|
37
|
+
{ 0, 1, 0, 1, 0 },
|
38
|
+
{ 0, 1, 0, 1, 0 },
|
39
|
+
{ 0, 0, 0, 1, 0 }
|
40
|
+
};
|
41
|
+
|
42
|
+
Node[,] nodes = new Node[h, w];
|
43
|
+
for (int y = 0; y < h; y++)
|
44
|
+
for (int x = 0; x < w; x++)
|
45
|
+
nodes [y, x] = new Node (x, y, map [y, x] == 1);
|
46
|
+
|
47
|
+
int sx = 0; // 現在地x。
|
48
|
+
int sy = 0; // 現在地y。
|
49
|
+
nodes [sy, sx].cost = 0;
|
50
|
+
while (true) {
|
51
|
+
Node currNode = null;
|
52
|
+
for (int y = 0; y < 5; y++) {
|
53
|
+
for (int x = 0; x < 5; x++) {
|
54
|
+
Node node = nodes [y, x];
|
55
|
+
if (node.done || node.cost == -1)
|
56
|
+
continue;
|
57
|
+
if (currNode == null || node.cost < currNode.cost)
|
58
|
+
currNode = node;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
if (currNode == null)
|
62
|
+
break;
|
63
|
+
currNode.done = true;
|
64
|
+
for (int dy = -1; dy <= 1; dy++) {
|
65
|
+
for (int dx = -1; dx <= 1; dx++) {
|
66
|
+
if (dx == 0 && dy == 0)
|
67
|
+
continue;
|
68
|
+
int x = currNode.x + dx;
|
69
|
+
int y = currNode.y + dy;
|
70
|
+
if (x < 0 || y < 0 || x >= w || y >= h)
|
71
|
+
continue;
|
72
|
+
Node node = nodes [y, x];
|
73
|
+
if (node.wall)
|
74
|
+
continue;
|
75
|
+
int newCost = currNode.cost + 1;
|
76
|
+
if (node.cost == -1 || newCost < node.cost) {
|
77
|
+
node.cost = newCost;
|
78
|
+
node.prev = currNode;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
int gx = 4; // 目的地x。
|
85
|
+
int gy = 4; // 目的地y。
|
86
|
+
Node tempNode = nodes [gy, gx];
|
87
|
+
while (tempNode != null) {
|
88
|
+
Debug.Log (string.Format ("x:{0} y:{1}", tempNode.x, tempNode.y));
|
89
|
+
tempNode = tempNode.prev;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
```
|