【wikioi】1295 N皇后问题
算法:DFS
刚开始卡了我一下,我竟然傻到用二维来放皇后= =。导致一直TLE。。。。
其实用1维就行了的,下标为行(列),值为列(行)
我是用下标为列做的。
上代码
#include <iostream> using namespace std;
int n, ans = 0;
int map[14];
void dfs(int x)
{
if(x > n) {ans++; return;}
int i, j;
for(i = 1; i <= n; i++) //放在某行
{
map[x] = i;
for(j = 1; j < x; j++) //判断前面列是否有重合,直接判断横行 和 斜行 (可自己画图为什么判断斜行成立)
if((map[j] == map[x]) ||
(x-map[x] == j-map[j] || x+map[x] == j+map[j]))
break;
if(j == x)
dfs(x+1);
}
} int main()
{
cin >> n;
dfs(1);
cout << ans;
return 0;
}
【wikioi】1295 N皇后问题的更多相关文章
- 1295 N皇后问题
题目描述 Description 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个皇后,任 ...
- 递归实现n(经典的8皇后问题)皇后的问题
问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...
- 八皇后算法的另一种实现(c#版本)
八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- N皇后问题—初级回溯
N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...
- 数据结构0103汉诺塔&八皇后
主要是从汉诺塔及八皇后问题体会递归算法. 汉诺塔: #include <stdio.h> void move(int n, char x,char y, char z){ if(1==n) ...
- N皇后问题
题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...
- LeetCode:N-Queens I II(n皇后问题)
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
随机推荐
- linux zookeeper 原理详解
http://cailin.iteye.com/blog/2014486 直接打开此链接即可 -------------------------------------------------- ...
- shell脚本检测局域网内存活主机
<1> d211 admin # for i in {3..254} ; do ping -c 1 192.168.1.$i &>/dev/null && e ...
- php数组转换js数组操作及json_encode应用
对于php,个人感觉能够熟练操作数组和字符串,基本上已经是入门了,php本身有很多操作数组和字符串的函数,今天在做一个功能时,需要用Js动态的创建门店信息,这些信息是要从后台添加的,想来想去,通过ph ...
- In-App Purchases验证
package com.demo.controller.web.app; import java.io.BufferedOutputStream; import java.io.BufferedRea ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- Android 中的code sign
Android 中和ios中都有code sign.它们的目的一样,都是要保证程序的可靠性,最基本实现原理也一样.但是sign的过程比较不同. 下面记录一点Android sign的重要知识. 请参看 ...
- apache AllowEncodedSlashes 允许URL中对路径分隔符进行编码
2013年11月29日 10:35:32 情景: 你想通过在当前的URL中记录来源页面的URL,以便处理完请求后再跳转回来源页: http://www.example1.com/refer/http: ...
- Java for LeetCode 040 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for LeetCode 079 Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...