#define dbg if(0) #include <iostream> using namespace std; typedef unsigned long long ull; const int BOARD_SIZE = 3300*3300; const ull MOD = 1000000007; char board[BOARD_SIZE]; void print_board(int n){ for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cout << (board[i*n+j]==0 ? '.' : '#'); } cout << endl; } } int check_position(int i, int n){ if(board[i] == 1){ return 0; } int x = i%n; int y = i/n; dbg cout << "i: " << i << " checking x: " << x << " y: " << y << endl; bool has_neighbour = false; if(x > 0 && board[y*n + x - 1] == 1){ has_neighbour = true; } if(x < n-1 && board[y*n + x + 1] == 1){ dbg cout << "has_neighbour" << endl; has_neighbour = true; } if(y > 0 && board[(y-1)*n + x] == 1){ has_neighbour = true; } if(y < n-1 && board[(y+1)*n + x] == 1){ has_neighbour = true; } if(has_neighbour){ board[i] = 1; return 1; } return 0; } void brut1(int n){ ull counter = 0; for(int i = 0; i < n*n; ++i){ int can_put_i = check_position(i, n); counter += can_put_i; if(can_put_i) board[i] = 0; } cout << counter%MOD << endl; } void brut2(int n){ ull counter = 0; for(int i = 0; i < (n*n)-1; ++i){ for(int j = i+1; j < n*n; ++j){ dbg cout << "i: " << i << " j: " << j << endl; int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); if( can_put_i && can_put_j){ dbg cout << "Adding" << endl; ++counter; } if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; } } cout << counter%MOD << endl; } void brut3(int n){ ull counter = 0; for(int i = 0; i < (n*n)-2; ++i){ for(int j = i+1; j < (n*n)-1; ++j){ for(int k = j+1; k < n*n; ++k){ int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); int can_put_k = check_position(k, n); if(can_put_i && can_put_j && can_put_k) ++counter; if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; if(can_put_k) board[k] = 0; } } } cout << counter%MOD << endl; } void brut4(int n){ ull counter = 0; for(int i = 0; i < (n*n)-3; ++i){ for(int j = i+1; j < (n*n)-2; ++j){ for(int k = j+1; k < (n*n)-1; ++k){ for(int l = k+1; l < n*n; ++l){ int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); int can_put_k = check_position(k, n); int can_put_l = check_position(l, n); if(can_put_i && can_put_j && can_put_k && can_put_l) ++counter; if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; if(can_put_k) board[k] = 0; if(can_put_l) board[l] = 0; } } } } cout << counter%MOD << endl; } int main(){ int n, k; char tmp; cin >> n >> k; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cin >> tmp; board[i*n + j] = tmp == '.' ? 0 : 1; } } dbg print_board(n); if(k == 1) brut1(n); if(k==2) brut2(n); if(k==3) brut3(n); if(k==4) brut4(n); return 0; }
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #define dbg if(0) #include <iostream> using namespace std; typedef unsigned long long ull; const int BOARD_SIZE = 3300*3300; const ull MOD = 1000000007; char board[BOARD_SIZE]; void print_board(int n){ for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cout << (board[i*n+j]==0 ? '.' : '#'); } cout << endl; } } int check_position(int i, int n){ if(board[i] == 1){ return 0; } int x = i%n; int y = i/n; dbg cout << "i: " << i << " checking x: " << x << " y: " << y << endl; bool has_neighbour = false; if(x > 0 && board[y*n + x - 1] == 1){ has_neighbour = true; } if(x < n-1 && board[y*n + x + 1] == 1){ dbg cout << "has_neighbour" << endl; has_neighbour = true; } if(y > 0 && board[(y-1)*n + x] == 1){ has_neighbour = true; } if(y < n-1 && board[(y+1)*n + x] == 1){ has_neighbour = true; } if(has_neighbour){ board[i] = 1; return 1; } return 0; } void brut1(int n){ ull counter = 0; for(int i = 0; i < n*n; ++i){ int can_put_i = check_position(i, n); counter += can_put_i; if(can_put_i) board[i] = 0; } cout << counter%MOD << endl; } void brut2(int n){ ull counter = 0; for(int i = 0; i < (n*n)-1; ++i){ for(int j = i+1; j < n*n; ++j){ dbg cout << "i: " << i << " j: " << j << endl; int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); if( can_put_i && can_put_j){ dbg cout << "Adding" << endl; ++counter; } if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; } } cout << counter%MOD << endl; } void brut3(int n){ ull counter = 0; for(int i = 0; i < (n*n)-2; ++i){ for(int j = i+1; j < (n*n)-1; ++j){ for(int k = j+1; k < n*n; ++k){ int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); int can_put_k = check_position(k, n); if(can_put_i && can_put_j && can_put_k) ++counter; if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; if(can_put_k) board[k] = 0; } } } cout << counter%MOD << endl; } void brut4(int n){ ull counter = 0; for(int i = 0; i < (n*n)-3; ++i){ for(int j = i+1; j < (n*n)-2; ++j){ for(int k = j+1; k < (n*n)-1; ++k){ for(int l = k+1; l < n*n; ++l){ int can_put_i = check_position(i, n); int can_put_j = check_position(j,n); int can_put_k = check_position(k, n); int can_put_l = check_position(l, n); if(can_put_i && can_put_j && can_put_k && can_put_l) ++counter; if(can_put_i) board[i] = 0; if(can_put_j) board[j] = 0; if(can_put_k) board[k] = 0; if(can_put_l) board[l] = 0; } } } } cout << counter%MOD << endl; } int main(){ int n, k; char tmp; cin >> n >> k; for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cin >> tmp; board[i*n + j] = tmp == '.' ? 0 : 1; } } dbg print_board(n); if(k == 1) brut1(n); if(k==2) brut2(n); if(k==3) brut3(n); if(k==4) brut4(n); return 0; } |