#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> #include <string> using namespace std; char c[3001][3001]; bool added[3001][3001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < n; j++) c[i][j] = s[j]; //fgets(c[i], n + 2, stdin); } vector<pair<int, int> > ones, twos, threes, v; int twosWays = 0, threesWays = 0, onesToTwos = 0, onesWithTwos = 0; int d1[4] = { -1,1,0,0 }; int d2[4] = { 0,0,-1,1 }; //--przypisywanie jedynek-- for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (c[i][j] == '#') { for (int di = 0; di < 4; di++) if (i + d1[di] >= 0 && i + d1[di] < n && j + d2[di] >= 0 && j + d2[di] < n && c[i + d1[di]][j + d2[di]] == '.') { ones.push_back(make_pair(i + d1[di], j + d2[di])); v.push_back(make_pair(i + d1[di], j + d2[di])); c[i + d1[di]][j + d2[di]] = '1'; } } } if (k == 1) { printf("%d\n", ones.size()); return 0; } //--przypisywanie dwójek-- for (int i = 0; i < ones.size(); i++) { int howManyTwos = 0; for (int di = 0; di < 4; di++) if (ones[i].first + d1[di] >= 0 && ones[i].first + d1[di] < n && ones[i].second + d2[di] >= 0 && ones[i].second + d2[di] < n) { if (c[ones[i].first + d1[di]][ones[i].second + d2[di]] == '.') { howManyTwos++; twos.push_back(make_pair(ones[i].first + d1[di], ones[i].second + d2[di])); c[ones[i].first + d1[di]][ones[i].second + d2[di]] = '2'; twosWays++; } else if (c[ones[i].first + d1[di]][ones[i].second + d2[di]] == '2') { twosWays++; howManyTwos++; } } if (howManyTwos == 2) onesToTwos++; if (howManyTwos == 3) onesToTwos += 3; if (howManyTwos == 4) onesToTwos += 6; } long long int mod = 1000 * 1000 * 1000 + 7; if (k == 2) { long long int oSize = ones.size(); long long int result = ((oSize * (oSize - 1) / 2) % mod + twosWays) % mod; cout << result << endl; return 0; } //--przypisywanie trójek-- for (int i = 0; i < twos.size(); i++) { int twoWaysLocal = 0; for (int di = 0; di < 4; di++) if (twos[i].first + d1[di] >= 0 && twos[i].first + d1[di] < n && twos[i].second + d2[di] >= 0 && twos[i].second + d2[di] < n) if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '1') twoWaysLocal++; if (twoWaysLocal == 2) onesWithTwos++; if (twoWaysLocal == 3) onesWithTwos += 3; if (twoWaysLocal == 4) onesWithTwos += 6; for (int di = 0; di < 4; di++) if (twos[i].first + d1[di] >= 0 && twos[i].first + d1[di] < n && twos[i].second + d2[di] >= 0 && twos[i].second + d2[di] < n) { if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '.') { threes.push_back(make_pair(twos[i].first + d1[di], twos[i].second + d2[di])); c[twos[i].first + d1[di]][twos[i].second + d2[di]] = '3'; threesWays += twoWaysLocal; } else if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '3') threesWays += twoWaysLocal; } } /*for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << c[i][j]; cout << endl; }*/ if (k == 3) { //cout << ones.size() << endl; //cout << threesWays << endl; //cout << onesToTwos << endl; //cout << twosWays << endl; //cout << onesWithTwos << endl; long long int oSize = ones.size(); long long int a = oSize, b = oSize - 1, c = oSize - 2; if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3; if (a % 2 == 0) a /= 2; else if (b % 2 == 0) b /= 2; else c /= 2; long long int tw = twosWays; long long int result = (((a*b) % mod) * c) % mod; //same jedynki result = (result + threesWays) % mod; //jedynka, dwójka i trójka result = (result + onesToTwos) % mod; //jedynka, dwie dwójki result = (result + (tw * (oSize - 1) - onesWithTwos) % mod) % mod; //dwie jedynki i dwójka cout << result << endl; return 0; } //k == 4 for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) added[i][j] = false; long long int result = 0; for (int i1 = 0; i1 < v.size(); i1++) { //dodanie dwójek, do których można dojść z v[i1] int v1AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i1].first + d1[di] >= 0 && v[i1].first + d1[di] < n && v[i1].second + d2[di] >= 0 && v[i1].second + d2[di] < n) { if (c[v[i1].first + d1[di]][v[i1].second + d2[di]] == '2') { v.push_back(make_pair(v[i1].first + d1[di], v[i1].second + d2[di])); added[v[i1].first + d1[di]][v[i1].second + d2[di]] = true; v1AddedNumbers++; } } for (int i2 = i1 + 1; i2 < v.size(); i2++) { int v2AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i2].first + d1[di] >= 0 && v[i2].first + d1[di] < n && v[i2].second + d2[di] >= 0 && v[i2].second + d2[di] < n) { if (c[v[i2].first + d1[di]][v[i2].second + d2[di]] == c[v[i2].first][v[i2].second] + 1 && !added[v[i2].first + d1[di]][v[i2].second + d2[di]]) { v.push_back(make_pair(v[i2].first + d1[di], v[i2].second + d2[di])); added[v[i2].first + d1[di]][v[i2].second + d2[di]] = true; v2AddedNumbers++; } } if (k == 4) { for (int i3 = i2 + 1; i3 < v.size(); i3++) { int v3AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i3].first + d1[di] >= 0 && v[i3].first + d1[di] < n && v[i3].second + d2[di] >= 0 && v[i3].second + d2[di] < n) { if (c[v[i3].first + d1[di]][v[i3].second + d2[di]] == c[v[i3].first][v[i3].second] + 1 && !added[v[i3].first + d1[di]][v[i3].second + d2[di]]) { v.push_back(make_pair(v[i3].first + d1[di], v[i3].second + d2[di])); added[v[i3].first + d1[di]][v[i3].second + d2[di]] = true; v3AddedNumbers++; } } result = (result + (v.size() - i3 - 1)) % mod; for (int i = 0; i < v3AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } } else //k==3 { /*for (int i3 = i2 + 1; i3 < v.size(); i3++) { cout << c[v[i1].first][v[i1].second] //<< "(" << v[i1].first << "," << v[i1].second << ")" << " " << c[v[i2].first][v[i2].second] //<< "(" << v[i2].first << "," << v[i2].second << ")" << " " << c[v[i3].first][v[i3].second] << endl; //<< "(" << v[i3].first << "," << v[i3].second << ")" << endl; }*/ result = (result + (v.size() - i2 - 1)) % mod; } for (int i = 0; i < v2AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } for (int i = 0; i < v1AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } cout << result << endl; }
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> #include <string> using namespace std; char c[3001][3001]; bool added[3001][3001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < n; j++) c[i][j] = s[j]; //fgets(c[i], n + 2, stdin); } vector<pair<int, int> > ones, twos, threes, v; int twosWays = 0, threesWays = 0, onesToTwos = 0, onesWithTwos = 0; int d1[4] = { -1,1,0,0 }; int d2[4] = { 0,0,-1,1 }; //--przypisywanie jedynek-- for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (c[i][j] == '#') { for (int di = 0; di < 4; di++) if (i + d1[di] >= 0 && i + d1[di] < n && j + d2[di] >= 0 && j + d2[di] < n && c[i + d1[di]][j + d2[di]] == '.') { ones.push_back(make_pair(i + d1[di], j + d2[di])); v.push_back(make_pair(i + d1[di], j + d2[di])); c[i + d1[di]][j + d2[di]] = '1'; } } } if (k == 1) { printf("%d\n", ones.size()); return 0; } //--przypisywanie dwójek-- for (int i = 0; i < ones.size(); i++) { int howManyTwos = 0; for (int di = 0; di < 4; di++) if (ones[i].first + d1[di] >= 0 && ones[i].first + d1[di] < n && ones[i].second + d2[di] >= 0 && ones[i].second + d2[di] < n) { if (c[ones[i].first + d1[di]][ones[i].second + d2[di]] == '.') { howManyTwos++; twos.push_back(make_pair(ones[i].first + d1[di], ones[i].second + d2[di])); c[ones[i].first + d1[di]][ones[i].second + d2[di]] = '2'; twosWays++; } else if (c[ones[i].first + d1[di]][ones[i].second + d2[di]] == '2') { twosWays++; howManyTwos++; } } if (howManyTwos == 2) onesToTwos++; if (howManyTwos == 3) onesToTwos += 3; if (howManyTwos == 4) onesToTwos += 6; } long long int mod = 1000 * 1000 * 1000 + 7; if (k == 2) { long long int oSize = ones.size(); long long int result = ((oSize * (oSize - 1) / 2) % mod + twosWays) % mod; cout << result << endl; return 0; } //--przypisywanie trójek-- for (int i = 0; i < twos.size(); i++) { int twoWaysLocal = 0; for (int di = 0; di < 4; di++) if (twos[i].first + d1[di] >= 0 && twos[i].first + d1[di] < n && twos[i].second + d2[di] >= 0 && twos[i].second + d2[di] < n) if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '1') twoWaysLocal++; if (twoWaysLocal == 2) onesWithTwos++; if (twoWaysLocal == 3) onesWithTwos += 3; if (twoWaysLocal == 4) onesWithTwos += 6; for (int di = 0; di < 4; di++) if (twos[i].first + d1[di] >= 0 && twos[i].first + d1[di] < n && twos[i].second + d2[di] >= 0 && twos[i].second + d2[di] < n) { if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '.') { threes.push_back(make_pair(twos[i].first + d1[di], twos[i].second + d2[di])); c[twos[i].first + d1[di]][twos[i].second + d2[di]] = '3'; threesWays += twoWaysLocal; } else if (c[twos[i].first + d1[di]][twos[i].second + d2[di]] == '3') threesWays += twoWaysLocal; } } /*for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << c[i][j]; cout << endl; }*/ if (k == 3) { //cout << ones.size() << endl; //cout << threesWays << endl; //cout << onesToTwos << endl; //cout << twosWays << endl; //cout << onesWithTwos << endl; long long int oSize = ones.size(); long long int a = oSize, b = oSize - 1, c = oSize - 2; if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3; if (a % 2 == 0) a /= 2; else if (b % 2 == 0) b /= 2; else c /= 2; long long int tw = twosWays; long long int result = (((a*b) % mod) * c) % mod; //same jedynki result = (result + threesWays) % mod; //jedynka, dwójka i trójka result = (result + onesToTwos) % mod; //jedynka, dwie dwójki result = (result + (tw * (oSize - 1) - onesWithTwos) % mod) % mod; //dwie jedynki i dwójka cout << result << endl; return 0; } //k == 4 for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) added[i][j] = false; long long int result = 0; for (int i1 = 0; i1 < v.size(); i1++) { //dodanie dwójek, do których można dojść z v[i1] int v1AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i1].first + d1[di] >= 0 && v[i1].first + d1[di] < n && v[i1].second + d2[di] >= 0 && v[i1].second + d2[di] < n) { if (c[v[i1].first + d1[di]][v[i1].second + d2[di]] == '2') { v.push_back(make_pair(v[i1].first + d1[di], v[i1].second + d2[di])); added[v[i1].first + d1[di]][v[i1].second + d2[di]] = true; v1AddedNumbers++; } } for (int i2 = i1 + 1; i2 < v.size(); i2++) { int v2AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i2].first + d1[di] >= 0 && v[i2].first + d1[di] < n && v[i2].second + d2[di] >= 0 && v[i2].second + d2[di] < n) { if (c[v[i2].first + d1[di]][v[i2].second + d2[di]] == c[v[i2].first][v[i2].second] + 1 && !added[v[i2].first + d1[di]][v[i2].second + d2[di]]) { v.push_back(make_pair(v[i2].first + d1[di], v[i2].second + d2[di])); added[v[i2].first + d1[di]][v[i2].second + d2[di]] = true; v2AddedNumbers++; } } if (k == 4) { for (int i3 = i2 + 1; i3 < v.size(); i3++) { int v3AddedNumbers = 0; for (int di = 0; di < 4; di++) if (v[i3].first + d1[di] >= 0 && v[i3].first + d1[di] < n && v[i3].second + d2[di] >= 0 && v[i3].second + d2[di] < n) { if (c[v[i3].first + d1[di]][v[i3].second + d2[di]] == c[v[i3].first][v[i3].second] + 1 && !added[v[i3].first + d1[di]][v[i3].second + d2[di]]) { v.push_back(make_pair(v[i3].first + d1[di], v[i3].second + d2[di])); added[v[i3].first + d1[di]][v[i3].second + d2[di]] = true; v3AddedNumbers++; } } result = (result + (v.size() - i3 - 1)) % mod; for (int i = 0; i < v3AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } } else //k==3 { /*for (int i3 = i2 + 1; i3 < v.size(); i3++) { cout << c[v[i1].first][v[i1].second] //<< "(" << v[i1].first << "," << v[i1].second << ")" << " " << c[v[i2].first][v[i2].second] //<< "(" << v[i2].first << "," << v[i2].second << ")" << " " << c[v[i3].first][v[i3].second] << endl; //<< "(" << v[i3].first << "," << v[i3].second << ")" << endl; }*/ result = (result + (v.size() - i2 - 1)) % mod; } for (int i = 0; i < v2AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } for (int i = 0; i < v1AddedNumbers; i++) { added[v[v.size() - 1].first][v[v.size() - 1].second] = false; v.pop_back(); } } cout << result << endl; } |