8 Queens Chess Problem

本文介绍了一种解决经典的八皇后问题的方法,通过递归回溯算法寻找所有可能的解决方案,并能够输出包含特定位置皇后的棋盘布局。

题目:八皇后模型,输出包含了某一点的所有皇后

注意:格式上,不同输入之间空两行。数字的最后没有空格,所以把空格写在前面。还有最后一行输出后,后面没有空行了

#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <ctgmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main()
{
	int xunhuan;
	cin>> xunhuan;
	while(xunhuan--){
		int x,y;
		cin >> x >> y;
		int t = 1;
		int a[8] = {0};
		int b[15] = {0};
		int c[15] = {0};
		int j = 0,i=0;
		int temp = 0;
		int count = 1;
		vector<int> m;
		vector<int>::iterator it;
		vector<int> sum;
		
		cout<<"SOLN       COLUMN"<<endl;
		cout<<" #      1 2 3 4 5 6 7 8"<<endl<<endl;
		while(1){
			for(i = 0; i <= 7; i ++){
				for(j = 0; j <= 7; j ++){
					if((a[j]==0) && (b[i+j]==0) && (c[i-j+7]==0) ){//这一点可以有皇后
						m.push_back(j);
						a[j]=1;
						b[i+j]=1;
						c[i-j+7]=1;
						break;
					}
				}
				//如果走完了还没有位置
				
				if(j == 8){
				Next_One:            while(1){
					if(i >= 0){
						i--;
						temp = m[i];
						//删除原来皇后的记录
						a[temp]=0;
						b[i+temp]=0;
						c[i-temp+7]=0;
						m.pop_back();
						
						for(j = temp +1; j <= 7; j ++){
							if((a[j]==0) && (b[i+j]==0) && (c[i-j+7]==0) ){//这一点可以有皇后
								m.push_back(j);
								a[j]=1;
								b[i+j]=1;
								c[i-j+7]=1;
								break;
							}
						}
						if(j != 8) break;
						
					}
				   }
					
				}
			}
			
			//判断
			if(m[y-1] == x-1){
				printf("%2d",count++);
				cout<<"     ";
				for(it = m.begin();it != m.end();it++){
					cout << " " << (*it)+1; //空格在之前出现
				}
				cout<<endl;

			}
				
			
			t++;
			if(t==93) break;
			goto Next_One;
			
		}
		
		// goto Next_One;
		if(xunhuan)cout<<endl;
 //先判断是不是最后一行了,否则不输出回车
	}
		return 0;
}




Sure, let's walk through solving the N-Queens Problem in Python using backtracking and creating a visualization animation with four queens as an example. The N-Queens Problem involves placing N chess queens on an N x N chessboard such that no two queens can attack each other (horizontally, vertically, or diagonally). **Step 1: Import required libraries** ```python import pygame from pygame.locals import * import numpy as np ``` **Step 2: Define helper functions** 1. `is_safe`: Check if a queen can be placed at a specific position without attacking others. 2. `solve_n_queens`: Recursive function to place queens one by one. 3. `draw_board`: Draw the current state of the board. ```python # ... (omitted for brevity) def is_safe(board, row, col): # Check row, column, and diagonal safety return all(board[row] != i or abs(i - col) != j for i, j in zip(range(row, -1, -1), range(col, len(board)))) def solve_n_queens(size, board, row=0): if row == size: # Base case: All queens are placed, print solution display_solution(board) return True for col in range(size): if is_safe(board, row, col): # Try placing a queen at this position board[row][col] = 1 if solve_n_queens(size, board, row + 1): # Move to next row return True else: # If it doesn't lead to a solution, backtrack board[row][col] = 0 # Remove queen from this position # No valid solution found, backtrack return False def draw_board(board): # ... (implement drawing logic using pygame) ``` **Step 3: Create the main loop** ```python def main(): size = 4 board = np.zeros((size, size)) solve_n_queens(size, board) if __name__ == "__main__": main() ``` To create an animated visualization, you'll need to use `pygame` to update the screen after each step, showing the placement of each queen and highlighting their positions. This part would involve setting up event handling, updating the display, and iterating through the recursive calls. Remember that the full implementation of the animation requires more code than shown here, but the basic structure described above will guide you towards solving the N-Queens Problem with visual feedback for four queens. **Related questions:** 1. What does the `is_safe` function check? 2. How does the `solve_n_queens` function work recursively? 3. Where should I start when implementing the visualization with Pygame?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值