这个我是按照单个点来判断的,一定要注意当s==k时要返回,因为后面不能继续放置了.

#include<iostream>
using namespace std;
const int N=9;
int n,k,res;
char a[N][N];
bool c[N],r[N];
void dfs(int x,int y,int s)
{
    if(s>k)return ;
    if(s==k)
        {res++;return;}
    if(y==n+1) x++,y=1;
    if(x>n)return ;
    dfs(x,y+1,s);//不放
    if(!r[x]&&!c[y]&&a[x][y]=='#')
        {
            r[x]=c[y]=true;
            dfs(x,y+1,s+1);
            r[x]=c[y]=false;
        }
}
int main()
{
    while(cin>>n>>k&&n!=-1&&k!=-1)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                cin>>a[i][j];
        }
        dfs(1,1,0);
        cout<<res<<endl;
        res=0;
    }
}