HUAS Summer Trainning #3~K

Description

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
当为-1 -1时表示输入结束。 
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1解题思路:这种题目最好用DFS解题,它是通过递归调用来实现自己的。用数组进行标记时最好将数组的声明放到主函数外面,因为C语言的全局变量在没有赋值以前默认为0,所以放到外面会更加方便。程序代码:
#include<cstdio>
#include <iostream>
#include<cstring>
using namespace std;
int n,k,ans;
char map[12][12];
int vis[12];
int dfs(int i,int cur)
{
	if(cur>=k)
	{
		ans++;
		return 0;
	}
	int a,b;
	for(a=i;a<n;a++)
	{
		for(b=0;b<n;b++)
		{
			if(!vis[b] && map[a][b]==‘#‘)
			{
				vis[b]=1;//标记
				dfs(a+1,cur+1);//递归
				vis[b]=0;
			}
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d%d",&n,&k)&&n!=-1)
	{
		ans=0;
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
			scanf("%s",map[i]);
		dfs(0,0);
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-06 04:46:32

HUAS Summer Trainning #3~K的相关文章

2015 HUAS Summer Trainning #4 B

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of

2015 HUAS Summer Trainning #4 C

My birthday is coming up and traditionally I’mserving pie. Not just one pie, no, I have a numberN of them, of various tastes and of various sizes. Fof my friends are coming to my party and each ofthem gets a piece of pie. This should be one pieceof o

HUAS Summer Trainning #3~L

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

2015 HUAS Summer Trainning #5~J

Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regul

HUAS Summer Trainning #3 B

题目: Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of themaximum positive product involving consecutive terms of S. If you cannot find a positive sequence,you should consider 0 as the value of the maximu

2015 HUAS Summer Trainning #5~A

Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains an inte

2015 HUAS Summer Trainning #6~G

Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into

HUAS Summer Trainning #3~E

Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases: They are equal. If we split string a into two halves of the same siz

2015 HUAS Summer Trainning #6~H

Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000). 接下来共T行,每行输入两个整数L,R(1<= L <= R <= 10