POJ—1321(棋盘问题)
题目链接:https://cn.vjudge.net/contest/65959#problem/A
入门dfs,给一张地图,由“#”和“.”组成,“#”处可以放棋子,且棋子不能同行同列,问放满所有“#”的方案数直接dfs ,dfs的变量是行数和已经放置的棋子数,因为一行只能放一个棋子,所以只需用bool数组判断该列是否放置了棋子。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
using namespace std;
char s[][];
int n,k,ans=,tot=;
bool vis[];
void dfs(int row)
{
if(tot>=k) {
ans++;return ;
}
if(row>n) return ;
for(int i=;i<=n;i++) {
if(s[row][i]=='#'&&!vis[i]) {
tot++;vis[i]=true;
dfs(row+);vis[i]=false;tot--;
}
}
dfs(row+);
}
int main()
{
while(~scanf("%d %d",&n,&k)!=EOF)
{
ans=;
memset(vis,false,sizeof(vis));
if(n==-&&n==k) break;
for(int i=;i<=n;i++) {
for(int j=;j<=n;j++) {
cin>>s[i][j];
}
}
dfs();
cout<<ans<<endl;
}
return ;
}
POJ—1321(棋盘问题)的更多相关文章
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...
- DFS POJ 1321 棋盘问题
题目传送门 /* DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 */ #include <cstdio> #include <algori ...
- POJ 1321 棋盘问题(C)回溯
Emmm,我又来 POJ 了,这题感觉比上次做的简单点.类似皇后问题.但是稍微做了一点变形,比如棋子数量是不定的.棋盘形状不在是方形等等. 题目链接:POJ 1321 棋盘问题 解题思路 基本思路:从 ...
- OpenJudge/Poj 1321 棋盘问题
1.链接地址: http://bailian.openjudge.cn/practice/1321 http://poj.org/problem?id=1321 2.题目: 棋盘问题 Time Lim ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- POJ 1321 - 棋盘问题 - [经典DFS]
题目链接:http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...
- poj 1321 棋盘问题 递归运算
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19935 Accepted: 9933 Description ...
- poj 1321 棋盘问题 简单DFS
题目链接:http://poj.org/problem?id=1321 很久没有敲搜索了啊,今天敲了个水题练练手,哈哈.... 题目大意: 就是求在n*n的方格上放置k个棋子的方案数 代码: #inc ...
- POJ 1321 棋盘问题(状态压缩DP)
不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
随机推荐
- Winform 实现跨线程以及Timer
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.exited?redirectedfrom=MSDN&am ...
- 各平台操作系统查询主机WWPN
查询主机WWPN 目录 3.4.3.8.2.3 查询主机WWPN 3.4.3.8.2.3.1 查看主机HBA相应端口的WWPN(Windows) 3.4.3.8.2.3.2 查看主机HBA相应端口的W ...
- row_number() over (partition by order by)的用法
原表为: 一.分区函数Partition By的与row_number()的用法 1.不分班按学生成绩排名 select *,row_number() over(order by Score desc ...
- redis 安装与使用
到git 官网上下载redis: DownLoad,选择.zip 压缩包 2. 下载后,对zip 包进行解压,解压后如下: 3. 配置并安装redis, 打开window dos 窗口,将目录切换到解 ...
- android 手机权限管理——PermissionsDispatcher
Android6.0 之后某些权限需要动态申请,相比于之前版本复杂了许多.不过已经有大神给我们写好了框架(PermissionsDispatcher),我们用起来还是很方便. 1.添加引用 根据 gr ...
- start to use await
import { browser, $, $$, by, element, protractor } from "protractor"; //import { SearchPag ...
- C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
#region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- SQL SERVER 死锁
sp_lock 查看锁表名称 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefr ...
- Java学习随笔(2)--爬虫--天气预报
public class Spiderweather { public static void main(String[] args) { List<String> list = null ...