poj1321

来源:互联网 发布:电脑音响品牌 知乎 编辑:程序博客网 时间:2024/05/17 22:06

本题是对深搜的考察,以前也写过深搜的题,但是不懂,这道题是看了别人的代码还是不懂,结果发现是看错题啦,呵呵,什么时候能不那么马虎呢?

#include <iostream>
using namespace std;
int status=0;
int n,k;
bool a[9][9];
bool b[9];
void Dev(int row, int num)
{
 if(k==num)
 {
  status++;
  return;
 }
 if(row>n)
  return;
 for(int j=1;j<=n;j++)
 {
  if(a[row][j] && !b[j])
  {
   b[j]=true;
   Dev(row+1,num+1);
   b[j]=false;
  }
 }
 Dev(row+1,num);//可能就是k比n小的
 return;
}
int main()
{
 
 int i,j;
 char c;
 while(cin>>n>>k)
 {
  
  if(n==-1 && k==-1)
   break;
  status=0;
        for(i=1 ;i<=n ;i++)
   for(j=1; j<=n; j++)
   {
    cin>>c;
    if(c=='#')
     a[i][j]=true;
    else
     a[i][j]=false;
   }
  for(i=1; i<=9; i++)
   b[i]=false;
  Dev(1,0);
        cout<<status<<endl;
 }
 return 0;
}

原创粉丝点击