标签:搜索
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 23338 | Accepted: 11567 |
Description
Input
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
一开始居然想多了。。本来我想着是把所有的‘#’保留下来然后深搜这些点。。后来各种判断条件写了一大堆还没写出来了
其实是从第一行开始放,然后放置下一行,枚举所有的列,然后深搜回溯。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,k,ans;
char ma[9][9];
bool vis[9];
void dfs(int x,int num)
{
if(num==k)
{
ans++;
return ;
}
for(int i=x+1;i<n;i++)
for(int j=0;j<n;j++)
{
if(ma[i][j]=='#'&&!vis[j])
{
vis[j]=1;
dfs(i,num+1);
vis[j]=0;
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==-1&&k==-1)break;
for(int i=0;i<n;i++)
scanf("%s",ma[i]);
memset(vis,0,sizeof(vis));
ans=0;dfs(-1,0);
printf("%d\n",ans);
}
return 0;
}标签:搜索
原文地址:http://blog.csdn.net/qq_16255321/article/details/41221215