深度优先遍历(dfs)-leetcode51:经典的八皇后问题

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
在这里插入图片描述
Given an integer n, return all distinct solutions to the n-queens puzzle.

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:

Input: 4
Output: [
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

简单说一下题目大概意思:先给出一个数字n,在n*n大小的棋盘上摆放n颗棋子,摆放的要求不仅是每行、每列不能同时有两枚棋子存在;还要求每条正斜线、反斜线上也不能同时有两枚棋子同时存在。这题很容易想到n有多大就搞多少重循环,把每行的每个位置遍历一下,暴力求解所有符合要求的情况。这显然是不合适的,当n为4时还可以搞个四重循环,当n为8时或者更大就不合适了。多重循环不行,就试着用递归。本题需要用到回溯法,差不多等于深度优先遍历算法。算法的伪代码如下:

void dfs(int v)
	visited[v] = true
	for(v 的每一个相邻点w)
		if(!visited[w])//如果没有被访问过
			dfs(w)

最后贴一下本题的代码:

package com.huawei;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wc 八皇后问题
 */
public class subject051 {
	class Solution {
		private List<List<String>> ans = new ArrayList<List<String>>();
		private boolean[][] v = null;
		private boolean[] vRow = null;// 记录每行的状态,该行是否放置皇后
		private boolean[] vCol = null;// 记录每列的状态,该列是否放置皇后
		private boolean[] angle1 = null;// 记录斜线/的状态,该/上是否放置皇后

		private int n;

		public List<List<String>> solveNQueens(int n) {
			this.n = n;
			v = new boolean[n][n];
			vRow = new boolean[n];
			vCol = new boolean[n];
			angle1 = new boolean[2 * n - 1];// 下标为x+y
			dfs(0);
			return ans;
		}

		// 判断该/上是否占有皇后
		private boolean isLegal(int x, int y) {
			int x0 = x, y0 = y;
			while (x >= 0 && y >= 0) {
				if (v[x][y]) {// 这条斜线上已经被占用
					return false;
				}
				x--;
				y--;
			}
			while (x0 < n && y0 < n) {
				if (v[x0][y0]) {
					return false;
				}
				x0++;
				y0++;
			}
			return true;
		}

		/**
		 * 当index+1行没有合适的位置放皇后时,回溯到index行尝试下一个位置放皇后
		 *
		 * @param row 对行row+1安放皇后
		 */
		private void dfs(int row) {
			if (row >= n) {	// n颗龙珠已经集齐
				ArrayList<String> list = new ArrayList<String>();
				for (int i = 0; i < n; i++) {
					StringBuffer sb = new StringBuffer();
					for (int j = 0; j < n; j++) {
						if (v[i][j]) {
							sb.append("Q\t");
						} else {
							sb.append(".\t");
						}
					}
					list.add(sb.toString());
				}
				ans.add(list);
				return;
			}
			for (int col = 0; col < n; col++) {
				if (!v[row][col] && !vRow[row] && !vCol[col] && !angle1[row + col] && isLegal(row, col)) {
					v[row][col] = true;
					vRow[row] = true;
					vCol[col] = true;
					angle1[row + col] = true;
					dfs(row + 1);
					angle1[row + col] = false;
					vCol[col] = false;
					vRow[row] = false;
					v[row][col] = false;
				}
			}
		}
	}

	public static void main(String[] args) {
		subject051 subject = new subject051();
		Solution s = subject.new Solution();
		long t1 = System.currentTimeMillis();
		List<List<String>> lists = s.solveNQueens(14);
		long t2 = System.currentTimeMillis();
		for (List<String> list : lists) {
			for (String str : list) {
				System.out.println(str);
			}
			System.out.println();
		}
		System.out.println(t2-t1);
	}
}```

本代码在leetcode平台上是可以AC的,笔者的电脑(i7:9700;16g内存;-Xms800m -Xmx1280m)可以执行n=14,话费时间4536ms,当n=15时eclipse报内存溢出,后来调高xms,Xms也不行,后续可以接着优化代码,以能够跑更大的n。
```java
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.StringBuffer.toString(StringBuffer.java:669)
	at no51_100.subject051$Solution.dfs(subject051.java:59)
	at no51_100.subject051$Solution.dfs(subject051.java:70)
	at no51_100.subject051$Solution.dfs(subject051.java:70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东心十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值