-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABCPATH.cs
141 lines (138 loc) · 4.57 KB
/
ABCPATH.cs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPOJSolutions
{
class ABCPATH
{
static int[,] plot;
static void Main()
{
Run();
Console.ReadLine();
}
static Dictionary<string, int> dp;
static void Run()
{
int counter = 0;
while (true)
{
counter++;
string[] str = Console.ReadLine().Split();
int H = int.Parse(str[0]);
int W = int.Parse(str[1]);
if (H == 0 && W == 0)
break;
plot = new int[H, W];
dp = new Dictionary<string, int>();
for (int i = 0; i < H; i++)
{
string entry = Console.ReadLine();
for (int j = 0; j < W; j++)
{
plot[i, j] = (int)entry[j]; // A : 65 , Z: 90
}
}
int overallBest = 0;
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (plot[i, j] == 65)
{
int mybest = DFS(i, j, H, W, 65);
if (mybest > overallBest)
overallBest = mybest;
}
}
}
Console.WriteLine(string.Format("Case {0}: {1}", counter, overallBest));
}
}
static int DFS(int i, int j, int H, int W, int currentNumber)
{
plot[i, j] = -1;//Visited
string key = string.Format("{0}-{1}", i, j);
if (dp.ContainsKey(key))
return dp[key];
int localBest = 1;
if (j - 1 >= 0)
{
//side one
if (plot[i, j - 1] == currentNumber + 1)
{
int max = DFS(i, j - 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
if (i - 1 >= 0) // diagonal
{
if (plot[i - 1, j - 1] == currentNumber + 1)
{
int max = DFS(i - 1, j - 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
if (i + 1 < H)
{
if (plot[i + 1, j - 1] == currentNumber + 1)
{
int max = DFS(i + 1, j - 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
}
if (j + 1 < W)
{
//side one
if (plot[i, j + 1] == currentNumber + 1)
{
int max = DFS(i, j + 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
if (i - 1 >= 0) // diagonal
{
if (plot[i - 1, j + 1] == currentNumber + 1)
{
int max = DFS(i - 1, j + 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
if (i + 1 < H)
{
if (plot[i + 1, j + 1] == currentNumber + 1)
{
int max = DFS(i + 1, j + 1, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
}
if (i - 1 >= 0)
{
if (plot[i - 1, j] == currentNumber + 1)
{
int max = DFS(i - 1, j, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
if (i + 1 < H)
{
if (plot[i + 1, j] == currentNumber + 1)
{
int max = DFS(i + 1, j, H, W, currentNumber + 1) + 1;
if (max > localBest)
localBest = max;
}
}
dp.Add(key, localBest);
return localBest;
}
}
}