Advertisement
Dmaxiya

P11724

Feb 23rd, 2025
131
0
364 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 300 + 100;
  6. int n, k, ans, tmp;
  7. int num[maxn][maxn];
  8. set<int> st;
  9. const int dir[4][2] = {
  10.     { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }
  11. };
  12.  
  13. bool in(int x, int y) {
  14.     return x >= 1 && x <= n && y >= 1 && y <= n;
  15. }
  16.  
  17. bool judge(int x, int y) {
  18.     st.clear();
  19.     for (int i = 0; i < 4; ++i) {
  20.         int xx = x + dir[i][0];
  21.         int yy = y + dir[i][1];
  22.         if (!in(xx, yy)) {
  23.             return false;
  24.         }
  25.         st.insert(num[xx][yy]);
  26.     }
  27.     return st.size() >= 3;
  28. }
  29.  
  30. int cal(int x, int y) {
  31.     int ret = 0;
  32.     for (int i = x - 1; i <= x; ++i) {
  33.         for (int j = y - 1; j <= y; ++j) {
  34.             if (judge(i, j)) {
  35.                 ++ret;
  36.             }
  37.         }
  38.     }
  39.     return ret;
  40. }
  41.  
  42. int main() {
  43. #ifdef ExRoc
  44.     freopen("test.txt", "r", stdin);
  45. #endif // ExRoc
  46.     ios::sync_with_stdio(false);
  47.  
  48.     cin >> n >> k;
  49.     for (int i = 1; i <= n; ++i) {
  50.         for (int j = 1; j <= n; ++j) {
  51.             cin >> num[i][j];
  52.         }
  53.     }
  54.     for (int i = 1; i < n; ++i) {
  55.         for (int j = 1; j < n; ++j) {
  56.             if (judge(i, j)) {
  57.                 ++ans;
  58.             }
  59.         }
  60.     }
  61.     tmp = ans;
  62.     k = min(k, 9);
  63.     for (int i = 1; i <= n; ++i) {
  64.         for (int j = 1; j <= n; ++j) {
  65.             int a = cal(i, j);
  66.             int tmpC = num[i][j];
  67.             for (int c = 1; c <= k; ++c) {
  68.                 num[i][j] = c;
  69.                 int b = cal(i, j);
  70.                 ans = max(ans, tmp + b - a);
  71.             }
  72.             num[i][j] = tmpC;
  73.         }
  74.     }
  75.     cout << ans << endl;
  76.  
  77.     return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement