首页 > 其他分享 >51.N-Queens

51.N-Queens

时间:2022-11-12 23:25:17浏览次数:39  
标签:return int res 51 Queens queenList col row

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

 

Example 1:

Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above

Example 2:

Input: n = 1
Output: [["Q"]]

 

 

public static List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
int[] queenList = new int[n]; //第i个位置存放的数表示row行时,Q的列,如果queenList[0] = 2,表示第0行的第二列是Q
placeQueen(queenList, 0, n, res);//在第0行放Q
return res;
}

private static void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {
//如果已经填满,就生成结果
if (row == n) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String str = "";
for (int col = 0; col < n; col++){
if(queenList[i] == col) {
str += "Q";
} else {
str += ".";
}
}
list.add(str);
}
res.add(list);
}
for (int col = 0; col < n; col++) {//循环每一列
if (isValid(queenList, row, col)) { //如果在该列放入Q不冲突的话
queenList[row] = col;
placeQueen(queenList, row + 1, n, res);
}
}
}

private static boolean isValid(int[] queenList, int row, int col) {
for (int i = 0; i < row; i++) {
int pos = queenList[i];
if (pos == col) { //和新加入的Q处于同一列
return false;
}
if (pos + row - i == col) { //在新加入的Q的右对角线上
return false;
}
if (pos - row + i == col) { //在新加入的Q的左对角线上
return false;
}
}
return true;
}

标签:return,int,res,51,Queens,queenList,col,row
From: https://www.cnblogs.com/MarkLeeBYR/p/16884995.html

相关文章

  • 【新人必看】加入萌新福利群,51CTO博客周边等你拿!
    在51CTO博客成功发布第一篇博文,可领取新人福利,周边奖品3选1。来分享属于自己的技术踩坑经验吧!(注:​​3种方式!让你3秒在51CTO博客快速发布一篇博文​​​​​​)【活动步骤】①......
  • 题解 P5188 【[COCI2009-2010#4] PALACINKE】
    postedon2022-07-2520:12:26|under题解|source做法:矩阵优化DP+容斥原理。矩阵优化DP先不要考虑商品,写个不管约束条件的DP。令\(f_{t,u}\)表示在\(t\)......
  • 51单片机定时器/计数器中断
    51单片机定时器/计数器中断一、定时器/计数器1-1定时器❤CPU时序相关知识点振荡周期:为单片机提供定时信号的振荡源的周期状态周期:2个振荡周期为1个状态周期,其中振......
  • 51单片机的中断系统 && 外部中断
    51单片机的中断系统&&外部中断中断系统是为使CPU具有外界紧急事件的实时处理能力而设置的。一、中断概念对于单片机来说,中断是指CPU在处理某一事件A时,发生另外一事......
  • N-Queens
    51.N-Queens https://leetcode.cn/problems/n-queens/ classSolution:defis_valid(self,path,x,y):"""给出现有的棋盘path,和位置(x,y),判断在(x,y)......
  • Keil51单片机解决数字显示不稳的问题
    Keil51单片机解决数字显示不稳的问题数字显示不稳,就是我们人眼的特点决定的,0.1秒的残留现象,低于这个值人眼发现不了其中变化,大于这个值就会出现同一个数字闪烁的现象。解......
  • Keil51单片机数码管鬼影显示问题
    Keil51单片机数码管鬼影显示问题所为的鬼影就是程序逻辑正确,但电路逻辑有问题。就是按程序逻辑,前一个数字显示后,直接显示下一组数字,因为没清干净,导致瞬间残留;处理的办法,......
  • arm 实现mdio 读写 88X5113 寄存器
    88X5113芯片说明该芯片和平常的phy芯片不同,以下为芯片采用MDIO接口实现读写寄存器的说明配置说明寄存器分为基地址和扩展地址,每次读写扩展地址的数据,需要先......
  • ed25519加密签名算法及应用
    刷知乎时看到一篇文章,很感兴趣,来学习一下!转载文章:ed25519加密签名算法及应用初次使用Github时都需上传本地的公钥,这时需要提前在本地生成密钥对,使用的是ssh-keygen命令......
  • [Bug0051]idea插件jrebel 运行报错JRebel-JVMTI [FATAL] Couldn‘t write to C:\User
    1、问题idea插件jrebel运行报错JRebel-JVMTI[FATAL]Couldn‘twritetoC:\Users\报错JRebel:Couldnotcreatelogfile:C:\Users\22611\.jrebel\jrebel.log(系......