정답을 알고싶어요
무들
질문 제목 : 정답을 알고싶어요질문 내용 :배열로 짜여진 이 프로그램을 포인터로 바꿔보려고 하는데요 정답을 가지고 있지 않아
확인이 불가능 합니다. ㅠㅠㅠ 도와주실분 없으신가요 같이 한번 짜봐주시면 정말 감사하겠습니다.
프로그램 내용은 자식노드와 부모노드로써 목표 노드를 찾는 프로그램입니다.#include stdio.h
#define LIMITL 256
#define TRUE 1
#define START 1
#define GOAL 999
struct node
{
int nodeid;
int parentid;
};
struct node openlist[LIMITL];
int openlistep = 0;
struct node closedlist[LIMITL];
int closedlistep = 0;
void initlist();
void printlist();
void expand(int id);
void movetofirst();
void removefirst();
int check(int id);
void printroute(int id);
int main()
{
initlist();
printlist();
while (TRUE)
{
if (openlistep == 0)
{
printf(목표 노드를 찾을 수 없습니다.\n);
break;
}
if (openlist[0].nodeid == GOAL)
{
printf(\n목표 노드를 찾았습니다.\n);
printf(%d[%d], openlist[0].nodeid,openlist[0].parentid);
printroute(openlist[0].parentid);
break;
}
expand(openlist[0].nodeid);
movetofirst();
printlist();
}
return 0;
}
void printroute(int id)
{
int i;
for (i = 0; i closedlistep; ++i)
if (closedlist[i].nodeid == id)
{
printf(-%d[%d], closedlist[i].nodeid,
closedlist[i].parentid);
break;
}
if (closedlist[i].parentid != 0)
printroute(closedlist[i].parentid);
printf(\n);
}
int check(int id)
{
int i;
int res = 0;
for (i = 0; i openlistep; ++i)
if (openlist[i].nodeid == id)
res = TRUE;
for (i = 0; i closedlistep; ++i)
if (closedlist[i].nodeid == id)
res = TRUE;
return res;
}
void removefirst()
{
int i;
for (i = 0; i openlistep; ++i)
openlist[i] = openlist[i+1];
--openlistep;
}
void movetofirst()
{
closedlist[closedlistep++] = openlist[0];
removefirst();
}void expand(int id)
{
int tree[][5] =
{
{1, 2, 3, 5},
{2, 4},
{3, 6, 7},
{5, 4, 8},
{6, 5, 7},
{8, 7, 999},
{0} /* 정보의 끝 */
};
int i = 0;
int j;
while (tree[i][0] != 0)
{
if (tree[i][0] == id)
{
for (j = 1; tree[i][j] != 0; ++j)
{
if (check(tree[i][j]) != TRUE)
{
openlist[openlistep].nodeid = tree[i][j];
openlist[openlistep++].parentid = id;
}
}
break;
}
++i;
}
}
void initlist()
{
openlist[0].nodeid = START;
openlist[0].parentid = 0;
++openlistep;
}
void printlist()
{
int i;
printf(\n오픈 리스트 );
for (i = 0; i openlistep; ++i)
printf(%d[%d],, openlist[i].nodeid,
openlist[i].parentid);
printf(\n);
printf(클로즈드 리스트 );
for (i = 0; i closedlistep; ++i)
printf(%d[%d],, closedlist[i].nodeid,
closedlist[i].parentid);
printf(\n);
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2695122 | 구조체에 대해 물어보고 싶은게 있습니다 ^^^.. (7) | 수련 | 2025-05-17 |
2695091 | txt 파일 입출력 후 2차 배열에 저장하기입니다. (3) | 헛장사 | 2025-05-17 |
2695063 | 수도요금 프로그램좀 짜주세요. | 시내 | 2025-05-17 |
2695033 | 답변좀요ㅠㅠ (1) | 비사벌 | 2025-05-16 |
2695010 | C++의 STL은 왜 굳이 템플릿화 시켜서 라이브러리를 만드나요? (초보수준의 질문..) (2) | 엘보어 | 2025-05-16 |
2694958 | 로직이 변한다는 것에 대해서 궁금합니다. | 튼동 | 2025-05-16 |
2694929 | 열혈강의 25-2 두번째 문제 질문 | 지우개 | 2025-05-15 |
2694900 | dequeue 에서 리턴값 프린트 방법알려주세요 오늘 12시까지 대화방에 있습니다 도와주세요 | 미투리 | 2025-05-15 |
2694854 | 절대값을 구할때 (2) | 그녀는귀여웠다 | 2025-05-15 |
2694827 | 이제 어떻게 공부해야할지 모르겠네요 | 새얀 | 2025-05-14 |
2694778 | 순열 계산요. | 맛조이 | 2025-05-14 |
2694754 | ShowWindow 함수를 이용하려 하는데 질문있습니다. (2) | 파도 | 2025-05-14 |
2694731 | 리눅스 커널의 시작점 질문 | 미르 | 2025-05-13 |
2694702 | 이거 뭐가문제인가요 코드수정좀 (3) | 맑은 | 2025-05-13 |
2694675 | C언어 후위표기를 중위표기로 | 앨런 | 2025-05-13 |
2694646 | 안녕하세요 파일 합치기 함수! (1) | 연블루 | 2025-05-13 |
2694618 | 잘몰라서 설명부탁드립니다. scanf 관련 (3) | 파라 | 2025-05-12 |
2694590 | 이 코드가 뭐하는 코드일까요? #2 | 빵순 | 2025-05-12 |
2694559 | 동적할당으로 배열(2차원열)을 만드는데 있어 그걸 함수화시키는데... (1) | 늘솔길 | 2025-05-12 |
2694532 | 네트워크에 관하여... (4) | 황소자리 | 2025-05-12 |