poj 1321 棋盘问题【dfs】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 28308 | Accepted: 13996 |
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<stdio.h>
#include<string.h>
char map[10][10];
int n,k,tot,max;
int judge(int r,int c)
{
int i;
if(map[r][c]!='#') return 0;
for(i=r-1;i>=0;i--)//判断当前点同一列上,上侧是否已经放置
{
if(map[i][c]=='o')
return 0;
if(map[i][c]=='.')
continue;
}
for(i=c-1;i>=0;i--)//判断当前点同一行上左侧是否已经放置
{
if(map[r][i]!='.')
continue;
if(map[r][i]=='o')
return 0;
}
return 1;
}
void dfs(int x,int tot)
{
int i,j;
int t;
if(tot==k)
max++;
else if(x==n) return;
else
{
for(i=0;i<n;i++)
{
if(judge(x,i))
{
map[x][i]='o';//标记已经放过的位置
dfs(x+1,tot+1);//搜索下一行
map[x][i]='#';//如果上个搜索失败,或者搜索到了结尾
} //回溯时取消标记
}
dfs(x+1,tot);//如果上一行没有可以放得位置则开始搜索下一行
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
max=0;
dfs(0,0);
printf("%d\n",max);
}
return 0;
}
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 在一个给定形状的棋盘(形 ...
随机推荐
- html-----003
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C# 执行批处理文件(*.bat)的方法代码
代码如下: static void Main(string[] args){ Process proc = null; try { st ...
- oracle查看用户信息
1.查看所有用户:select * from dba_users; select * from all_users; select * from user_users;2.查看用户或角色系统权限(直接 ...
- jquery mobile基本结构搭建
官网:http://jquerymobile.com/ 基本结构:
- 博客系统-3.0CodeIgniter系统SAE版本的配置 application/config/
autoload.php(系统启动时自动加载的文件:包,类库,驱动,方法助手,配置) $autoload['libraries'] = array('database', 'access', 'pag ...
- thinkphp使用问题
下面总结一些,我在使用中遇到的问题,以后遇到了再补充 一.<a>标签的跳转问题 问题:我在控制器Home/Index/index里面使用了Public里面的index.html模板,ind ...
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
转自:http://blog.csdn.net/chuanchuan608/article/details/17915959 目前正在学习python,使用的工具为python3.2.3.发现3x版本 ...
- Quartz1.8.5例子(二)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- MySQL驱动阅读------Connection连接的建立,基于JDBC-----5.1.26
一般获取数据库连接的程序 Class.forName("com.mysql.jdbc.Driver"); final Connection connection = (Connec ...
- 0x1L
整形常量的后缀表示其类型,包括如下后缀,其中U和L的大小写通用 后缀 类型L long int LL long long int U ...