题目描述
blablabla
样例
import java.util.*;
class Main {
public static int n,k,res;
public static int dx[] = {0,+1,0,-1};//遍历的方向(右下左上)
public static int dy[] = {+1,0,-1,0};
public static String brr[] = new String[25]; //读取数据
public static int isth[] = new int[30]; //判断字母是否走过
public static char arr[][] = new char[25][25];
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for (int i = 0; i < n; i++) brr[i] = sc.next();
for (int i = 0; i < n; i++) arr[i] = brr[i].toCharArray();
isth[arr[0][0] - 64] = 1;
dfs(0,0,1);
System.out.println(res);
}
public static void dfs (int x,int y,int step) {
for (int i = 0; i < 4; i++) {
int a = x + dx[i];
int b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < k && isth[arr[a][b] - 64] != 1 ) {
isth[arr[a][b] - 64]++;
dfs(a,b,step+1);
isth[arr[a][b] - 64]--;
}
}
res = getMax(res,step);
}
public static int getMax(int x, int y) {
return x > y ? x:y;
}
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla