PAT甲级 1128. N Queens Puzzle (20)

1128. N Queens Puzzle (20)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general N queens problem of placing N non-attacking queens on an N×N chessboard. (From Wikipedia - "Eight queens puzzle".)

Here you are NOT asked to solve the puzzles. Instead, you are supposed to judge whether or not a given configuration of the chessboard is a solution. To simplify the representation of a chessboard, let us assume that no two queens will be placed in the same column. Then a configuration can be represented by a simple integer sequence (Q1, Q2, ..., QN), where Qi is the row number of the queen in the i-th column. For example, Figure 1 can be represented by (4, 6, 8, 2, 7, 1, 3, 5) and it is indeed a solution to the 8 queens puzzle; while Figure 2 can be represented by (4, 6, 7, 2, 8, 1, 9, 5, 3) and is NOT a 9 queens' solution.

 
Figure 1
 
Figure 2

Input Specification:

Each input file contains several test cases. The first line gives an integer K (1 < K <= 200). Then K lines follow, each gives a configuration in the format "N Q1 Q2 ... QN", where 4 <= N <= 1000 and it is guaranteed that 1 <= Qi <= N for all i=1, ..., N. The numbers are separated by spaces.

Output Specification:

For each configuration, if it is a solution to the N queens problem, print "YES" in a line; or "NO" if not.

Sample Input:
4
8 4 6 8 2 7 1 3 5
9 4 6 7 2 8 1 9 5 3
6 1 5 2 6 4 3
5 1 3 5 2 4
Sample Output:
YES
NO
NO
YES
————————————————————————————————————

题目的意思是给出n列皇后位置,判断是否不会互相攻击

思路:n^2暴力判断

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;


int a[10005];
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        int flag=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<i; j++)
            {
                if(a[i]==a[j]||fabs(a[i]-a[j])==fabs(j-i))
                {
                    flag=1;
                    break;
                }
            }
        printf("%s\n",flag==1?"NO":"YES");
    }
    return 0;
}


N-Queens问题是一个经典的回溯算法问题,它涉及在一个n×n的棋盘上放置n个皇后,使得任意两个皇后都不在同一行、同一列或对角线上。要将其动画演示,你可以使用Python的pygame库来创建一个动态棋盘,并逐步添加皇后的位置。下面是一个简单的示例代码框架: ```python import pygame import numpy as np # 初始化Pygame pygame.init() # 定义棋盘大小和颜色 board_size = (400, 400) cell_size = board_size[0] // n colors = [(255, 255, 255), (0, 0, 0)] # 白色背景,黑色格子 # 创建窗口 screen = pygame.display.set_mode(board_size) pygame.display.set_caption("N-Queens 动画") # 定义函数用于放置皇后并更新图像 def place_queen(position): # ... (在这里实现绘制皇后和删除旧位置的代码) # 主函数 def animate_queens(n): board = np.zeros((n, n), dtype=np.bool) for i in range(n): # 递归尝试每个位置 if not solve_n_queens(i, board): break # 更新屏幕 screen.fill(colors[0]) for j, queen in enumerate(board): if queen: x, y = i * cell_size, j * cell_size draw_square(x, y, colors[1], cell_size) pygame.display.flip() # 短暂暂停以显示动画 pygame.time.wait(500) def solve_n_queens(row, board): # ... (在这里实现N-Queens问题的回溯算法) # 其他辅助函数 def draw_square(x, y, color, size): pygame.draw.rect(screen, color, (x, y, size, size)) # 主程序开始 n = 8 # 可以调整这个值 animate_queens(n) # 循环直到用户关闭窗口 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 清理并退出 pygame.quit() ``` 这个代码示例仅提供了一个基本框架,你需要填充`solve_n_queens()`函数实现回溯算法,以及`place_queen()`函数用于在屏幕上绘制皇后。同时,为了完整实现动画效果,你还需要处理键盘事件或者按钮点击来控制下一个皇后的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值