棋盘问题

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33272 Accepted: 16456
Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放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


代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
char pos[10][10];
int vis[9];
int ans;
int n,k,m,per;
void dfs(int col,int cnt)
{
if(cnt==k)
{
ans++;
return ;
}
if(col>=n)
return ;
for (int j=0; j<n; j++)
{
if(pos[col][j]=='#'&&vis[j]==0)//搜索当前行
{
vis[j]=1;//可放置标记
dfs(col+1,cnt+1);//试探下一行
vis[j]=0;//试探结束
}
}
dfs(col+1,cnt);//此行未找到,搜索下一行
return ;
}
int main(void)
{
int i,j,r,s;
int col[10];
while (~scanf("%d%d",&n,&k)&&n!=-1&&k!=-1)
{
memset(pos,0,sizeof(pos));
memset(vis,0,sizeof(vis));
memset(col,0,sizeof(col));
for (i=0; i<n; i++)
{
scanf("%s",pos[i]);
}
ans=0;
dfs(0,0);
cout<<ans<<endl;
}
return 0;
}

POJ——1321棋盘问题(DFS+回溯)的更多相关文章

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

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

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

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

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

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

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

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

  5. (简单) POJ 1321 棋盘问题,回溯。

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

  6. POJ 1321 棋盘问题 dfs 难度:0

    http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...

  7. POJ 1321 棋盘问题 DFS搜索

    简单搜索 练习一下回溯 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. POJ 1321 棋盘问题 DFS 期末前水一水就好……

    A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  9. POJ - 1321 棋盘问题 dfs分层搜索(n皇后变式)

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

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

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

随机推荐

  1. python基础教程总结11——图形用户界面GUI

    1. 丰富的平台 工具包 描述 Tkinter 使用Tk平台.很容易得到.半标准. wxpython 基于wxWindows.跨平台越来越流行. PythonWin 只能在Windows上使用.使用了 ...

  2. 2018.4.18 Ubuntu 的telnet命令详解

    Ubuntu 的telnet命令详解 1.作用用途 Telnet 命令通常用来远程登录,Telnet 程序是基于 Telnet 协议的远程登录客户端程序.Telnet 协议是TCP/IP协议族中的一员 ...

  3. 2018.3.16 win 关闭自动更新代码

    新建一个文本文件修改后缀名为 .bat 格式 net stop wuauserv sc config wuauserv start=disabled shutdown -r -t 1

  4. sklearn 学习之分类树

    概要 基于 sklearn 包自带的 iris 数据集,了解一下分类树的各种参数设置以及代表的意义.   iris 数据集介绍 iris 数据集包含 150 个样本,对应数据集的每行数据,每行数据包含 ...

  5. IDE spec for registry settings

    IDE spec for registry settings Advanced customization of Visual Assist is possible with registry set ...

  6. linux readahead

    blockdev --getra /dev/sda blockdev --setra 2048 /dev/sda 必须将其写入配置文件/etc/rc.local,否则重启就会失效.[root@loca ...

  7. 01_3_查询指定id的单个对象

    01_3_查询指定id的单个对象 1. 映射文件配置如下信息 <select id="selectStudentById" resultClass="Student ...

  8. NOIP模拟赛 高级打字机

    [题目描述] 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x. ...

  9. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

  10. release判断系统

    #!/bin/bash # Name: Atomic Archive configuration script # Copyright Atomicorp, 2002-2018 # License: ...