Poj Solution 1321

http://poj.org/problem?id=1321

#include <iostream>
using namespace std;
int map[10][10];
int vis[10];
int n,k,ans;
void dfs(int col,int cnt)
{
int i;
if(cnt==k)
{
   ans++ ;
   return ;
}
if(n-col<k-cnt-1) return ;
for(i=1;i<=n;i++)
{
   if(!vis[i]&&map[col][i])
   {
    vis[i]=1;
    dfs(col+1,cnt+1);
    vis[i]=0;
   }
}
dfs(col+1,cnt);
}
int main()
{
int i,j;
char ch;
while(scanf("%d %d",&n,&k)!=EOF)
{
   if(n==-1&&k==-1) break;
   for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
     cin>>ch;
     if(ch=='.')      map[i][j]=0;
     else if(ch=='#') map[i][j]=1;
    }
   ans=0;
   memset(vis,0,sizeof(vis));
   dfs(1,0);
   printf("%dn",ans);
}
return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *