Description

  在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
 
  标准的回溯的问题,直接按行递归,就像八皇后的递归。
 
代码如下:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring> using namespace std; int n,k;
int map1[][];
int remi[],remj[];
int ans;
int cou; bool judge(int x,int y)
{
for(int i=;i<cou;++i)
if(remj[i]==y)
return ; return ;
} void dfs(int cur,int hang)
{
if(cur==k)
{
++ans;
return;
} for(int i=hang;i<=n;++i)
{
if(n-i+<k-cur)
break; for(int j=;j<=n;++j)
if(map1[i][j]&&judge(i,j))
{
remi[cou]=i;
remj[cou++]=j;
dfs(cur+,i+);
--cou;
}
}
} int main()
{
char c,s[]; ios::sync_with_stdio(false); for(cin>>n>>k;n!=-&&k!=-;cin>>n>>k)
{
ans=;
cou=;
for(int i=;i<=n;++i)
{
cin>>s; for(int j=;j<=n;++j)
{
c=s[j-];
if(c=='#')
map1[i][j]=;
else
map1[i][j]=;
}
} dfs(,); cout<<ans<<endl;
} return ;
}

(简单) POJ 1321 棋盘问题,回溯。的更多相关文章

  1. POJ 1321 棋盘问题(C)回溯

    Emmm,我又来 POJ 了,这题感觉比上次做的简单点.类似皇后问题.但是稍微做了一点变形,比如棋子数量是不定的.棋盘形状不在是方形等等. 题目链接:POJ 1321 棋盘问题 解题思路 基本思路:从 ...

  2. POJ 1321 棋盘问题 --- DFS

    POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...

  3. DFS POJ 1321 棋盘问题

    题目传送门 /* DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 */ #include <cstdio> #include <algori ...

  4. POJ 1321 棋盘问题(DFS板子题,简单搜索练习)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44012   Accepted: 21375 Descriptio ...

  5. Poj 1321 棋盘问题 【回溯、类N皇后】

    id=1321" target="_blank">棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  6. poj 1321 棋盘问题 简单DFS

    题目链接:http://poj.org/problem?id=1321 很久没有敲搜索了啊,今天敲了个水题练练手,哈哈.... 题目大意: 就是求在n*n的方格上放置k个棋子的方案数 代码: #inc ...

  7. POJ 1321 棋盘问题【DFS/回溯/放与不放/类似n皇后】

    棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62164 Accepted: 29754 Description 在一 ...

  8. POJ 1321 棋盘问题 (DFS + 回溯)

    题目链接:http://poj.org/problem?id=1321 题意:中文题目,就不多说了...... 思路: 解题方法挺多,刚开始想的是先从N行中选择出来含有“#”的K行,再在这K行中放置K ...

  9. poj 1321 棋盘问题 (回溯法)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69951   Accepted: 33143 Descriptio ...

随机推荐

  1. wpf无法加载程序集的元数据 可能已从web下载

    wpf无法加载程序集的元数据 可能已从web下载     别人写的wpf程序,源代码放到自己电脑上,设计器加载出现问题:无法加载程序集“XXX”的元数据. 解除锁定步骤:找到你的dll->右键属 ...

  2. Anton and School

    Anton and School time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Hibernate 系列教程2-创建maven工程

    第1步:通过eclipse新建1个java maven项目. 选择file–>new–>other–>MAVEN PROJECT选项 第2步:New Maven project 选择 ...

  4. 实测switch支持的参数类型

    @Test public void testSwitch() { switch (2) { case 1: System.out.println("int型:" + 1); bre ...

  5. hdu 4463 Outlets(最小生成树)

    Outlets Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submi ...

  6. Cloudsim 3.0在myclipse下的安装过程

    (1)下载cloudsim 3.0: http://code.google.com/p/cloudsim/downloads/list (2)下载flanaga.jar包  下载地址:http://w ...

  7. 转:SQL Case when 的使用方法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  8. .Net多线程编程—Parallel LINQ、线程池

    Parallel LINQ 1 System.Linq.ParallelEnumerable 重要方法概览: 1)public static ParallelQuery<TSource> ...

  9. 英文SEO外部链接资源收集之常用的footprints

      inurl:/privacy-policy "Using Article Directory plugin"inurl:/terms "Using Article D ...

  10. Centos6.5安装与配置Tomcat-8的方法

    环境要求: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bs ...