求助各位大佬:在本地IDE一直是栈溢出错误,牛客不知道为啥还通过了10%
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int s = in.nextInt();
int m = in.nextInt();
int n = in.nextInt();
int[][] arr=new int[m][n];
for(int i=0;i<m;++i) {
for(int j=0;j<n;++j) {
arr[i][j]=in.nextInt();
}
}
boolean res=demo(arr,s,0,0);
if(res) {
System.out.println(1);
}else {
System.out.println(0);
}
}
}
public static boolean demo(int[][] arr,int s,int i,int j) {
if(i<0||j<0||i>=arr.length||j>=arr[0].length||arr[i][j]==0) return false;
if((i==arr.length-1)&&(j==arr[0].length-1)) return true;
boolean ans=demo(arr,s,i-s,j)||demo(arr,s,i+s,j)||demo(arr,s,i,j-s)||demo(arr,s,i,j+s);
return ans;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int s = in.nextInt();
int m = in.nextInt();
int n = in.nextInt();
int[][] arr=new int[m][n];
for(int i=0;i<m;++i) {
for(int j=0;j<n;++j) {
arr[i][j]=in.nextInt();
}
}
boolean res=demo(arr,s,0,0);
if(res) {
System.out.println(1);
}else {
System.out.println(0);
}
}
}
public static boolean demo(int[][] arr,int s,int i,int j) {
if(i<0||j<0||i>=arr.length||j>=arr[0].length||arr[i][j]==0) return false;
if((i==arr.length-1)&&(j==arr[0].length-1)) return true;
boolean ans=demo(arr,s,i-s,j)||demo(arr,s,i+s,j)||demo(arr,s,i,j-s)||demo(arr,s,i,j+s);
return ans;
}
}
全部评论
(5) 回帖