I've just started to learn Java,and I've got some questions for my code. Well,the task is to get a number and print a matrix like this:
get 3,and print:
1 2 3
8 9 4
7 6 5
get 4,and print:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Because I'm learning algorithm,so I deal the question by DFS, here's my cpp code,which can solve the task.
#include "iostream"
using namespace std;
int n;
int matrix[100][100]={0};
int visited[100][100]={0};
void dfs(int n,int x,int y,int i,int dire)
{
//x,y is the coordinate,i is the number to write
//dire is the variety to control the direction of the dfs
if(i==n*n+1)return;
switch(dire)
{
case(1)://write the number on right
{
if(visited[x][y+1]==0&&y+1<n)
{
visited[x][y+1]=1;
matrix[x][y+1]=i;
i++;
dfs(n,x,y+1,i,1);
}
else dfs(n,x,y,i,2);
break;
}
case(2)://down
{
if(visited[x+1][y]==0&&x+1<n)
{
visited[x+1][y]=1;
matrix[x+1][y]=i;
i++;
dfs(n,x+1,y,i,2);
}
else dfs(n,x,y,i,3);
break;
}
case(3)://left
{
if(visited[x][y-1]==0&&y-1>=0)
{
visited[x][y-1]=1;
matrix[x][y-1]=i;
i++;
dfs(n,x,y-1,i,3);
}
else dfs(n,x,y,i,4);
break;
}
case(4)://up
{
if(visited[x-1][y]==0&&x-1>=0)
{
visited[x-1][y]=1;
matrix[x-1][y]=i;
i++;
dfs(n,x-1,y,i,4);
}
else dfs(n,x,y,i,1);
break;
}
}
}
void output(int n)
{
int p,q=0;
for(q=0;q<n;q++)
{
for(p=0;p<n;p++)
{
cout<<matrix[q][p]<<" ";
if(matrix[q][p]<10)
cout<<" ";
if(matrix[q][p]<100)
cout<<" ";
}
cout<<endl;
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]=0;
visited[i][j]=0;
}
}
///dfs_r(n,0,-1,1);
dfs(n,0,-1,1,1);
output(n);
}
But things goes wrong when I translate it into Java
import java.util.Scanner;
public class mainclass{
public static class method{
int n;
int visited[][]=new int[n][n];
int matrix[][]=new int[n][n];
method(int temp){
n=temp;
}
void dfs(int x,int y,int i,int dire)
{
if(i==n*n+1)return;
switch(dire)
{
case(1):
{
if(visited[x][y+1]==0&&y+1<n)
{
visited[x][y+1]=1;
matrix[x][y+1]=i;
i++;
dfs(x,y+1,i,1);
}
else dfs(x,y,i,2);
break;
}
case(2):
{
if(visited[x+1][y]==0&&x+1<n)
{
visited[x+1][y]=1;
matrix[x+1][y]=i;
i++;
dfs(x+1,y,i,2);
}
else dfs(x,y,i,3);
break;
}
case(3):
{
if(visited[x][y-1]==0&&y-1>=0)
{
visited[x][y-1]=1;
matrix[x][y-1]=i;
i++;
dfs(x,y-1,i,3);
}
else dfs(x,y,i,4);
break;
}
case(4):
{
if(visited[x-1][y]==0&&x-1>=0)
{
visited[x-1][y]=1;
matrix[x-1][y]=i;
i++;
dfs(x-1,y,i,4);
}
else dfs(x,y,i,1);
break;
}
}
}
void output()
{
int p,q=0;
for(q=0;q<n;q++)
{
for(p=0;p<n;p++)
{
System.out.print(matrix[q][p]);
if(matrix[q][p]<10)
System.out.print(" ");
if(matrix[q][p]<100)
System.out.print(" ");
}
System.out.println();
}
}
}
public static void main(String args[]){
Scanner reader=new Scanner(System.in);
int n=reader.nextInt();
method c=new method(n);
c.dfs(0,-1,1,1);
c.output();
}
}
I think my algorithm is all right,but I can't work it out why my Java code crashes. I think I should learn more about basic grammar of Java.
edit: I use dev-cpp to run my cpp code and eclipse to run my Java code. I'm not really know how to describe how does the crash of my Eclipse got, the code is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at mainclass$method.dfs(mainclass.java:17)
at mainclass.main(mainclass.java:87)
<terminated, exit value: 1>C:\Program Files\Java\jre1.8.0_101\bin\javaw.exe (2016年9月27日 上午9:51:32)
if(visited[x][y+1]==0&&y+1<n)toif(y+1<n && visited[x][y+1]==0)array[2](effectively third item, including 0 index), but your array only has two items, indexed 0 and 1. The easiest thing would be to use some modern IDE, place a breakpoint on the line where you get the exception and debug it step by step to check what happens.