1.28.2019
开始kuangbin系列。
第一题 https://vjudge.net/problem/POJ-1321#author=0
搜索问题,DFS。
由于是考虑行列,可以以行为变量,在dfs里面搜索列,可以不重复。
和皇后问题有点相似。可以对比着参考。
AC代码:
#include <iostream>
using namespace std;
char s[10][10];
int n,m;
bool vis[10];
int cnt;
int ans;
void dfs(int x,int cnt){
if(cnt==m){
ans++;
return;
}
if(x>=n){
return ;//>=防止越界
}
for(int i=0;i<n;i++){
if(!vis[i]&&s[x][i]=='#'){
vis[i]=1;
dfs(x+1,cnt+1);
vis[i]=0;
}
}
dfs(x+1,cnt);
}
int main(){
while(cin>>n>>m){
if(n==-1&&m==-1){
return 0;
}
else {
for(int i=0;i<n;i++){
cin>>s[i];
}
cnt=0;
ans=0;
dfs(0,0);
}
cout<<ans<<endl;
}
return 0;
}