QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118097#5416. Fabulous Fungus Frenzyhos_lyricWA 1ms3628kbC++145.0kb2023-07-03 03:26:052023-07-03 03:26:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-03 03:26:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3628kb
  • [2023-07-03 03:26:05]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


constexpr int A = 'z' - '0' + 1;
static_assert(A == 75);

int M, N, K;
char S[30][30], T[30][30];
int P[30], Q[30];
char U[30][30][30];

int freqS[A], freqT[A], freqU[30][A];

vector<pair<int, pair<int, int>>> ops;
char c[30][30];
void tate(int x, int y) {
  ops.emplace_back(-3, make_pair(x, y));
  swap(c[x][y], c[x + 1][y]);
}
void yoko(int x, int y) {
  ops.emplace_back(-1, make_pair(x, y));
  swap(c[x][y], c[x][y + 1]);
}

// move gs to top-left
void preset(int k, int *gs) {
  // keep wild in top-left
  int sx = 0, sy = 0;
  for (int a = A; a >= 0; --a) {
    for (int g = 0; g < gs[a]; ++g) {
      for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
        if (c[x][y] == '0' + a) {
          for (; sx > x; ) tate(x++, y);
          for (; sy > y; ) yoko(x, y++);
          for (; sx < x; ) tate(--x, y);
          for (; sy < y; ) yoko(x, --y);
          goto foundChar;
        }
      }
      assert(false);
     foundChar:{}
      assert(c[sx][sy] == '0' + a);
      c[sx][sy] = '#';
      if (++sy == Q[k]) {
        ++sx;
        sy = 0;
      }
    }
  }
  assert(sx == P[k] && sy == 0);
  ops.emplace_back(k + 1, make_pair(0, 0));
  for (int x = 0; x < P[k]; ++x) for (int y = 0; y < Q[k]; ++y) {
    c[x][y] = '0' + A;
  }
}

bool solve() {
  ops.clear();
  memcpy(c, T, sizeof(T));
  
  memset(freqS, 0, sizeof(freqS));
  memset(freqT, 0, sizeof(freqT));
  memset(freqU, 0, sizeof(freqU));
  for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) ++freqS[S[x][y] - '0'];
  for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) ++freqT[T[x][y] - '0'];
  for (int k = 0; k < K; ++k) {
    for (int x = 0; x < P[k]; ++x) for (int y = 0; y < Q[k]; ++y) ++freqU[k][U[k][x][y] - '0'];
  }
  
  // A: wild
  int fs[A + 1] = {}, gs[A + 1] = {};
  copy(freqT, freqT + A, fs);
  for (; ; ) {
// cerr<<"fs = ";pv(fs,fs+A+1);for(int x=0;x<M;++x)cerr<<c[x]<<endl;
    {
      bool ok = true;
      for (int a = 0; a < A; ++a) {
        ok = ok && (fs[a] <= freqS[a]);
      }
      if (ok) {
        break;
      }
    }
    for (int k = 0; k < K; ++k) {
      // preset -> wild (make >= 1 wild)
      fill(gs, gs + (A + 1), 0);
      for (int a = 0; a < A; ++a) {
        gs[a] = min(fs[a], freqU[k][a]);
        gs[A] += (freqU[k][a] - gs[a]);
      }
      if (gs[A] <= fs[A] && gs[A] < P[k] * Q[k]) {
        preset(k, gs);
        for (int a = 0; a <= A; ++a) {
          fs[a] -= gs[a];
        }
        fs[A] += P[k] * Q[k];
        goto foundPreset;
      }
    }
    return false;
   foundPreset:{}
  }
  
  // match S
  for (int sx = 0; sx < M; ++sx) for (int sy = 0; sy < N; ++sy) {
    for (const char want : {S[sx][sy], (char)('0' + A)}) {
      for (int x = 0; x < M; ++x) for (int y = 0; y < N; ++y) {
        if (c[x][y] == want) {
          for (; sx > x; ) tate(x++, y);
          for (; sy > y; ) yoko(x, y++);
          for (; sx < x; ) tate(--x, y);
          for (; sy < y; ) yoko(x, --y);
          goto foundCharS;
        }
      }
    }
    assert(false);
   foundCharS:{}
    c[sx][sy] = '#';
  }
  
  reverse(ops.begin(), ops.end());
  return true;
}

int main() {
  for (; ~scanf("%d%d%d", &M, &N, &K); ) {
    for (int x = 0; x < M; ++x) {
      scanf("%s", S[x]);
    }
    for (int x = 0; x < M; ++x) {
      scanf("%s", T[x]);
    }
    for (int k = 0; k < K; ++k) {
      scanf("%d%d", &P[k], &Q[k]);
      for (int x = 0; x < P[k]; ++x) {
        scanf("%s", U[k][x]);
      }
    }
    
    const bool ans = solve();
    if (ans) {
      printf("%d\n", (int)ops.size());
      for (const auto &op : ops) {
        printf("%d %d %d\n", op.first, op.second.first + 1, op.second.second + 1);
      }
    } else {
      puts("-1");
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3628kb

input:

3 3 1
OOO
GOG
BGB

OOO
GGG
BBB

3 1
B
G
B

output:

12
-1 3 1
-3 2 3
-1 2 1
-3 1 3
-3 2 3
-1 3 2
-1 1 2
-1 1 1
1 1 1
-1 3 1
-3 2 1
-3 1 1

result:

wrong answer puzzle remain unsolved