poj1321 基础DFS

最近两天在写搜索专题

题意:中文题就不用讲题意

思路:由于每行每列只允许最多一个棋子,那么我们可以从第一行开始往下搜索,并标记列,如果这列放了就不放,否则就放,并向下层搜索,这样可以不用标记行了。

#include<stdio.h>
#include<string.h>
int n,sum,m;
const int maxn = 10;
char map[maxn][maxn];
int visc[maxn];

void DFS(int r,int k)
{
	if(k == m)
	{
		sum ++;
		return ;
	}
	if(r >= n)return ;
	for(int i =0;i<n;i++)
	{
		if(!visc[i] && map[r][i] == ‘#‘)
		{
			visc[i] = 1;
			DFS(r+1,k+1);
			visc[i] = 0;
		}
	}
	DFS(r+1,k);
}
int main()
{
	while(scanf("%d%d",&n,&m),n+1+m+1)
	{
		sum = 0;
		memset(visc,0,sizeof(visc));
		for(int i=0;i<n;i++)
		{
			scanf("%s",map[i]);
		}
		DFS(0,0);
		printf("%d\n",sum);
	}
	return 0;
}

poj1321 基础DFS,布布扣,bubuko.com

时间: 2024-07-31 14:34:47

poj1321 基础DFS的相关文章

poj 2676 Sudoku (基础DFS)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15456   Accepted: 7574   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

Poj3620--Avoid The Lakes(基础Dfs)

Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6908   Accepted: 3680 Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of wate

棋盘问题---poj1321(dfs)

http://poj.org/problem?id=1321 由于搜索是原来写的,而集训的时候没来所以只能现在补补咯-_- 简单的深搜 #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> #define N 10 #define INF 0xfffffff #include<math.h> #include<iostream> usin

题目--oil Deposits(油田) 基础DFS(深度搜索)

上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--depth first search,话不多说,直接上题了解: Description:某石油勘探公司正在按计划勘探地下油田资源,工作在一片长方形的地域中.他们首先将该地域划分为许多小正方形区域,然后使用探测设备分别探测每一块小正方形区域内是否有油.若在一块小正方形区域中探测到有油,则标记为’@’,否则标

POJ2488 dfs

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41972   Accepted: 14286 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

HDU 1241 DFS

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31745    Accepted Submission(s): 18440 Problem Description The GeoSurvComp geologic survey company is responsible for detecting under

poj1154 【DFS】

LETTERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8976   Accepted: 4017 Description A single-player game is played on a rectangular board divided in R rows and C columns. There is a single uppercase letter (A-Z) written in every po

2017.5.3 4.全排列

题目描述 给定N(N<10),按照字典序输出所有的N排列. 输入 第一行输入N. 样例输入 3 输出 输出1到N的全排列,一行一个排列,按照字典序顺序输出. 样例输出 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 基础DFS题 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 int n; 6 bool visit[20]; 7 int list[20]; 8 9

bzoj1621 / P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm

P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 基础dfs,按题意递归即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define re register 5 using namespace std; 6 int n,k,ans; 7 void dfs(int x){ 8 if(x<=k||((x-k)&1)){//不能有半