POJ 1321 棋盘问题 (dfs)
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<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; char chess[][];
long long res;
int n,K; bool judge(int x,int y)
{
for(int i=;i<x;i++)
if(chess[i][y]=='C')
return false;
for(int j=;j<y;j++)
if(chess[x][j]=='C')
return false;
return true;
} void dfs(int p,int k)//p表示棋盘中第几个位置(二维转一维),k表示第几个棋子
{
if(k==K)
{
res++;
return;
}
else if(p==n*n)
return;
else
{
int i,j;
i=p/n;
j=p%n;
if(chess[i][j]=='#'&&judge(i,j))
{
chess[i][j]='C';
dfs(p+,k+);
chess[i][j]='#';
}
dfs(p+,k);
} } int main()
{ while(~scanf("%d%d",&n,&K))
{
if(n==-&&K==-)
break;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
cin>>chess[i][j];
res=;
dfs(,);
printf("%lld\n",res); }
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 期末前水一水就好……
A - 棋盘问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 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 Time Limit: 1000MS Memory Limit: 10000K Description 在一个给定形状的棋盘(形 ...
随机推荐
- UI BOL 练习 get value set attr
" " " " "**********************change list************************* "2 ...
- Git Diff 格式分析
参考: http://stackoverflow.com/questions/2529441/how-to-read-the-output-from-git-diff https://www.git- ...
- stund客户端使用结果说明
stun服务器是用于检测网络类型的重要工具. 源码地址:https://svwh.dl.sourceforge.net/project/stun/stun/0.97/stund-0.97.tgz 或者 ...
- 搜索路径---PYTHONPATH 变量
- 基本git指令
--git包含命令行界面和图形化界面 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git config -- global user.name '你再github上注册的用户名' ...
- 六招让你的Ubuntu马上提速
Chris Hoffman Ubuntu的启动速度非常快,按了开机键之后很快就进入桌面.但我们仍然可以充分利用内存,通过多种方法让开机速度更快.某些方法真的可以提速,对于旧电脑的效果尤其明显. 选用轻 ...
- learning ddr tRP and tRP tRTP CL tRAS
referce :https://blog.csdn.net/ghostyu/article/details/7728106 tRP(RAS Precharge Time): “内存行地址控制器预充电 ...
- angular 路由项目例子
angular 路由是我在工作中体验非常便捷的一点, 这是详细的API ,查看API 可以了解很多东西, https://github.com/angular-ui/ui-router/wiki/Qu ...
- ural1517
题解: 后缀数组 求一下最长公共字串 代码: #include<cstdio> #include<cmath> #include<algorithm> #inclu ...
- day13-类的重写和类的私有方法
类的重写 在python中 有时需要进行重写,重写是继承机制中的一个重要部分, 可以重写一般方法也可以重写构造方法,构造方法是用来初始化新创建对象的状态. class A : def hello(se ...