동적 할당을 할 때 에러가 생겨요 ㅠ.ㅠ (0-1 배낭 문제) 제발 도와주세요 ㅠㅠ
갤투
질문 제목 :동적 할당을 할 때 에러가 생겨요 ㅠ.ㅠ0-1knapsack problem을 branch and bound(분기한정법)을이용해서 해결하는 과제에용:)
(각 아이템에 weight과 benefit이 주어졌을 때 제한된 포대자루에 아이템을 어떻게 넣어야 최고 수익을 얻을 수 있을지에 대한 문제입니다)
저는 linked list를 이용해서 트리형태로 구현하려고 해서
각 node가 왼쪽을 가리키는 포인터와 오른쪽을 가리키는 포인터를 있게 했고,
점점 가지가 뻗쳐나갈 때마다 동적으로 메모리를 할당하게 했고
promising한 node들을 point하는 linked list를 추가로 두어서
그 promising linked list를 보고 가지를 뻗쳐 나가게 하려고 했는데가지가 한번 밑으로 뻗어나가고 나서
(동적 할당으로)
두번째로 뻗어 나가려고 할때 오류가 나요 ㅠㅠㅠㅠㅠ전체 코드 첨부하고
또 그림으로 설명한 사진 파일도 첨부합니다 ㅠㅠㅠㅠ
제발 제발 꼭 도와주세요 ㅠㅠㅠ
축젠데 2틀째 과제하면서 밤을 새고 있어요 ㅠㅠㅠ
질문 내용 :
a는 item의 개수이며
weightbound는 포대가 실을 수 있는 무게입니다.int branchandbound(int a, int weightbound)
{
int max_benefit;
int item=0;
int i,j;
bnb *node, *root;
promising *promisingcur, *rootofpromising, *biggestbound, *check, *temp;
/*promising한 node를 point하고 있는 linked list를 만든다 (struct bnb에서 right변수 사용)*/
rootofpromising = (promising *)malloc(sizeof(promising));
promisingcur = rootofpromising;
biggestbound = (promising *)malloc(sizeof(promising));
/*initialize first node of the branch and bound tree*/
root = (bnb *)malloc(sizeof(bnb));
node = root;
ce:pre node-benefit = 0;
node-weight = 0;
node-itemno = 0;
node-promising = 1;
node-bound = getbound(a, node-benefit, node-weight, weightbound, node-itemno);
node-right = node-left = null;
node-up = null; /*promising linked list의 다음 node 메모리를 할당해주고 root node를 추가시킨다*/
temp = promisingcur;
promisingcur-next = (promising *)malloc(sizeof(promising));
promisingcur = promisingcur-next;
promisingcur-bnbnode = node;
promisingcur-next = null;
promisingcur-back = temp;
/*왼쪽과 오른쪽 node로 extend 시킨다*/
extend(a, weightbound, node, &max_benefit, promisingcur); check = rootofpromising-next;
while(rootofpromising-next != null)
{
/*promising linked list에서 node가 한개면 그 node를 extend함*/
if(check-next == null)
biggestbound-bnbnode = check-bnbnode;
/*두개 이상이면 계속해서 비교를 해서 가장 bound가 큰 node를 extend함*/
else
{
while(check-next != null)
{
if((check-next-bnbnode-bound) (check-bnbnode-bound))
{
biggestbound-bnbnode = check-next-bnbnode;
}
check = check-next;
}
}
//extend(a, weightbound, biggestbound-bnbnode, &max_benefit, promisingcur);
biggestbound-bnbnode-right = (bnb *)malloc(sizeof(bnb));
biggestbound-bnbnode-left = (bnb *)malloc(sizeof(bnb));
*
이 지점에서 에러가 납니다.
extend함수에서 어떤 노드에 동적으로 메모리를 할당하려고 할 때런타임 에러로
unhandled exception at 0x77d215de in algorithmhomework.exe: 0xc0000005: access violation writing location 0xcdcdcde5.
라고 뜹니다 ㅠ.ㅠ }
}void extend(int a, int weightbound, bnb *node, int *max_benefit, promising *promisingcur)
{
bnb *rnode, *lnode;
promising *temp;
node-right = (bnb *)malloc(sizeof(bnb));
node-left = (bnb *)malloc(sizeof(bnb));
lnode = node-left;
rnode = node-right;
/*put values for the left node*/
/*parent node에서 item의 benefit과 weight을 더한다*/
lnode-benefit = node-benefit + inputvalue[0].benefit;
lnode-weight = node-weight + inputvalue[0].weight;
lnode-itemno = node-itemno + 1;
pr/spanprintf(lnode\tbenefit:%d\tweight:%d\titem:%d\n,lnode-benefit,lnode-weight,lnode-itemno);
/*bound는 greedy함수를 조금 변형시킨 함수를 사용한다*/
lnode-bound = getbound(a, lnode-benefit, lnode-weight, weightbound, lnode-itemno); /*이 node의 benefit이 max benefit보다 크면 max_benefit을 이 benefit 값으로 바꾼다(단 이 item을 넣은 weight이 knapsack weight을 넘지 않을 때)*/ if(lnode-benefit *max_benefit && lnode-weight = weightbound)
*max_benefit = lnode-benefit;
/*만약 bound가 max_benefit보다 작거나 weight이 knapsack weight을 넘으면node를 nonpromising으로 지정해준다 (promising을 0으로 설정)*/ if(lnode-bound *max_benefit || lnode-weight weightbound)
{
lnode-promising= 0;
lnode-left = null;
lnode-right = null;
}
/*promising인 경우 왼쪽과 오른쪽 node 메모리를 할당해주고 promisingnodes linked list에 추가해준다*/
else
{
lnode-promising = 1;
lnode-up = node;
lnode-right = (bnb *)malloc(sizeof(bnb));
lnode-left = (bnb *)malloc(sizeof(bnb));
lnode-right-right = lnode-right-left = lnode-left-right = lnode-left-left = null;
temp = promisingcur;
promisingcur-next = (promising *)malloc(sizeof(promising));
promisingcur = promisingcur-next;
promisingcur-bnbnode = lnode;
promisingcur-next = null;
promisingcur-back = temp;
} /*put values for the right node (do the same as the left side)*/ rnode-benefit = node-benefit;
rnode-weight = node-weight;
rnode-itemno = node-itemno + 1;
printf(rnode\tbenefit:%d\tweight:%d\titem:%d\n,rnode-benefit,rnode-weight,rnode-itemno);
rnode-bound = getbound(a, rnode-benefit, rnode-weight, weightbound, rnode-itemno);
if(rnode-benefit *max_benefit && rnode-weight = weightbound)
*max_benefit = rnode-benefit;
if(rnode-bound *max_benefit || rnode-weight weightbound)
{
rnode-promising= 0;
rnode-right = null;
rnode-left = null;
}
else
{
rnode-promising= 1;
rnode-up = node;
rnode-right = (bnb *)malloc(sizeof(bnb));
rnode-left = (bnb *)malloc(sizeof(bnb));
rnode-right-right = rnode-right-left = rnode-left-right = rnode-left-left = null; temp = promisingcur;
promisingcur-next = (promising *)malloc(sizeof(promising));
promisingcur = promisingcur-next;
promisingcur-bnbnode = rnode;
promisingcur-next = null;
promisingcur-back = temp;
} //자녀가 둘다 promising이라면 부모 node를 promising linked list에서 delete(더이상 부모에서부터 extend하지 않을 테니까) if(lnode-promising==1 && rnode-promising==1)
{
temp = promisingcur-back-back;
promisingcur-back-back-back-next = promisingcur-back;
promisingcur-back = promisingcur-back-back;
free(temp);
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
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 |