pku acm 1321 源码

#include <iostream>
using namespace std ;
char a[8][8] ;
bool row[8] , col[8] ;
int n , k ; 
int sum ;

void dfs( int x , int y ,int step)
{
	int i , j ;
	if ( step > k )
	{
		sum++ ;
		return;
	}

	for ( i = x ; i < n ; i++ )
	{
		for ( j = 0 ; j < n ; j++ )
		{
			if ( a[i][j] == '#' && row[i] == 0 && col[j] == 0 )
			{
				row[i] = 1 ;
				col[j] = 1 ;
				a[i][j] = '.' ;
				dfs(i , j ,step+1) ;
				row[i] = 0 ;
				col[j] = 0 ;
				a[i][j] = '#' ;
			}
		}
	}
}
int main()
{
	int i , j ; 
	while ( true )
	{
		cin >> n >> k ;
		if ( n == -1 && k == -1 )
			break ;
		memset( row , 0 , sizeof(row) ) ;
		memset( col , 0 , sizeof(col) ) ;
		for ( i = 0 ; i < n ; i++ )
			for ( j = 0 ; j < n ; j++ )
				cin >> a[i][j] ;
		sum = 0 ;
		dfs( 0 , 0 ,1) ;
		cout << sum << endl ;
	}
	return 0 ;
}

你可能感兴趣的:(ACM,DFS,pku,1321)