#include <iostream> #include <cstdio> using namespace std; char tab[ 3001 ][ 3001 ]; int where[ 4 ][ 2 ]; int canI( int x, int y ) { if ( tab[ x ][ y ] != '.' ) return 0; if ( tab[ x - 1 ][ y ] == '#' || tab[ x + 1 ][ y ] == '#' || tab[ x ][ y - 1 ] == '#' || tab[ x ][ y + 1 ] == '#' ) return 1; if ( tab[ x - 1 ][ y ] == '?' || tab[ x + 1 ][ y ] == '?' || tab[ x ][ y - 1 ] == '?' || tab[ x ][ y + 1 ] == '?' ) return 2; return 0; } bool reallyCanI( int x, int y, int q ) { for ( int i = 0; i < q; ++i ) { if ( x < where[ i ][ 0 ] || x == where[ i ][ 0 ] && y < where[ i ][ 1 ] ) return false; } return true; } int fun( int k, int n, int q = 0 ) { int h, counter = 0; if ( q == k ) return 1; for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { h = canI( i, j ); if ( h == 1 ) { if ( reallyCanI( i, j, q ) ) { where[ q ][ 0 ] = i; where[ q ][ 1 ] = j; tab[ i ][ j ] = '?'; counter = ( counter + fun( k, n, q + 1 ) ) % 1000000007; tab[ i ][ j ] = '.'; } } else if ( h == 2 ) { where[ q ][ 0 ] = i; where[ q ][ 1 ] = j; tab[ i ][ j ] = '?'; counter = ( counter + fun( k, n, q + 1 ) ) % 1000000007; tab[ i ][ j ] = '.'; } } } return counter; } int main() { int n, k; char z; scanf( "%d%d%c", &n, &k, &z ); for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { scanf( "%c", &tab[ i ][ j ] ); } scanf( "%c", &z ); } printf( "%d\n", fun( k, n ) ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <iostream> #include <cstdio> using namespace std; char tab[ 3001 ][ 3001 ]; int where[ 4 ][ 2 ]; int canI( int x, int y ) { if ( tab[ x ][ y ] != '.' ) return 0; if ( tab[ x - 1 ][ y ] == '#' || tab[ x + 1 ][ y ] == '#' || tab[ x ][ y - 1 ] == '#' || tab[ x ][ y + 1 ] == '#' ) return 1; if ( tab[ x - 1 ][ y ] == '?' || tab[ x + 1 ][ y ] == '?' || tab[ x ][ y - 1 ] == '?' || tab[ x ][ y + 1 ] == '?' ) return 2; return 0; } bool reallyCanI( int x, int y, int q ) { for ( int i = 0; i < q; ++i ) { if ( x < where[ i ][ 0 ] || x == where[ i ][ 0 ] && y < where[ i ][ 1 ] ) return false; } return true; } int fun( int k, int n, int q = 0 ) { int h, counter = 0; if ( q == k ) return 1; for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { h = canI( i, j ); if ( h == 1 ) { if ( reallyCanI( i, j, q ) ) { where[ q ][ 0 ] = i; where[ q ][ 1 ] = j; tab[ i ][ j ] = '?'; counter = ( counter + fun( k, n, q + 1 ) ) % 1000000007; tab[ i ][ j ] = '.'; } } else if ( h == 2 ) { where[ q ][ 0 ] = i; where[ q ][ 1 ] = j; tab[ i ][ j ] = '?'; counter = ( counter + fun( k, n, q + 1 ) ) % 1000000007; tab[ i ][ j ] = '.'; } } } return counter; } int main() { int n, k; char z; scanf( "%d%d%c", &n, &k, &z ); for ( int i = 1; i <= n; ++i ) { for ( int j = 1; j <= n; ++j ) { scanf( "%c", &tab[ i ][ j ] ); } scanf( "%c", &z ); } printf( "%d\n", fun( k, n ) ); } |