3차원 미로문제 질문 할게요 !!

3차원 미로문제 입니다^^
레포트라서 짜고 있었습니다만.. 많이 어렵네요;;
소스가 다소 복잡해보이더라도...이해해주세요..
아직 초보라서 말입니다..

import java.util.*;
class stackmaze{//미로탐색경로
int row,col,hei,dir;
}
class move{//6방향의 좌표를 가질 이동클래스
int vert;//세로
int horiz;//가로
int jump;//높이
}
public class maze {
private static int max_stack_size=100;//스택의 최대 길이
private static int row_size=11;//미로의 최대 행 길이
private static int col_size=15;//미로의 최대 열 길이
private static int height_size=2;//미로의 최대 높이
static stackmaze stack[];//경로 저장
static move move[];//이동 좌표 저장

// 탐색한 경로를 표시할 배열
static int [][][]mark;//mark는 [높이+2][행+2][열+2]
static // 미로를 구성하는 배열 (바깥테두리는 모두 1로 표시)
int [][][]maze= {//maze는 [높이+2][행+2][열+2]
{//높이가 +2 를 더 해주었기 때문에 가장 위와 가장 아래는 전부 1 로 채움
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
},
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1},
{1,1,1,1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,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
}
};
static int top=-1;//스택의 top변수
// 미로의 시작점은 1,1,1이고 끝나는 점을 가리키는 변수
public static int exit_row = row_size;
public static int exit_col = col_size;
public static int exit_hei = height_size;

public static void path(){
int row=0, col=0, hei=0, next_row, next_col, next_hei, dir;
boolean found=false;
stackmaze stack2;

mark[1][1][1]=1;// 처음 시작지점
top=0; // 스택에 처음 시작위치를 입력.
stack[0].row=1;
stack[0].col=1;
stack[0].hei=1;
stack[0].dir=1;
while(top-1&&!found)
{
stack2 = pop(top);
row=stack2.row;
col=stack2.col;
hei=stack2.hei;
dir=stack2.dir;
while(dir6&&!found)
{
next_row = row + move[dir].vert;// 현 좌표에서 이동할수 있는 위치를 계산
next_col = col + move[dir].horiz;
next_hei = hei + move[dir].jump;

if(next_row==exit_row&&next_col==exit_col&&next_hei==exit_hei)
found=true;// 만약 그곳이 출구라면 도착 표시
else if(maze[next_hei][next_row][next_col]==1&&mark[next_hei][next_row][next_col]==1)
{
mark[next_hei][next_row][next_col] = 1;
stack2.row = row;
stack2.col = col;
stack2.hei = hei;
stack2.dir = ++dir;
push(top, stack2);
row = next_row;
col = next_col;
hei = next_hei;
dir = 0;
}
else
++dir;
}
}

if(found==true)
{
system.out.println(the path is:);
system.out.println( row col hei);

for(int i=0 ; i = top ; i++)
{
system.out.printf( %3d,%3d,%3d , stack[i].hei, stack[i].row, stack[i].col);
if((i+1)%5 == 0)
system.out.println();
}
system.out.printf( %3d,%3d,%3d , hei, row, col);
system.out.println();
system.out.printf( %3d,%3d,%3d\n, exit_hei, exit_row, exit_col);
}
else
system.out.println( the maze does not have a path\n);// 못찾았으면 에러표시
}
public static void push(int top,stackmaze stackmaze){
if(top = max_stack_size-1)
{
stack_full();
return;
}
stack[++top] = stackmaze;
}
public static stackmaze pop(int top){
if(top == -1)
return stack_empty();
return stack[top--];
}
public static void stack_full(){
system.out.println(stack is full !!);
}
public static stackmaze stack_empty(){
stackmaze stack=new stackmaze();
stack.row=-1;
stack.col=-1;
stack.hei=-1;
stack.dir=-1;
system.out.println(stack is empty !! );
return stack;
}
public static void main(string[] args) {
//6가지의 좌표 변동값 저장
move[0].vert = -1;move[0].horiz = 0;move[0].jump = 0;//북
move[1].vert = 0;move[1].horiz = 1;move[1].jump = 0;//동
move[2].vert = 1;move[2].horiz = 0;move[2].jump = 0;//남
move[3].vert = 0;move[3].horiz = -1;move[3].jump = 0;//서
move[4].vert = 0;move[4].horiz = 0;move[4].jump = 1;//위
move[5].vert = 0;move[5].horiz = 0;move[5].jump = -1;//아래
path();
}
}exception in thread main java.lang.nullpointerexception
at maze.main(maze.java:185)
즉, 위의 소스코드 중 바탕색을 칠해 놓은 첫번째 줄에서 뜨는건데..어떠한 오류인지는 알겠는데..
해결을 못하겠네요

1. 널포인터익셉션 오류가 왜 뜨는지요??
2. 저 소스에서 좀더 개선할점이나 잘못된점을 짚어주세요

궁금한건 2가지 정도가 되겠네요, 잘부탁드려요 !

댓글 0

아직 댓글이 없습니다.

프로그램 개발일반 다른 글

목록