我正在为家庭作业做这个问题。我已经使用标准的自下而上动态编程算法解决了这个问题。我的代码显示了我的测试用例的预期结果,但网站说它给出了错误的答案。我无法理解缺少此代码的位置。请帮我。
import java.io.*;
import java.util.*;
class Main300{
public static void main (String[] args) throws java.lang.Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int nn = Integer.parseInt(br.readLine());
for(int j = 0 ; j < nn; j++){
int n = Integer.parseInt(br.readLine());
char[][] a = new char[n][n];
int ki = -1;
int kj = -1;
for(int i = 0 ; i < n ; i++){
String s = br.readLine();
for(int k = 0 ; k < n; k++){
a[i][k] = s.charAt(k);
if(a[i][k] == 'K'){
ki = i;
kj = k;
}
}
}
System.out.println(ans(a, ki, kj));
}
}
private static int ans(char[][] a, int ki, int kj){
int[][] x = new int[a.length][a.length];
for(int j = a.length-1; j >= 0; j--){
for(int i = 0 ; i < a.length; i++){
if(a[i][j] == 'P'){
x[i][j]++;
}
if(i-2 >= 0 && j+1 <= a.length-1 && a[i-2][j+1] == 'P'){
x[i][j] += x[i-2][j+1];
}else if(i-1 >= 0 && j+2 <= a.length-1 && a[i-1][j+2] == 'P'){
x[i][j] += x[i-1][j+2];
}else if(i+2 <= a.length-1 && j+1 <= a.length-1 && a[i+2][j+1] == 'P'){
x[i][j] += x[i+2][j+1];
}else if(i+1 <= a.length-1 && j+2 <= a.length-1 && a[i+1][j+2] == 'P'){
x[i][j] += x[i+1][j+2];
}
}
}
return x[ki][kj];
}
}