에이스타 알고리즘으로 지하철 환승프로그램 만들려고하는데요....
희선
//playpart부분
import java.util.*;
public class playpart
{
public static void main(String[] args)
{
stmake stm=new stmake();
Scanner scan=new Scanner(System.in
String start,end;
st block,temp, startst, endst, RANDOM;
working openlist, templist, closelist, oend, cend, forcal;
int countnum = 0;
int counttransnum = 0;
double RE = 1;
block=new st();
startst=new st();
endst=new st();
RANDOM=new st();
RANDOM.timeall=99999999;
RANDOM.name=랜덤;
closelist=new working
closelist.prev=null;
closelist.in=startst;
closelist.next=null;
cend=closelist;
openlist=new working
openlist.prev=null;
openlist.in= RANDOM;
oend=new working
openlist.next=oend;
oend.prev=openlist;
oend.in=RANDOM;
forcal=new working();
startst.prev=block;
block.daum=startst; while(RE==1)
{
System.out.println(출발역을 입력하시오);
start=scan.nextLine();
System.out.println(도착역을 입력하시오);
end=scan.nextLine();
for(int i=0; istm.stnum; i++)
{
if(stm.s[i].name.equals(start))
{
startst=stm.s[i];
startst.state=2;
}
if(stm.s[i].name.equals(end))
{
endst=stm.s[i];
}
}
for(int i=0; istartst.nextst; i++)
{
templist=new working();
templist.prev=oend;
oend.next=templist;
templist.next=null;
oend=templist;
oend.in=startst.next[i];
oend.in.prev=cend.in;
oend.in.state=1;
startst.next[i].timeall = startst.timeall + startst.tonext[i];
while(cend.in != endst)
{
templist=forcal.findleasttime(openlist);
if(templist.prev==null)
{
templist.prev=cend;
templist.next.prev=null;
templist.next=null;
cend=templist;
}
else if(templist.next==null)
{
oend=oend.prev;
templist.prev.next=null;
templist.prev=cend;
cend=templist;
}
else
{
templist.prev.next=templist.next;
templist.next.prev=templist.prev;
templist.prev=cend;
templist.next=null;
cend=templist;
}
for(int i=0; icend.in.nextst; i++)
{
System.out.println(cend.in.name + - + cend.in.next[i].name + - + cend.in.next[i].state);
if(cend.in.next[i].state==0)
{
working twk=new working();
twk.in=cend.in.next[i];
twk.in.timeall = cend.in.timeall + cend.in.tonext[i];
twk.in.prev=cend.in;
twk.in.state=1;
twk.next=null;
twk.prev=oend;
oend.next=twk;
oend=oend.next;
}
else if(cend.in.next[i].state==2)//state가 2이면 건너서 연결되어 있는 것.
{
if(templist.in.next[i].timeall (cend.in.timeall + cend.in.tonext[i]))
{
templist.in.next[i].timeall = cend.in.timeall + cend.in.tonext[i];
templist.in.next[i].prev = cend.in;
}
}
else if(cend.in.next[i].state==1)//state가 1이면 연결됬다고 표시하는 것
{
if(templist.in.next[i].timeall (cend.in.timeall + cend.in.tonext[i]))
{
templist.in.next[i].timeall = cend.in.timeall + cend.in.tonext[i];
templist.in.next[i].next[i].prev = cend.in;
}
}
}
}
System.out.println(===============이 동 경 로=================);
temp=endst;
while(temp.prev != null)
{
temp.prev.daum=temp;
temp=temp.prev;
countnum++;//지나는 역의 개수를 센다.
}
temp=startst;//endst부터 startst에 이르기까지 연산을 반복한다.
while(temp.daum != null)//startst에서부터 연산을 시작한다.
{
if(temp==startst)
{
System.out.println(역이름 : + temp.name + ( + temp.linenum + 호선) + 소요시간 : + temp.timeall+ 분 + 출발역 );
temp = temp.daum;
}
else if((temp.transfer==1)&&(temp.linenum != temp.daum.linenum))
{
System.out.println(역이름 : + temp.name + ( + temp.linenum + 호선) + 소요시간 : + temp.timeall + 분 + 환승역 );
temp = temp.daum;
counttransnum++;//환승역 개수 카운트
}
else if(temp.transfer==0)//일반적인 호선일 때
{
System.out.println(역이름 : + temp.name + ( + temp.linenum + 호선) + 소요시간 : + temp.timeall + 분);
temp = temp.daum;
}
}
System.out.println(역이름 : + temp.name + ( + temp.linenum +호선) + 소요시간 : + temp.timeall + 분 + 도착역 );
System.out.println(=============================================);
System.out.println(통과역 : + countnum + 개 + 환승역 : + counttransnum + 개 );
System.out.println(=============================================);
RE++;
Scanner scan1=new Scanner(System.in);
System.out.println(프로그램을 다시 시작하시겠습니까?);
System.out.println(1. YES 2. NO);
System.out.println(=============================================);
RE = scan1.nextInt();
if(RE==2)
{
System.out.println(프로그램을 종료합니다);
}
}
}
}
//역에 대한 클래스 선언
public class st
{
double timeall=0;
st prev=null;
st daum=null;
st[] next;
double[] tonext;//다음 역까지 걸리는 시간을 표시하기 위한 변수
String name;
int linenum;//노선번호
int transfer;//값이 0일때는 환승불가, 값이 1일 때 환승가능
int state=0; //에이스타 알고리즘 구현을 위한 state변수이다.
int nextst;
void inst()
{
next=new st[nextst];
tonext=new double[nextst];
}
}
//최소시간 계산을 위한 부분
public class working
{
working prev=null,next=null;
st in;
working findleasttime(working w)//최소시간을 찾는다.
{
working ret=new working();
working temp=w;
double leasttime=9999999;
while(temp.next != null)//!=의 의미는 op1과 op2가 다르면 참이라는 뜻이다.
//while은 어떤 조건식을 만족하는 동안 문장을 수행한다.
//다음 역으로 가는 시간이 존재하지 않을 때 알고리즘을 종료
{
if(temp.in.timeall leasttime)//temp에 저장된 총 시간이 최종시간보다 작을 때
{
leasttime=temp.in.timeall;
ret=temp;//temp값을 ret에 임시저장
}
temp=temp.next; //다음 역도 마찬가지로 계산해주어 도착역까지 계산을 반복
}
if(temp.in.timeall = leasttime)//거쳐간 역의 모든 시간의 합이 최소시간보다 작거나 같을 때
{
leasttime=temp.in.timeall; //전자의 값에 후자의 값을 대입한다.
ret=temp;//ret에 temp값을 임시저장
}
System.out.println( );
System.out.println(leasttime + 분);//그리고 전자의 값, 즉 계산된 최소시간을 출력한다.
return ret;//ret값을 초기값으로 리턴한다.
}
}
//역 정보 입력
//닥터자바는 한글로 데이터를 입력받는데에 제한이 있으므로 역 이름을 숫자로 표기함
public class stmake
{
int stnum;//역의 개수를 선언하기 위한 변수선언
st [] s;
stmake()
{
stnum=62;//역의 총 개수를 지정
s = new st[stnum];
for(int i=0;i62;i++)
{
s[i]=new st();
}
//////////////////////////////////////////////////////////////////////////////////////3호선역
s[0].nextst=1;
s[0].inst();//next와 tonext의 정의를 이용하기 위함
s[0].name=1;//대저
s[0].linenum=3;
s[0].next[0]=s[1];//s[0]에서 s[1]으로 연결됨
s[0].tonext[0]=2;//s[0]에서 s[1]으로 가는데 소요되는 시간을 나타냄
s[0].transfer=0;
s[1].nextst=2;
s[1].inst();
s[1].name=2;//체육공원
s[1].linenum=3;
s[1].next[0]=s[0];
s[1].next[1]=s[2];
s[1].tonext[0]=2;
s[1].tonext[1]=2;
s[1].transfer=0;
s[2].nextst=2;
s[2].inst();
s[2].name=3;//강서구청
s[2].linenum=3;
s[2].next[0]=s[1];
s[2].next[1]=s[3];
s[2].tonext[0]=2;
s[2].tonext[1]=3;
s[2].transfer=0;
s[3].nextst=2;
s[3].inst();
s[3].name=4;//구포
s[3].linenum=3;
s[3].next[0]=s[2];
s[3].next[1]=s[4];
s[3].tonext[0]=3;
s[3].tonext[1]=3;
s[3].transfer=0;
역은 원래 62개정도를 입력을 했는데, 그냥 이정도만 썻구요
playpart부분에서 제일 밑에 temp.dautemp.daum이 값이 자꾸 null이 떠서
최종적으로 역이름과 호선 그리고 시간이 출력되는 while부분의 연산이 시행되지 않네요
왜그런거죠??
그리고 이 문제를 해결하려면 어케해야하나요ㅜㅜ
고수님의 답변 부탁드립니다.
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2701285 | 아이폰이나 안드로이드 폰 인터넷으로 볼때 | 배꽃 | 2025-07-12 |
2701230 | 테마 설정하면 밑에 뜨는 글 삭제 (1) | 창의적 | 2025-07-12 |
2701177 | css적용이 안되요~ (6) | 다니엘 | 2025-07-11 |
2701151 | 사이트작업시 inputbox 가 readonly 형태표시 어떻게 하시나요? (1) | 찬내 | 2025-07-11 |
2701123 | 간단한 select 질문입니다 (3) | 천사의눈물 | 2025-07-11 |
2701061 | 비베질문.. | 똘끼 | 2025-07-10 |
2701034 | 메일폼 내 script 삽입가능한 방법 없을까요.. (2) | 마음새 | 2025-07-10 |
2701008 | 분명히 버튼을 만들었는데 액션이 안걸립니다. (3) | 재찬 | 2025-07-10 |
2700923 | 전체중앙정렬&독타입&쿼크모드 ㅜㅠ (8) | 푸른들 | 2025-07-09 |
2700893 | 질문드리겠습니다. | 도도한 | 2025-07-09 |
2700793 | 무비클립에 마우스 오버시 랜덤으로 효과음 나기는 어떻게 ;; (1) | 바닐라 | 2025-07-08 |
2700741 | 웹전송? (2) | 연와인 | 2025-07-07 |
2700686 | 카테고리 호버시 세부카테고리 보이게하는 것, css로만 가능할까요?? (3) | 다힘 | 2025-07-07 |
2700658 | 메타태그 질문드립니다..ㅠㅠ;;; | 모해 | 2025-07-07 |
2700632 | 외부에서 이미지 파일을 불러와야 합니다. 도와주세요. (4) | 에일린 | 2025-07-06 |
2700579 | (air + as3) smtp 이용해서 첨부파일 포함해서 메일 보내기 | 물보라 | 2025-07-06 |
2700524 | 클릭시 밑에 메뉴 나오게 (4) | 새솔 | 2025-07-05 |
2700505 | activex를 비쥬얼6.0으로 만들었는데요 비스타 배포시에 안되서 질문드립니다 (3) | 참이 | 2025-07-05 |
2700452 | c언어에서... 자료형 구분.... (3) | 시내 | 2025-07-05 |
2700422 | 버전에 관해 질문 | 라온제나 | 2025-07-04 |