//Here is the code:
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.io.*;
public class ABCPATH {
public static int computeLongest(char[][] connect, int i, int j, int[][] dp) {
if (dp[i][j] != 0) {
return dp[i][j];
}
int max = 1;
char c = connect[i][j];
c++;
int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
int dx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
for (int k = 0; k < dx.length; k++) {
int inew = i + dy[k];
int jnew = j + dx[k];
if (inew >= 0 && inew < connect.length && jnew >= 0 && jnew < connect[0].length) {
if (connect[inew][jnew] == c) {
return dp[i][j] = Math.max(max, 1 + computeLongest(connect, inew, jnew, dp));
}
}
}
return max;
}
public static void main(String[] args) throws IOException {
//Scanner s = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
int k = 1;
int p = 1;
while (k != -1) {
String in[] = sc.nextLine().split(" ");
if (Integer.parseInt(in[0]) != 0 && Integer.parseInt(in[1]) != 0) {
int r = Integer.parseInt(in[0]);
int c = Integer.parseInt(in[1]);
char[][] connect = new char[r][c];
for (int i = 0; i < r; i++) {
String in2 = sc.nextLine();
for (int j = 0; j < in2.length(); j++) {
connect[i][j] = in2.charAt(j);
}
}
int max = 0;
int dp[][] = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (connect[i][j] == 'A') {
max = Math.max(max, computeLongest(connect, i, j, dp));
}
}
}
System.out.println("Case " + p + ": " + max);
p++;
}
else {
k = -1;
}
}
}
}
這個問題的鏈接是:http://www.spoj.com/problems/ABCPATH/,即使在測試1把它給一個錯誤的答案。但是我使用的算法是正確的,因爲我已經從一些人 驗證了成功的提交。從SPOJ獲取上SPOJ ABCPATH一個錯誤的答案很長一段時間
始終的問題給出了問題的描述。到SPOJ的網址是不夠的。 – xenteros