棋盘问题(dfs)
http://poj.org/problem?id=1321
思路:按行搜索,回溯时还原棋盘。
#include <stdio.h>
#include <string.h>
int map[][],vis[];
int ans,n,k;
void dfs(int row,int cnt)
{ if (k==cnt)
{
ans++;
return ;
}
if (row > n)
return ;
for (int i = ; i <= n; i ++)
{
if (map[row][i] && !vis[i])
{
vis[i] = ;
dfs(row+,cnt+);
vis[i] = ;
}
}
dfs(row+,cnt);//处理k<n的情况
return ;
}
int main()
{
while(~scanf("%d%d%*c",&n,&k))
{
if (n==-&&k==-)
break;
char ch;
ans = ;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for (int i = ; i <= n; i ++)
{
for (int j = ; j <= n; j ++)
{
scanf("%c",&ch);
if (ch=='#')
map[i][j] = ;
}
getchar();
}
dfs(,);
printf("%d\n",ans);
}
return ;
}
棋盘问题(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 期末前水一水就好……
A - 棋盘问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 2015 百度之星 1003 棋盘占领 dfs
棋盘占领 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_show ...
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
题目描述 小Q正在设计一种棋类游戏.在小Q设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的 ...
- (比赛)B - 棋盘问题(dfs)
B - 棋盘问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Practice POJ ...
- POJ - 1321 棋盘问题 dfs分层搜索(n皇后变式)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47960 Accepted: 23210 Descriptio ...
- POJ1321 棋盘问题 —— DFS回溯
题目链接:http://poj.org/problem?id=1321 棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ 1321 棋盘问题 DFS搜索
简单搜索 练习一下回溯 #include <iostream> #include <cstdio> #include <cstring> #include < ...
随机推荐
- THREE.js代码备份——canvas - geometry - earth(球体贴纹理)
<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - g ...
- HDU_1085_Holding Bin-Laden Captive!_母函数
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- SYN(synchronous)TCP/IP
SYN(synchronous)是TCP/IP建立连接时使用的握手信号.在客户机和服务器之间建立正常的TCP网络连接时,客户机首先发出一个SYN消息,服务器使用SYN+ACK应答表示接收到了这个消息, ...
- 解决Mysql Workbench的Error Code: 1175错误
错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE ...
- flutter 环境搭建
环境: ladder什么的是必不可少的 win10 + Idea 2019.1.13 + Genymotion 2.12 基本可以在模拟器中运行项目,还有些许小问题,但是可以看到效果了 基本流程 下载 ...
- 删数据ORA-02292主键约束问题
通常在删除某个表A的时候,会出现这个错误.原因是另一个表B的某个字段引用了A表的某个字段作为约束(这个的另一个说法是外键). 假如引用的字段叫field,当B.field = A.field , 而你 ...
- Python 函数 day3
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...
- CentOS7搭建KMS服务器
使用vlmcsd搭建KMS服务器 1.下载vlmcsd: wget https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries ...
- 51nod1049 最大子段和【动态规划】
N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的连续子段和的最大值.当所给的整数均为负数时和为0. 例如:-2,11,-4,13,-5,- ...
- PAT 1114 Family Property
This time, you are supposed to help us collect the data for family-owned property. Given each person ...