回答編集履歴
4
修正
answer
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
もともとのプログラムは、いって、もどって、いってを繰り返し、値が求まらない。
|
2
|
+
いわゆる無限ループになっている。
|
2
3
|
調べたところは戻らない。一方向にするというようにするために、
|
3
4
|
たどっていった履歴を残すことで、数え上げる。最後は、たどった履歴を数える。
|
4
5
|
C#7.0のローカル関数も使っています。
|
@@ -8,7 +9,6 @@
|
|
8
9
|
//あらかじめ、白黒のデータをDicに突っ込んでおく。GetPixelは確か遅い。
|
9
10
|
Dictionary<(int, int), bool> imageData = new Dictionary<(int, int), bool>();
|
10
11
|
|
11
|
-
Dictionary<(int, int), bool> imageData = new Dictionary<(int, int), bool>();
|
12
12
|
public int 連続を求める(int p_x,int p_y)
|
13
13
|
{
|
14
14
|
HashSet<(int, int)> historyList = new HashSet<(int, int)>();
|
3
リファクタ
answer
CHANGED
@@ -8,11 +8,23 @@
|
|
8
8
|
//あらかじめ、白黒のデータをDicに突っ込んでおく。GetPixelは確か遅い。
|
9
9
|
Dictionary<(int, int), bool> imageData = new Dictionary<(int, int), bool>();
|
10
10
|
|
11
|
+
Dictionary<(int, int), bool> imageData = new Dictionary<(int, int), bool>();
|
11
|
-
|
12
|
+
public int 連続を求める(int p_x,int p_y)
|
12
13
|
{
|
13
14
|
HashSet<(int, int)> historyList = new HashSet<(int, int)>();
|
14
15
|
Stack<(int, int)> stack = new Stack<(int, int)>();
|
15
16
|
|
17
|
+
IEnumerable<(int,int)> 周辺の座標(int x,int y)
|
18
|
+
{
|
19
|
+
for (int i = -1; i <= 1; i++)
|
20
|
+
{
|
21
|
+
for (int l = -1; l <= 1; l++)
|
22
|
+
{
|
23
|
+
yield return (x + i, y + l);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
16
28
|
void Check(int x,int y)
|
17
29
|
{
|
18
30
|
if (historyList.Contains((x, y)) == false && imageData.ContainsKey((x, y)))
|
@@ -20,17 +32,17 @@
|
|
20
32
|
if (imageData[(x, y)])
|
21
33
|
{
|
22
34
|
historyList.Add((x, y));
|
23
|
-
|
35
|
+
foreach (var item in 周辺の座標( x, y))
|
24
36
|
{
|
25
|
-
|
37
|
+
if (historyList.Contains(item) == false)
|
26
38
|
{
|
27
|
-
|
39
|
+
stack.Push(item);
|
28
40
|
}
|
29
|
-
}
|
41
|
+
}
|
30
42
|
}
|
31
43
|
}
|
32
44
|
}
|
33
|
-
|
45
|
+
//開始地点の入力
|
34
46
|
stack.Push((p_x, p_y));
|
35
47
|
while(stack.Count>0)
|
36
48
|
{
|
@@ -40,5 +52,3 @@
|
|
40
52
|
return historyList.Count;
|
41
53
|
}
|
42
54
|
```
|
43
|
-
|
44
|
-
ソース読み直したら、わざわざローカル関数を使わなくてもよかった・・・まぁいいか。
|
2
修正
answer
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
もともとのプログラムは、いって、もどって、いってを繰り返し、値が求まらない。
|
2
|
+
調べたところは戻らない。一方向にするというようにするために、
|
1
3
|
たどっていった履歴を残すことで、数え上げる。最後は、たどった履歴を数える。
|
2
4
|
C#7.0のローカル関数も使っています。
|
3
5
|
デバッグしていないので、バグあるかも。
|
1
修正
answer
CHANGED
@@ -28,6 +28,7 @@
|
|
28
28
|
}
|
29
29
|
}
|
30
30
|
}
|
31
|
+
//初期値をいれる
|
31
32
|
stack.Push((p_x, p_y));
|
32
33
|
while(stack.Count>0)
|
33
34
|
{
|
@@ -36,4 +37,6 @@
|
|
36
37
|
}
|
37
38
|
return historyList.Count;
|
38
39
|
}
|
39
|
-
```
|
40
|
+
```
|
41
|
+
|
42
|
+
ソース読み直したら、わざわざローカル関数を使わなくてもよかった・・・まぁいいか。
|