컴퓨터공학부에 재학중인 학생임 ㅠㅠ
프로그램을 짜다 하두 안되서.. 횽들한테 요청해봐요
기본적으로 Knight Tour 프로그램을 짜는겁니다.
인터넷상에서 Recursive 로 구현하는 소스가 많아서 Recursive 로 구현하는건 문제가 없었지만,
이것을 Non-Recursive 형태로 구현하라는 과제 입니다...
학생스스로 노력해서 과제를 해야되지만,
이틀동안 매달렸는데 결국 못하고 있어 ㅠㅠㅠㅠㅠ
기존의 Recursive 형태를 스택과 트리형태로 구현하여 짜보았는데,,, 무한루프스핀 ㅠㅠ
이렇게 짜는면 안되는건지, 도움좀줘영ㅇ 형들 ㅠㅠ
-------------------------------------------------------------------------------------
[Recursive]
int knightTour (int m, int n, point pos, int counter){
int i;
point next;
if(counter == m * n)
return 1;
for(i = 0; i < 8; i++){
// 다음 포지션
next.x = pos.x + direction[i].x;
next.y = pos.y + direction[i].y;
if ( next.x > 0 && next.x <= n &&
next.y > 0 && next.y <= m &&
board[next.y][next.x] != MARK )
{
board[next.y][next.x] = MARK;
path[next.y][next.x] = counter+1;
if ( knightTour(m, n, next, counter+1) )
return 1;
board[next.y][next.x] = UNMARK;
}
}
return 0;
}
-------------------------------------------------------------------------------------
[Non-Recursive - 안되네요 ㅠ]
int knightTour_nonRecursive (int m, int n, point pos){
int result = 0;
int counter = 1;
point next;
position current;
current.x = pos.x;
current.y = pos.y;
current.dir = 0;
for(int i = current.dir; i < 8; ){
// 다음 포지션
next.y = current.y + direction[i].y;
next.x = current.x + direction[i].x;
if( next.x > 0 && next.x <= n && next.y > 0 && next.y <= m && board[next.y][next.x] != MARK ){
current.dir = i;
// 스택에 해당 위치와 이동방향 저장
stackPos.push(current);
temp = current;
// 이동
current.x = next.x;
current.y = next.y;
board[next.y][next.x] = MARK;
path[next.y][next.x] = ++ counter;
if(counter == m * n){
cout << "42break"<< endl;
break;
}else
i = 0;
}else if(i == 7){
while(1){
board[next.y][next.x] = UNMARK;
path[next.y][next.x] = 0;
counter --;
current = stackPos.top();
stackPos.pop();
i = current.dir + 1;
if(i > 7)
continue;
else
break;
}
}else{
i ++;
}
}
if(counter == m * n)
result = 1;
return result;
}
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.