poj 1321碁盤問題(dfs、トレース)

1152 ワード

http://poj.org/problem?id=1321
dfsは、残りの行数が残りの駒の数より小さい場合は、検索しないでください。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#define LL long long
#define _LL __int64
#define eps 1e-8

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10;
int n,m;
char Map[maxn][maxn];
int vis[maxn];
int ans;

void dfs(int x,int cur)
{
	if(x > m)
	{
		ans++;
		return;
	}
	if(cur > n)
		return;

	for(int i = cur; ;i++)
	{
		if(n-i < m-x) //  。                 
			break;
		for(int j = 1; j <= n; j++)
		{
			if(!vis[j] && Map[i][j] == '#')
			{
				vis[j] = 1;
				dfs(x+1,i+1);//   i+1   cur+1
				vis[j] = 0;
			}
		}
	}
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		if(n == -1 && m == -1)
			break;
		for(int i = 1; i <= n; i++)
			scanf("%s",Map[i]+1);
		ans = 0;
		memset(vis,0,sizeof(vis));
		dfs(1,1);
		printf("%d
",ans); } return 0; }