Synopsis ¶
- 나ì�´íŠ¸ê°€ 움ì§�ì�¼ 수 있는 ì—¬ë�Ÿ ë°©í–¥ì�„ ì�¼ì •한 순서ì—� ë”°ë�¼ 움ì§�ì�¸ë‹¤.
(2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1)
- 나ì�´íŠ¸ê°€ 체스íŒ�ì�˜ ëª¨ë“ ì¹¸ì�„ 한번씩 방문하면 종료ë�œë‹¤.
- 시작 ì§€ì �ì�„ (0, 0) 부터 (7, 7) 까지 64가지로 하여 순서대로 검색한다.
- 시작 ì§€ì �ê³¼ 종료 ì§€ì �ì�„ 화면ì—� ì¶œë ¥í•œë‹¤.
- 나ì�´íŠ¸ê°€ 움ì§�ì�¸ 순서를 화면ì—� ì¶œë ¥í•œë‹¤.
Source ¶
Knight.h ¶
~cpp
// Knight.h: interface for the CKnight class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_KNIGHT_H__B5234B12_3582_4CB8_8253_6ADFBE7B5E68__INCLUDED_)
#define AFX_KNIGHT_H__B5234B12_3582_4CB8_8253_6ADFBE7B5E68__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CKnight
{
private:
int m_ChessBoard[8][8];
int m_Vertical[8], m_Horizontal[8];
int m_CurrentRow, m_CurrentColumn;
int m_Footprint[65];
unsigned int m_Move;
public:
void tour();
void showPosition();
void showNightTour();
CKnight(int sr, int sc);
virtual ~CKnight();
};
#endif // !defined(AFX_KNIGHT_H__B5234B12_3582_4CB8_8253_6ADFBE7B5E68__INCLUDED_)
Knight.cpp ¶
~cpp
// Knight.cpp: implementation of the CKnight class.
//
//////////////////////////////////////////////////////////////////////
#include "Knight.h"
#include "iostream"
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKnight::CKnight(int sr, int sc)
{
int tempHorizontal[] = {2, 1, -1, -2, -2, -1, 1, 2};
int tempVertical[] = {-1, -2, -2, -1, 1, 2, 2, 1};
for (int row = 0 ; row < 8 ; row++){
for (int col = 0 ; col < 8 ; col++) {
m_ChessBoard[row][col] = 0;
m_Footprint[row * 8 + col] = 0;
}
m_Horizontal[row] = tempHorizontal[row];
m_Vertical[row] = tempVertical[row];
}
m_Move = 0;
m_CurrentRow = sr, m_CurrentColumn = sc;
}
CKnight::~CKnight()
{
}
void CKnight::showNightTour()
{
for (int row = 0 ; row < 8 ; row++) {
printf("\n\n");
for (int col = 0 ; col < 8 ; col++)
printf("%d\t", m_ChessBoard[col][row]);
}
printf("\n\n경 로 :\n");
for (int counter = 0 ; counter < 64 ; counter++){
printf(" %d", m_Footprint[counter]);
if (counter % 32 == 31)
printf("\n");
}
printf("��횟수 : %d\n", m_Move);
system("pause");
system("cls");
}
void CKnight::showPosition()
{
printf("(%d, %d)", m_CurrentRow, m_CurrentColumn);
}
void CKnight::tour()
{
for (int counter = 1 ; counter < 65 ; counter++) {
m_ChessBoard[m_CurrentRow][m_CurrentColumn] = counter;
int direction = -1;
while (++direction <= 8 && m_ChessBoard[m_CurrentRow][m_CurrentColumn] < 64) {
if (direction > 7) { // BackStep
direction = m_Footprint[--counter];
int rewind = (direction + 4) % 8;
m_ChessBoard[m_CurrentRow][m_CurrentColumn] = 0;
m_CurrentRow += m_Horizontal[rewind];
m_CurrentColumn += m_Vertical[rewind];
continue;
}
m_CurrentRow += m_Horizontal[direction];
m_CurrentColumn += m_Vertical[direction];
if (m_CurrentRow < 0 || m_CurrentRow > 7
|| m_CurrentColumn < 0 || m_CurrentColumn > 7
|| m_ChessBoard[m_CurrentRow][m_CurrentColumn] != 0){
m_CurrentRow -= m_Horizontal[direction];
m_CurrentColumn -= m_Vertical[direction];
continue;
}
m_Footprint[counter] = direction;
m_Move++;
break;
}
}
}
main.cpp ¶
~cpp
#include "Knight.h"
int main(){
for (int row = 0 ; row < 8 ; row++)
for (int col = 0 ; col < 8 ; col++){
CKnight knight(row, col);
knight.showPosition();
knight.tour();
knight.showPosition();
knight.showNightTour();
knight.~CKnight();
}
return 0;
}
Bugs & Gossips ¶
- 재니야
; Nightê°€ ì•„ë‹ˆê³ Knight야;
- 와하하~ 페ì�´ì§€ ì�´ë¦„ 잘못 ì �어서 새로 만들다..ã…¡.ã…¡;;; -재니
- 와하하~ 페ì�´ì§€ ì�´ë¦„ 잘못 ì �어서 새로 만들다..ã…¡.ã…¡;;; -재니
- 위ì�˜ ì•Œê³ ë¦¬ì¦˜ì�€ broot-force하게 루트를 찾는 것ì�¸ë�°,
나ì�´íŠ¸ê°€ 위치한 ì…€ì�´ ê³ ë¦½ë�œ 경우 BackStep ê³¼ì •ì�„ 한 번 ë�” 실행하면 루트를 찾는 시간ì�´ 훨씬 ì§§ì•„ì§�.
- ì…€ì�´ ê³ ë¦½ë�˜ë©´ 어차피 다른 경로로 ì ‘ê·¼í• ìˆ˜ 없어서 BackStep ì�„ ë‘�번 한 ê±´ë�°...
몇몇 경우�서 broot-force 보다 � 검색� 많� 하는 경우� 발견�.
- ì…€ì�´ ê³ ë¦½ë�˜ë©´ 어차피 다른 경로로 ì ‘ê·¼í• ìˆ˜ 없어서 BackStep ì�„ ë‘�번 한 ê±´ë�°...










