POJ 1321 棋盘问题 DFS 期末前水一水就好……
Description
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 10
const int inf=0x7fffffff; //无限大
char board[maxn][maxn];
int place[maxn];
int n,k;
int cnt,num; void dfs(int i)
{
if(k==num)
{
cnt++;
return;
}
if(i>=n)
return;
for(int j=;j<n;j++)
{
if(!place[j]&&board[i][j]=='#')
{
place[j]=;
num++;
dfs(i+);
place[j]=;
num--;
}
}
dfs(i+);
}
int main()
{
while(cin>>n>>k)
{
if(n==-&&k==-)
break;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
cin>>board[i][j];
}
memset(place,,sizeof(place));
cnt=;
num=;
dfs();
cout<<cnt<<endl;
}
return ;
}
POJ 1321 棋盘问题 DFS 期末前水一水就好……的更多相关文章
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
- POJ 1321 棋盘问题 (DFS + 回溯)
题目链接:http://poj.org/problem?id=1321 题意:中文题目,就不多说了...... 思路: 解题方法挺多,刚开始想的是先从N行中选择出来含有“#”的K行,再在这K行中放置K ...
- POJ - 1321 棋盘问题 dfs分层搜索(n皇后变式)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47960 Accepted: 23210 Descriptio ...
- POJ 1321 棋盘问题 DFS搜索
简单搜索 练习一下回溯 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- DFS POJ 1321 棋盘问题
题目传送门 /* DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 */ #include <cstdio> #include <algori ...
- POJ 1321 棋盘问题(C)回溯
Emmm,我又来 POJ 了,这题感觉比上次做的简单点.类似皇后问题.但是稍微做了一点变形,比如棋子数量是不定的.棋盘形状不在是方形等等. 题目链接:POJ 1321 棋盘问题 解题思路 基本思路:从 ...
- poj 1321 棋盘问题 简单DFS
题目链接:http://poj.org/problem?id=1321 很久没有敲搜索了啊,今天敲了个水题练练手,哈哈.... 题目大意: 就是求在n*n的方格上放置k个棋子的方案数 代码: #inc ...
随机推荐
- juery下拉刷新,ajax请求,div加载更多元素(一)
;//设置当前页数 var flag=true; //滑动加载 $(function(){ var winH = $(window).height(); //页面可视区域高度 $(window).sc ...
- 使用postman做接口测试(三)
三,接口用例的设计 个人感觉用例的设计才是重要的哈,网上查了一些资料总结了一下 1.业务流程测试 通过性验证: 1, 按照接口文档上的参数,正常传参,是否可以返回正确的结果 2, 是否满足前提条件,比 ...
- centos7 部署镜像仓库 harbor
=============================================== 2018/4/16_第2次修改 ccb_warlock 更新 ...
- 30 C? Go? Cgo!
C? Go? Cgo! 17 March 2011 Introduction Cgo lets Go packages call C code. Given a Go source file writ ...
- 日期时间设置 "2018-05-04T16:36:23.6341371+08:00" 格式
using System;using System.Collections.Generic;using System.Globalization;using System.Text; namespac ...
- Filebeat入门
一.安装filebeat 简介 Beats 是安装在服务器上的数据中转代理. Beats 可以将数据直接传输到 Elasticsearch 或传输到 Logstash . Beats 有多种类型,可以 ...
- MySQL学习笔记:exists和in的区别
一.exists函数 表示存在,常常与子查询配合使用. 用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False. 当子查询返回为真时,则外层查询语句将进行 ...
- test.c
test.c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include < ...
- CentOS7 64位下MySQL5.7安装与配置(YUM)转
安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...
- mac上Homebrew安装以及python安装
Homebrew homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能. Homebrew常用命令 查看brew的帮助:brew –help 安装 ...