ABCPATH problem
I have been trying to solve problem ABCPATH but repeatedly getting WA. I havent been able to find out the bug in my code… help would be really appreciated.
here is my code
thanks in advance smile
import java.util.;
import java.lang.;
class Main
{
static boolean vis[][];
static int dfs(char ch,char a[][],int i,int j)
{
vis[i][j]=true;
int max=0;
for(int k=-1;k<=1;k++)
{
for(int l=-1;l<=1;l++)
{
if(!vis[i+k][j+l])
{
if(ch+1==a[i+k][j+l])
{
int c=dfs((char)(ch+1),a,i+k,j+l);
if(c>max)
max=c;
}
}
}
}
return max+1;
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
char a[][]=new char[52][52];
int h,w,C=1;
h=sc.nextInt();
w=sc.nextInt();
StringBuilder ans=new StringBuilder();
do
{
vis=new boolean[52][52];
int max=0;
for(int i=1;i<=h;i++)
{
String s=sc.next();
for(int j=1;j<=w;j++)
{
a[i][j]=s.charAt(j-1);
}
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
if(a[i][j]==‘A’)
{
int val=dfs(‘A’,a,i,j);
if(val>max)
{
max=val;
}
}
}
}
h=sc.nextInt();
w=sc.nextInt();
ans.append(“Case “).append(C++).append(”: “).append(max).append(”\n”);
}while(h!=0&&w!=0);
System.out.println(ans);
}
}