結果

問題 No.3335 ReCT
コンテスト
ユーザー テナガザル
提出日時 2025-11-07 23:02:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 5,467 bytes
コンパイル時間 1,078 ms
コンパイル使用メモリ 112,588 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-07 23:02:22
合計ジャッジ時間 6,083 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 96 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int tt[3][6] = {
  {1, 2, 2, 2, 2, 4},
  {1, 1, 2, 3, 4, 4},
  {1, 3, 3, 3, 3, 4},
};
const int ttc[4][4] = {
  {1, 1, 1, 2},
  {4, 1, 2, 2},
  {4, 4, 3, 2},
  {4, 3, 3, 3},
};
vector<vector<int>> ttd = {
  {1, 1, 1, 1, 2},
  {3, 1, 2, 2, 2},
  {3, 3, 3, 4, 2},
  {3, 4, 4, 4, 4},
};
vector<vector<int>> tcc = {
  {1, 1, 1, 2},
  {1, 2, 2, 2},
  {1, 1, 3, 2},
  {3, 3, 3, 3},
};

int main()
{
  int h, w;
  cin >> h >> w;
  if (min(h, w) <= 2)
  {
    cout << "-1\n-1\n-1" << endl;
    return 0;
  }
  int flag = (h > w);
  if (flag) swap(h, w);
  auto pri = [&](vector<vector<int>> &a) -> void
  {
    if (flag) for (int i = 0; i < w; ++i) for (int j = 0; j < h; ++j) cout << a[j][i] << " \n"[j == h - 1];
    else for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cout << a[i][j] << " \n"[j == w - 1];
  };
  {
    if (h <= 2)
    {
      cout << "-1\n";
    }
    else if (h == 3)
    {
      if (w <= 3 || w % 2 == 1) cout << "-1\n";
      else
      {
        cout << w / 2 << endl;
        vector<vector<int>> ans(h, vector<int> (w));
        for (int i = 0; i < 3; ++i) ans[0][i] = ans[i][0] = 1;
        ans[1][2] = 1;
        int id = 2;
        for (int i = 1; i + 3 < w; i += 4)
        {
          ans[1][i] = ans[1][i + 3] = id;
          for (int j = i; j <= i + 3; ++j) ans[2][j] = id;
          id += 2;
        }
        id = 3;
        for (int i = 3; i + 3 < w; i += 4)
        {
          ans[1][i] = ans[1][i + 3] = id;
          for (int j = i; j <= i + 3; ++j) ans[0][j] = id;
          id += 2;
        }
        id = w / 2;
        for (int i = w - 3; i < w; ++i)
        {
          for (int j = 0; j < 3; ++j) if (ans[j][i] == 0) ans[j][i] = id;
        }
        pri(ans);
      }
    }
    else
    {
      vector<vector<int>> ans(h, vector<int> (w));
      auto dfs = [&](auto f, int x1, int x2, int y1, int y2, int id) -> void
      {
        if (x2 - x1 > 4)
        {
          if (x2 - x1 == 5)
          {
            for (int i = x1; i < x2; ++i) ans[i][y1] = ans[i][y2 - 1] = id;
            for (int i = y1; i < y2; ++i) ans[x2 - 1][i] = id;
            f(f, x1, x2 - 1, y1 + 1, y2 - 1, id + 1);
          }
          else
          {
            for (int i = y1; i < y2; ++i) ans[x1][i] = ans[x2 - 1][i] = id;
            for (int i = x1; i < x2; ++i) ans[i][y2 - 1] = id;
            f(f, x1 + 1, x2 - 1, y1, y2 - 1, id + 1);
          }
        }
        else
        {
          for (int i = y1; i < y2; ++i) ans[x1][i] = ans[x2 - 2][i] = id;
          for (int i = y1; i < y2; ++i) ans[x1 + 1][i] = ans[x2 - 1][i] = id + 1;
          ans[x1 + 1][y2 - 1] = id;
          ans[x2 - 2][y1] = id + 1;
          return;
        }
      };
      dfs(dfs, 0, h, 0, w, 1);
      int ma = 0;
      for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) ma = max(ans[i][j], ma);
      cout << ma << endl;
      pri(ans);
    }
  }
  {
    if (w >= 6)
    {
      vector<vector<int>> ans(h, vector<int> (w));
      int id = 0;
      for (int i = 0; i + 2 < h; i += 3)
      {
        for (int j = 0; j < 3; ++j) for (int k = 0; k < 4; ++k) ans[i + j][k] = tt[j][k] + id;
        for (int j = 4; j < w - 1; ++j) for (int k = 0; k < 3; ++k) ans[i + k][j] = tt[k][4] + id;
        for (int j = 0; j < 3; ++j) ans[i + j][w - 1] = tt[j][5] + id;
        id += 4;
      }
      if (h % 3 == 1)
      {
        ans[h - 2][1] = id + 1;
        for (int i = 0; i < w; ++i) ans[h - 1][i] = id + 1;
        ++id;
      }
      else if (h % 3 == 2)
      {
        ans[h - 3][1] = id + 1;
        for (int i = 0; i < w; ++i) ans[h - 2][i] = id + 1;
        ans[h - 2][w - 1] = ans[h - 3][w - 1];
        ans[h - 2][w - 2] = id + 2;
        for (int i = 0; i < w; ++i) ans[h - 1][i] = id + 2;
        id += 2;
      }
      cout << id << endl;
      pri(ans);
    }
    else
    {
      if (h == 4 && w == 4)
      {
        cout << 4 << endl;
        for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) cout << ttc[i][j] << " \n"[j == 3];
      }
      else if (h == 4 && w == 5)
      {
        cout << 4 << endl;
        pri(ttd);
      }
      else
      {
        cout << -1 << endl;
      }
    }
  }
  {
    if (h == 4 && w == 4)
    {
      cout << 3 << endl;
      pri(tcc);
    }
    else
    {

      vector<vector<int>> ans(h, vector<int> (w));
      auto dfs = [&](auto f, int x1, int x2, int y1, int y2, int id) -> void
      {
        if (x2 - x1 > 3)
        {
          if (x2 - x1 == 4)
          {
            for (int i = x1; i < x2; ++i) ans[i][y1] = ans[i][y2 - 1] = id;
            for (int i = y1; i < y2; ++i) ans[x2 - 1][i] = id;
            f(f, x1, x2 - 1, y1 + 1, y2 - 1, id + 1);
          }
          else
          {
            for (int i = y1; i < y2; ++i) ans[x1][i] = ans[x2 - 1][i] = id;
            for (int i = x1; i < x2; ++i) ans[i][y2 - 1] = id;
            f(f, x1 + 1, x2 - 1, y1, y2 - 1, id + 1);
          }
        }
        else
        {
          for (int i = y1; i < y2 - 1; ++i) ans[x1][i] = ans[x2 - 1][i] = id;
          for (int i = y1 + 1; i < y2; ++i) ans[x1 + 1][i] = id + 1;
          ans[x1 + 1][y1] = id;
          ans[x2 - 1][y2 - 1] = ans[x1][y2 - 1] = id + 1;
          return;
        }
      };
      dfs(dfs, 0, h, 0, w, 1);
      int ma = 0;
      for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) ma = max(ans[i][j], ma);
      cout << ma << endl;
      pri(ans);
    }
  }
}
0