My sample input is giving correct answer but on submitting all test cases are giving run error. Can you please help me with this.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
int[][] cor=new int[2][n];
int max=0;
for (int i=0; i<n; i++) {
cor[0][i]=sc.nextInt();
max=Math.max(max,cor[0][i]);
cor[1][i]=sc.nextInt();
max=Math.max(max,cor[1][i]);
}
char[][] board=new char[max][max];
for (int i=0; i<max; i++) {
for (int j=0; j<max; j++) {
board[i][j]='-';
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<max; j++) {
board[cor[0][i]-1][j]='X';
}
for (int j=0; j<max; j++) {
board[j][cor[1][i]-1]='X';
}
}
// for (int i=0; i<max; i++) {
// for (int j=0; j<max; j++) {
// System.out.print (board[i][j]+" ");
// }
// System.out.println ();
// }
System.out.println (maxArea (board, 0, 0, new boolean[max][max]));
}
static int maxArea (char[][] board, int i, int j, boolean[][] visited) {
if (board[i][j]=='X') {
int a=0, b=0, c=0;
if (i+1<board.length)
a=maxArea (board, i+1, j, visited);
if (j+1<board.length)
b=maxArea (board, i, j+1, visited);
if (i+1<board.length && j+1<board.length)
c=maxArea (board, i+1, j+1, visited);
return Math.max (a, Math.max (b,c));
}
int x=0, y=0, z=0, w=0;
if (isSafe (board, i+1, j, visited)) {
visited[i+1][j]=true;
x= 1 + maxArea (board, i+1, j, visited);
visited[i+1][j]=false;
}
if (isSafe (board, i-1, j, visited)) {
visited[i-1][j]=true;
y= 1 + maxArea (board, i-1, j, visited);
visited[i-1][j]=false;
}
if (isSafe (board, i, j+1, visited)) {
visited[i][j+1]=true;
z= 1 + maxArea (board, i, j+1, visited);
visited[i][j+1]=false;
}
if (isSafe (board, i, j-1, visited)) {
visited[i][j-1]=true;
w= 1 + maxArea (board, i, j-1, visited);
visited[i][j-1]=false;
}
return Math.max (Math.max(x,y), Math.max(z,w));
}
static boolean isSafe (char[][] board, int i, int j, boolean[][] visited){
if (i<0 || j<0 || i==board.length ||j==board.length || visited[i][j] || board[i][j]=='X') {
return false;
}
return true;
}
}