इस ट्यूटोरियल में, हम मॉडिफाइड नाइट द्वारा प्राप्त संभावित पदों की संख्या का पता लगाने के लिए एक कार्यक्रम पर चर्चा करेंगे।
इसके लिए हमें 8*8 की बिसात दी जाएगी। हमारा काम यह पता लगाना है कि दिए गए चरणों की संख्या के साथ संशोधित नाइट कितने पदों पर कब्जा कर सकता है।
उदाहरण
#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