在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n

当为-1 -1时表示输入结束。

随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1
乍一看和八皇后类似,但其实不太一样。和网上思路不太一样,这个题其实可以看成是:把相同的棋子以某种方式放进给定的格点里,求放入方式,可以转化成求全排列+判定,用DFS搞过。(全排列暴搜参考蓝书相关代码)。
跑了922ms,在T的边缘疯狂试探。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int n,k;
char mmap[][];
struct node
{
int x;
int y;
}nod[];
bool vis[];
vector<node>v;
int ans=;
void dfs(int x,int cnt)//x表示选的序号 cnt表示已经用的棋子 //从vector里选出m个符合要求的位置
{
if(cnt>k||(cnt+v.size()-x)<k)
{
return;
}
if(x==v.size())
{
ans++;
return;
}
//不选x
dfs(x+,cnt);
//选x
int i;
bool flag=;
for(i=;i<x;i++)
{
if(vis[i]&&i!=x&&(v[i].x==v[x].x||v[i].y==v[x].y))//判断是否同行同列
{
flag=;
break;
}
}
if(flag)
{
vis[x]=;
dfs(x+,cnt+);
vis[x]=;
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF&&n!=-&&k!=-)
{
memset(mmap,,sizeof(mmap));
memset(vis,,sizeof(vis));
ans=;
int i,j;
for(i=;i<n;i++)
{
scanf("%s",mmap[i]);
}
v.clear();
node non;
non.x=;
non.y=;
v.push_back(non);//为了使vector下标从1开始,放一个空的占位
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(mmap[i][j]=='#')//满足条件的格点放入vector
{
node temp;
temp.x=i;
temp.y=j;
v.push_back(temp);
}
}
}
dfs(,);//暴搜
cout<<ans<<endl;
}
}
												

POJ1321棋盘问题(暴搜)的更多相关文章

  1. 模拟 + 暴搜 --- Help Me with the Game

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3175   Accepted: ...

  2. 【BZOJ-3033】太鼓达人 欧拉图 + 暴搜

    3033: 太鼓达人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 154[Submit][Status][Discuss] ...

  3. c++20701除法(刘汝佳1、2册第七章,暴搜解决)

    20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有 ...

  4. Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜

    题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...

  5. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  6. Sicily1317-Sudoku-位运算暴搜

    最终代码地址:https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1317.c 这题博主刷了1天,不是为了做出来,AC ...

  7. codeforces 339C Xenia and Weights(dp或暴搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Weights Xenia has a set of weig ...

  8. Usaco 2.3 Zero Sums(回溯DFS)--暴搜

    Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...

  9. HDU4403(暴搜)

    A very hard Aoshu problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

随机推荐

  1. Go操作influxDB

      influxDB 安装 下载 https://portal.influxdata.com/downloads/ 这里需要注意因为这个网站引用了google的api所以国内点页面的按钮是没反应的,怎 ...

  2. 【PAT甲级】1113 Integer Set Partition (25分)

    题意: 输入一个正整数N(2<=N<=1e5),接着输入N个正整数,将这些数字划分为两个不相交的集合,使得他们的元素个数差绝对值最小且元素和差绝对值最大. AAAAAccepted cod ...

  3. AC3 Rematrix

    当L R channel highly correlated时,AC3 encoder 使用rematrix技术压缩L/R的和和差. 原始信号为left,right,使用rematrix压缩信号为le ...

  4. Can't bind to 'ngModel' since it isn't a known property of 'input'.

    angular项目启动报错 Can't bind to 'ngModel' since it isn't a known property of 'input'. 原因:当前module模块未引入 ' ...

  5. spring jdbcTemplate query 返回值为null

    spring jdbcTemplate query 返回值为null 今天使用以下方法从数据库中查询数据,返回列表 public List<BookBean> getBooks(){ St ...

  6. 【音乐欣赏】《Running Away》 - Taska Black / DROELOE

    曲名:Runing Away 作者:Taska Black .DROELOE [00:00.000] Running with the speed of light [00:03.081] Illum ...

  7. RHEL7安装ZABBIX 3.2

    参考并结合: http://blog.sina.com.cn/s/blog_560130f20101bfou.html http://blog.itpub.net/20893244/viewspace ...

  8. String - 字符串分割操作

    如果我想将一个字符串按照每8位一组分为若干个块,然后存储在一个byte[ ]数组中,我首先需要确定这个byte数组的长度,但由于我无法确定这个字符串的长度是否可以被8整除,所以无法直接判断,因此需要对 ...

  9. Mac上通过docker配置PHP开发环境

    这篇文章介绍的内容是关于Mac上通过docker配置PHP开发环境,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 更多PHP相关知识请关注我的专栏PHP​zhuanlan.zhihu. ...

  10. 创建Maven project 提示pom.xml 首行错误

    背景 使用eclipse创建Maven SpringBoot 2.2.0 项目时报错,更换springboot 版本也不行,排除框架依赖原因.然后别人的eclipse创建的同样2.2.2 maven项 ...