回答編集履歴
2
修正
test
CHANGED
@@ -100,9 +100,9 @@
|
|
100
100
|
|
101
101
|
Node currNode = null;
|
102
102
|
|
103
|
-
for (int y = 0; y <
|
103
|
+
for (int y = 0; y < h; y++) {
|
104
104
|
|
105
|
-
for (int x = 0; x <
|
105
|
+
for (int x = 0; x < w; x++) {
|
106
106
|
|
107
107
|
Node node = nodes [y, x];
|
108
108
|
|
1
サンプルを追加
test
CHANGED
@@ -1 +1,187 @@
|
|
1
1
|
A*より簡単な[ダイクストラ法](http://www.deqnotes.net/acmicpc/dijkstra/)がおすすめです。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
サンプルを書いてみました。
|
10
|
+
|
11
|
+
ご参考ください。
|
12
|
+
|
13
|
+
```cshapr
|
14
|
+
|
15
|
+
using System.Collections;
|
16
|
+
|
17
|
+
using System.Collections.Generic;
|
18
|
+
|
19
|
+
using UnityEngine;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
public class DijkstraSample : MonoBehaviour {
|
24
|
+
|
25
|
+
class Node {
|
26
|
+
|
27
|
+
public int x;
|
28
|
+
|
29
|
+
public int y;
|
30
|
+
|
31
|
+
public bool wall;
|
32
|
+
|
33
|
+
public int cost;
|
34
|
+
|
35
|
+
public bool done;
|
36
|
+
|
37
|
+
public Node prev;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
public Node(int x, int y, bool wall) {
|
42
|
+
|
43
|
+
this.x = x;
|
44
|
+
|
45
|
+
this.y = y;
|
46
|
+
|
47
|
+
this.wall = wall;
|
48
|
+
|
49
|
+
cost = -1;
|
50
|
+
|
51
|
+
done = false;
|
52
|
+
|
53
|
+
prev = null;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
void Start() {
|
62
|
+
|
63
|
+
int w = 5;
|
64
|
+
|
65
|
+
int h = 5;
|
66
|
+
|
67
|
+
int[,] map = new int[,] {
|
68
|
+
|
69
|
+
{ 0, 1, 0, 0, 0 },
|
70
|
+
|
71
|
+
{ 0, 0, 0, 1, 0 },
|
72
|
+
|
73
|
+
{ 0, 1, 0, 1, 0 },
|
74
|
+
|
75
|
+
{ 0, 1, 0, 1, 0 },
|
76
|
+
|
77
|
+
{ 0, 0, 0, 1, 0 }
|
78
|
+
|
79
|
+
};
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
Node[,] nodes = new Node[h, w];
|
84
|
+
|
85
|
+
for (int y = 0; y < h; y++)
|
86
|
+
|
87
|
+
for (int x = 0; x < w; x++)
|
88
|
+
|
89
|
+
nodes [y, x] = new Node (x, y, map [y, x] == 1);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
int sx = 0; // 現在地x。
|
94
|
+
|
95
|
+
int sy = 0; // 現在地y。
|
96
|
+
|
97
|
+
nodes [sy, sx].cost = 0;
|
98
|
+
|
99
|
+
while (true) {
|
100
|
+
|
101
|
+
Node currNode = null;
|
102
|
+
|
103
|
+
for (int y = 0; y < 5; y++) {
|
104
|
+
|
105
|
+
for (int x = 0; x < 5; x++) {
|
106
|
+
|
107
|
+
Node node = nodes [y, x];
|
108
|
+
|
109
|
+
if (node.done || node.cost == -1)
|
110
|
+
|
111
|
+
continue;
|
112
|
+
|
113
|
+
if (currNode == null || node.cost < currNode.cost)
|
114
|
+
|
115
|
+
currNode = node;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
if (currNode == null)
|
122
|
+
|
123
|
+
break;
|
124
|
+
|
125
|
+
currNode.done = true;
|
126
|
+
|
127
|
+
for (int dy = -1; dy <= 1; dy++) {
|
128
|
+
|
129
|
+
for (int dx = -1; dx <= 1; dx++) {
|
130
|
+
|
131
|
+
if (dx == 0 && dy == 0)
|
132
|
+
|
133
|
+
continue;
|
134
|
+
|
135
|
+
int x = currNode.x + dx;
|
136
|
+
|
137
|
+
int y = currNode.y + dy;
|
138
|
+
|
139
|
+
if (x < 0 || y < 0 || x >= w || y >= h)
|
140
|
+
|
141
|
+
continue;
|
142
|
+
|
143
|
+
Node node = nodes [y, x];
|
144
|
+
|
145
|
+
if (node.wall)
|
146
|
+
|
147
|
+
continue;
|
148
|
+
|
149
|
+
int newCost = currNode.cost + 1;
|
150
|
+
|
151
|
+
if (node.cost == -1 || newCost < node.cost) {
|
152
|
+
|
153
|
+
node.cost = newCost;
|
154
|
+
|
155
|
+
node.prev = currNode;
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
int gx = 4; // 目的地x。
|
168
|
+
|
169
|
+
int gy = 4; // 目的地y。
|
170
|
+
|
171
|
+
Node tempNode = nodes [gy, gx];
|
172
|
+
|
173
|
+
while (tempNode != null) {
|
174
|
+
|
175
|
+
Debug.Log (string.Format ("x:{0} y:{1}", tempNode.x, tempNode.y));
|
176
|
+
|
177
|
+
tempNode = tempNode.prev;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
```
|