#include <iostream> #include <vector> using namespace std; long long p[3000][3000]; long long n, k; long long result = 0; class Point { public: int x, y; Point(int x, int y) { this -> x = x; this -> y = y; } Point left() { return Point(this -> x-1, this -> y); } Point right() { return Point(this -> x+1, this -> y); } Point bottom(){ return Point(this -> x, this -> y-1); } Point top() { return Point(this -> x, this -> y+1); } }; void addToResult(long long x) { result %= 1000000007; x %= 1000000007; result += x; if(result < 0) result *= -1; result %= 1000000007; } int isCorner(int x, int y, int v) { if(p[x][y] == v) { int ctr_crn = 0; if( ((x > 0 && p[x-1][y] == v-1) && (y > 0 && p[x][y-1] == v-1)) ) ctr_crn++; if( ((x > 0 && p[x-1][y] == v-1) && (y < n-1 && p[x][y+1] == v-1)) ) ctr_crn++; if( ((x < n-1 && p[x+1][y] == v-1) && (y < n-1 && p[x][y+1] == v-1)) ) ctr_crn++; if( ((x < n-1 && p[x+1][y] == v-1) && (y > 0 && p[x][y-1] == v-1)) ) ctr_crn++; if(ctr_crn == 1) return 1; if(ctr_crn == 2) return 2; if(ctr_crn > 2) return 4; } return 0; } int isLine(int x, int y, int v) { if(p[x][y] == v) { int ctr_ln = 0; if( (x > 0 && p[x-1][y] == v-1) && (x < n-1 && p[x+1][y] == v-1) ) ctr_ln++; if( (y > 0 && p[x][y-1] == v-1) && (y < n-1 && p[x][y+1] == v-1) ) ctr_ln++; if(ctr_ln == 1) return 1; if(ctr_ln > 1) return 2; } return 0; } bool isValid(long long x, long long y, int busy) { if(x < 0 || x >= n || y < 0 || y >= n) return false; if(p[x][y] == busy) return false; if( (x > 0 && p[x-1][y] == busy) || (x < n-1 && p[x+1][y] == busy) || (y > 0 && p[x][y-1] == busy) || (y < n-1 && p[x][y+1] == busy) ) { return true; } return false; } bool isValid(long long x, long long y, int busy, int notValidFor) { for(int i = notValidFor; i > 0; i--) { if(p[x][y] == notValidFor || isValid(x, y, i)) { return false; } } return isValid(x, y, busy); } long long s(long long n, long long k) { if(k <= 0 || k > n) return 0; long long result = 1, div = 1; for(long long i = n; i > n - k; i--) { result *= i; } for(int i = 1; i <= k; i++) { div *= i; } result /= div; return result; } void inspectPoint(int x, int y, int depth, long long validFieldsCount) { if(depth <= k) { p[x][y] = depth; if(depth == 3) addToResult(validFieldsCount - 1); if(depth == 4) addToResult(validFieldsCount - 2); if(isValid(x - 1, y, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x - 1, y, depth + 1, validFieldsCount); } if(isValid(x + 1, y, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x + 1, y, depth + 1, validFieldsCount); } if(isValid(x, y - 1, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x, y - 1, depth + 1, validFieldsCount); } if(isValid(x, y + 1, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x, y + 1, depth + 1, validFieldsCount); } } } int main() { vector<Point> validPoints; cin >> n >> k; for(long long i = 0; i < n; i++) { string s; cin >> s; for(long long j = 0; j < n; j++) { if(s[j] == '.') p[i][j] = 0; else p[i][j] = 1; } } long long validFieldsCount = 0; for(long long x = 0; x < n; x++) { for(long long y = 0; y < n; y++) { if(isValid(x, y, 1)) { validPoints.push_back(Point(x, y)); validFieldsCount++; } } } if(validFieldsCount >= k) { addToResult(s(validFieldsCount, k)); } for(Point point : validPoints) { inspectPoint(point.x, point.y, 2, validFieldsCount); } if(k >= 3) { for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { addToResult(isCorner(x, y, 3)); addToResult(isLine(x, y, 3)); } } } if(k >= 4) { for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { addToResult(isCorner(x, y, 4)); addToResult(isLine(x, y, 4)); } } } cout << result; 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <iostream> #include <vector> using namespace std; long long p[3000][3000]; long long n, k; long long result = 0; class Point { public: int x, y; Point(int x, int y) { this -> x = x; this -> y = y; } Point left() { return Point(this -> x-1, this -> y); } Point right() { return Point(this -> x+1, this -> y); } Point bottom(){ return Point(this -> x, this -> y-1); } Point top() { return Point(this -> x, this -> y+1); } }; void addToResult(long long x) { result %= 1000000007; x %= 1000000007; result += x; if(result < 0) result *= -1; result %= 1000000007; } int isCorner(int x, int y, int v) { if(p[x][y] == v) { int ctr_crn = 0; if( ((x > 0 && p[x-1][y] == v-1) && (y > 0 && p[x][y-1] == v-1)) ) ctr_crn++; if( ((x > 0 && p[x-1][y] == v-1) && (y < n-1 && p[x][y+1] == v-1)) ) ctr_crn++; if( ((x < n-1 && p[x+1][y] == v-1) && (y < n-1 && p[x][y+1] == v-1)) ) ctr_crn++; if( ((x < n-1 && p[x+1][y] == v-1) && (y > 0 && p[x][y-1] == v-1)) ) ctr_crn++; if(ctr_crn == 1) return 1; if(ctr_crn == 2) return 2; if(ctr_crn > 2) return 4; } return 0; } int isLine(int x, int y, int v) { if(p[x][y] == v) { int ctr_ln = 0; if( (x > 0 && p[x-1][y] == v-1) && (x < n-1 && p[x+1][y] == v-1) ) ctr_ln++; if( (y > 0 && p[x][y-1] == v-1) && (y < n-1 && p[x][y+1] == v-1) ) ctr_ln++; if(ctr_ln == 1) return 1; if(ctr_ln > 1) return 2; } return 0; } bool isValid(long long x, long long y, int busy) { if(x < 0 || x >= n || y < 0 || y >= n) return false; if(p[x][y] == busy) return false; if( (x > 0 && p[x-1][y] == busy) || (x < n-1 && p[x+1][y] == busy) || (y > 0 && p[x][y-1] == busy) || (y < n-1 && p[x][y+1] == busy) ) { return true; } return false; } bool isValid(long long x, long long y, int busy, int notValidFor) { for(int i = notValidFor; i > 0; i--) { if(p[x][y] == notValidFor || isValid(x, y, i)) { return false; } } return isValid(x, y, busy); } long long s(long long n, long long k) { if(k <= 0 || k > n) return 0; long long result = 1, div = 1; for(long long i = n; i > n - k; i--) { result *= i; } for(int i = 1; i <= k; i++) { div *= i; } result /= div; return result; } void inspectPoint(int x, int y, int depth, long long validFieldsCount) { if(depth <= k) { p[x][y] = depth; if(depth == 3) addToResult(validFieldsCount - 1); if(depth == 4) addToResult(validFieldsCount - 2); if(isValid(x - 1, y, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x - 1, y, depth + 1, validFieldsCount); } if(isValid(x + 1, y, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x + 1, y, depth + 1, validFieldsCount); } if(isValid(x, y - 1, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x, y - 1, depth + 1, validFieldsCount); } if(isValid(x, y + 1, depth, depth - 1)) { if(depth == k) { addToResult(1); } inspectPoint(x, y + 1, depth + 1, validFieldsCount); } } } int main() { vector<Point> validPoints; cin >> n >> k; for(long long i = 0; i < n; i++) { string s; cin >> s; for(long long j = 0; j < n; j++) { if(s[j] == '.') p[i][j] = 0; else p[i][j] = 1; } } long long validFieldsCount = 0; for(long long x = 0; x < n; x++) { for(long long y = 0; y < n; y++) { if(isValid(x, y, 1)) { validPoints.push_back(Point(x, y)); validFieldsCount++; } } } if(validFieldsCount >= k) { addToResult(s(validFieldsCount, k)); } for(Point point : validPoints) { inspectPoint(point.x, point.y, 2, validFieldsCount); } if(k >= 3) { for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { addToResult(isCorner(x, y, 3)); addToResult(isLine(x, y, 3)); } } } if(k >= 4) { for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { addToResult(isCorner(x, y, 4)); addToResult(isLine(x, y, 4)); } } } cout << result; return 0; } |