Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 Modified Knight가 도달할 수 있는 모든 가능한 위치를 셉니다.

<시간/>

이 튜토리얼에서는 Modified Knight가 도달할 수 있는 가능한 위치의 수를 찾는 프로그램에 대해 논의할 것입니다.

이를 위해 8*8 체스판이 제공됩니다. 우리의 임무는 Modified Knight가 주어진 단계 수로 캡처할 수 있는 위치의 수를 찾는 것입니다.

#include <bits/stdc++.h>
using namespace std;
//finding the positions
void findSteps(int current_row, int current_column,int curr, int board_size, int steps,int* visited){
   //bound checking
   if (current_row >= board_size || current_row < 0
      || current_column >= board_size || current_column < 0
      || curr > steps) {
      return;
   }
   if (curr == steps) {
      *((visited + (current_row)*board_size) + current_column) = 1;
      return;
   }
   findSteps(current_row - 2, current_column - 1,curr + 1, board_size, steps, visited);
   findSteps(current_row - 2, current_column + 1,curr + 1, board_size, steps, visited);
   findSteps(current_row - 1, current_column - 2,curr + 1, board_size, steps, visited);
   findSteps(current_row - 1, current_column - 1,curr + 1, board_size, steps, visited);
   findSteps(current_row - 1, current_column + 1,curr + 1, board_size, steps, visited);
   findSteps(current_row - 1, current_column + 2,curr + 1, board_size, steps, visited);
   findSteps(current_row + 1, current_column - 2,curr + 1, board_size, steps, visited);
   findSteps(current_row + 1, current_column - 1,curr + 1, board_size, steps, visited);
   findSteps(current_row + 1, current_column + 1,curr + 1, board_size, steps, visited);
   findSteps(current_row + 1, current_column + 2,curr + 1, board_size, steps, visited);
   findSteps(current_row + 2, current_column - 1,curr + 1, board_size, steps, visited);
   findSteps(current_row + 2, current_column + 1,curr + 1, board_size, steps, visited);
   return;
}
int countSteps(int current_row, int current_column,int board_size, int steps){
   int visited[board_size][board_size];
   for (int i = 0; i < board_size; i++) {
      for (int j = 0; j < board_size; j++) {
         visited[i][j] = 0;
      }
   }
   int answer = 0;
   findSteps(current_row, current_column, 0,board_size, steps, (int*)visited);
   for (int i = 0; i < board_size; i++) {
      for (int j = 0; j < board_size; j++) {
         if (visited[i][j] == 1) {
            answer++;
         }
      }
   }
   return answer;
}
int main(){
   int board_size = 8, steps = 1;
   int current_row = 4, current_column = 4;
   cout << countSteps(current_row, current_column,board_size, steps);
   return 0;
}

출력

12