2016HUAS暑假集训题1 H - N皇后问题
Description
你的任务是,对于给定的N,求出有多少种合法的放置方法。
Input
Output
Sample Input
Sample Output
#include<stdio.h>
#include<string.h> int n,tmp;
int map[11]; void DFS(int k)
{
int i,j,flag;
if(k==n+1)//递归边界,走到这里皇后不会冲突
{
tmp++;
return;
}
else
{
for(i=1;i<=n;++i)
{
map[k]=i; //把第i行的皇后放到第i列
flag=1;
for(j=1;j<k;++j) // 检查是否个前面的皇后冲突
{
if(map[j]==i||i-k==map[j]-j||i+k==map[j]+j)
{
flag=0;
break;
}
}
if(flag) //如果合法 则继续递归
DFS(k+1);
}
}
} int main()
{
int i,m;
int ans[11];
for(n=1;n<=10;++n)
{
tmp=0;
DFS(1);
ans[n]=tmp;
}
while(scanf("%d",&m),m)
{
printf("%d\n",ans[m]);
}
return 0;
}
2016HUAS暑假集训题1 H - N皇后问题的更多相关文章
- 2016HUAS暑假集训题1 A-士兵队列训练问题
A - 士兵队列训练问题 Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从 ...
- 2016HUAS暑假集训题1 J - 迷宫问题
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- 2016huas暑假集训训练题 G-Who's in the Middle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数 刚开始以为是按数学中的 ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- 2016HUAS暑假集训训练题 F - 简单计算器
Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...
- POJ3660 暑假集训-最短路H题floyd
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#rank#include<iostream> #include& ...
- 2016HUAS暑假集训训练题 E - Rails
There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- 2016HUAS暑假集训训练题 D - Find a way
F ...
随机推荐
- background为圆角的表框,dp转Px,Px转dp
圆角边框<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="ht ...
- windows加入path路径
右键我的电脑,属性:高级系统设置,高级,环境变量:在系统变量中选path,编辑:将python安装路径加入即可(注意分号):
- linux安装配置SVN并设置钩子
安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 #检查是否安装了低版本的SVN 1 rpm ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- DSP using MATLAB 示例Example2.11
上代码: b = [1]; a = [1, -1, 0.9]; n = [-20:120]; h = impz(b,a,n); set(gcf,'Color','white'); %subplot(2 ...
- .Net 连接Oracle 数据库写法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- HDU5726 GCD(二分 + ST表)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...
- React版本修改内容
React新版本(0.12.2)在2014年12月18日发布,对比了我之前用的v0.11.2版本改动很大,基本的写法被扩展,让我顿时感觉自己又要重新开始.坑啊~ 事已至此,必须重新适应,首先我们来看看 ...
- sql跨电脑导数据
启用Ad Hoc Distributed Queries: reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigu ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...