2n皇后的两种解法 python(全面解析),第二种耗时大大缩短

23 篇文章 1 订阅
12 篇文章 19 订阅

题目

在这里插入图片描述

两种思路

思路一:同时放黑皇后和白皇后
思路二:放完黑皇后以后,再放白皇后

第一种解法(耗时较长)

同时考虑黑皇后和白皇后
考虑index行,分三步:

  • 在index行放置黑皇后
  • 在index行放置白皇后
  • 递归,在index+1重复上述操作
  • 递归结束条件 index == n
    代码
from collections import defaultdict

n = int(input())
board = []
for i in range(n):
    board.append(input().split())
col1 = defaultdict(bool)
dia11 = defaultdict(bool)
dia21 = defaultdict(bool)
col2 = defaultdict(bool)
dia12 = defaultdict(bool)
dia22 = defaultdict(bool)

count = 0


def dfs(index):
    global count
    if n <= 0:
        return 0
    if index == n:

        count += 1
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col1[j] and not dia11[index + j] and not dia21[index - j] \
                and board[index][j] == "1":
            col1[j] = True
            dia11[index + j] = True
            dia21[index - j] = True
            board[index][j] = "0"
            for k in range(n):
                if not col2[k] and not dia12[index + k] and not dia22[index - k] \
                        and board[index][k] == "1":
                    col2[k] = True
                    dia12[index + k] = True
                    dia22[index - k] = True
                    board[index][k] = "0"
                    dfs(index + 1)
                    col2[k] = False
                    dia12[index + k] = False
                    dia22[index - k] = False
                    board[index][k] = "1"

            col1[j] = False
            dia11[index + j] = False
            dia21[index - j] = False
            board[index][j] = "1"

    return 0


dfs(0)
print(count)

在这里插入图片描述

第二种解法(耗时大大缩短)

思路

  • 先在index放置黑皇后
  • 递归,在index+1行放置黑皇后
  • 递归到底,index == n ,这个时候说明黑皇后已经全部放置完毕(一种情况),这个时候再开始第二次dfs2(0),这个时候是放置白皇后,白皇后也放置完毕以后,一个解就找到了。

代码:

from collections import defaultdict

n = int(input())
board = []
for i in range(n):
    board.append(input().split())
col1 = defaultdict(bool)
dia11 = defaultdict(bool)
dia21 = defaultdict(bool)
col2 = defaultdict(bool)
dia12 = defaultdict(bool)
dia22 = defaultdict(bool)
count = 0


def dfs2(index):
    global count
    if index == n:
        count += 1
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col2[j] and not dia12[index + j] and not dia22[index - j] \
                and board[index][j] == "1":
            col2[j] = True
            dia12[index + j] = True
            dia22[index - j] = True
            board[index][j] = "0"
            dfs2(index + 1)
            col2[j] = False
            dia12[index + j] = False
            dia22[index - j] = False
            board[index][j] = "1"

    return 0

def dfs1(index):
    if n <= 0:
        return 0
    if index == n:
        dfs2(0)
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col1[j] and not dia11[index + j] and not dia21[index - j] \
                and board[index][j] == "1":
            col1[j] = True
            dia11[index + j] = True
            dia21[index - j] = True
            board[index][j] = "0"
            dfs1(index + 1)
            col1[j] = False
            dia11[index + j] = False
            dia21[index - j] = False
            board[index][j] = "1"

    return 0


dfs1(0)
print(count)

技术交流:
mark

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
根据给定的问题描述和代码,这是一个关于蓝桥杯C++题目中的n皇后问题。题目要求在一个n*n的棋盘上放置n个黑皇后和n个白皇后,使得任意两个黑皇后和任意两个白皇后都不在同一行、同一列或同一条对角线上。代码使用递归和回溯的方法来解决这个问题。 代码中的pd1函数用于判断黑皇后的位置是否合法,pd2函数用于判断白皇后的位置是否合法。fang1函数用于放置黑皇后,fang2函数用于放置白皇后。最后,通过调用fang1函数来计算总共有多少种放法。 需要注意的是,代码中使用的p1和p2数组分别表示黑皇后和白皇后的位置,qi数组表示棋盘上每个位置是否可以放皇后。 因此,根据给定的代码,可以计算出总共有多少种放法。 #### 引用[.reference_title] - *1* [蓝桥杯 基础练习 2n皇后问题C/C++(可作DFS练习)](https://blog.csdn.net/qq_43838669/article/details/129306490)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [蓝桥杯2n皇后问题](https://blog.csdn.net/qq_59611494/article/details/128682794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值