QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#338817 | #5416. Fabulous Fungus Frenzy | goodier | WA | 1ms | 5796kb | C++14 | 5.3kb | 2024-02-26 12:44:29 | 2024-02-26 12:44:32 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
using namespace std;
typedef pair<int,int> PII;
const int N = 65, M = 4e5 + 10, S = 'z' + 1, C = 62;
int a[N][N][N], b[N][N], cntb[N], c[N][N][N], cntc[N][N], top, v[N], n, m, k;
int id[S], idx;
PII it[N], sz[N];
char str[N];
struct Op{
int k, x, y;
}stk[M];
void make_move(int& x, int& y, int dx, int dy)
{
int nx = x + dx, ny = y + dy;
int nowc = b[x][y], nxtc = b[nx][ny];
a[nowc][x][y] = 0, a[nowc][nx][ny] = 1;
a[nxtc][nx][ny] = 0, a[nxtc][x][y] = 1;
swap(b[x][y], b[nx][ny]);
x = nx, y = ny;
}
void f(PII st, PII ed)
{
int x1 = st.x, y1 = st.y, x2 = ed.x, y2 = ed.y;
if(y1 >= y2)
{
while(x1 < x2)
{
stk[++top] = Op({-3, x1, y1});
make_move(x1, y1, 1, 0);
}
while(x1 > x2)
{
stk[++top] = Op({-4, x1, y1});
make_move(x1, y1, -1, 0);
}
while(y1 > y2)
{
stk[++top] = Op({-2, x1, y1});
make_move(x1, y1, 0, -1);
}
}
else
{
while(y1 > y2)
{
stk[++top] = Op({-2, x1, y1});
make_move(x1, y1, 0, -1);
}
while(y1 < y2)
{
stk[++top] = Op({-1, x1, y1});
make_move(x1, y1, 0, 1);
}
while(x1 > x2)
{
stk[++top] = Op({-4, x1, y1});
make_move(x1, y1, -1, 0);
}
}
}
PII getnxt(int c)
{
int i = it[c].x, j = it[c].y;
while(1)
{
j++;
if(j > m) i++, j = 1;
if(i > n) return it[c] = mp(i, j);
if(a[c][i][j]) break;
}
it[c] = mp(i, j);
return it[c];
}
void input(int idc)
{
int n = sz[idc].x, m = sz[idc].y;
for(int i = 1; i <= n; i++)
{
scanf("%s", str + 1);
for(int j = 1; j <= m; j++)
{
c[idc][i][j] = id[str[j]];
cntc[idc][c[idc][i][j]]++;
}
}
}
int valid(int idc)
{
int cnt0 = cntb[0];
for(int i = 1; i <= C; i++)
{
if(cntb[i] < cntc[idc][i])
{
if(cntb[i] + cnt0 < cntc[idc][i])
{
return 0;
}
cnt0 -= cntc[idc][i] - cntb[i];
}
}
return 1;
}
void output()
{
printf("%d\n", top);
while(top)
{
printf("%d %d %d\n", stk[top].k, stk[top].x, stk[top].y);
top--;
}
}
int ok()
{
int cnt[N]; memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cnt[b[i][j]]++;
}
}
for(int i = 0; i <= C; i++) if(cnt[i] != cntb[i]) return 0;
return 1;
}
int main()
{
//freopen("data.in", "r", stdin);
for(int i = 'A'; i <= 'Z'; i++) id[i] = ++idx;
for(int i = 'a'; i <= 'z'; i++) id[i] = ++idx;
for(int i = '0'; i <= '9'; i++) id[i] = ++idx;
scanf("%d%d%d", &n, &m, &k);
sz[0] = mp(n, m);
input(0);
for(int i = 1; i <= n; i++)
{
scanf("%s", str + 1);
for(int j = 1; j <= m; j++)
{
b[i][j] = id[str[j]];
a[b[i][j]][i][j] = 1;
cntb[b[i][j]]++;
}
}
for(int i = 1; i <= k; i++)
{
scanf("%d%d", &sz[i].x, &sz[i].y);
input(i);
}
while(1)
{
if(cntb[0] == n * m)
{
output();
return 0;
}
int flag = 0;
for(int p = 0; p <= k; p++)
{
if(v[p]) continue;
if(valid(p))
{
for(int i = 0; i <= C; i++) it[i] = mp(1, 0);
int n1 = sz[p].x, m1 = sz[p].y;
for(int i = 1; i <= n1; i++)
{
for(int j = 1; j <= m1; j++)
{
PII nxt = getnxt(c[p][i][j]);
if(nxt.x > n) nxt = getnxt(0);
f(nxt, mp(i, j));
}
}
if(p) stk[++top] = Op({p, 1, 1});
for(int i = 1; i <= n1; i++)
{
for(int j = 1; j <= m1; j++)
{
int nowc = b[i][j];
a[nowc][i][j] = 0; a[0][i][j] = 1;
b[i][j] = 0;
cntb[nowc]--, cntb[0]++;
}
}
for(int i = 0; i <= C; i++) it[i] = mp(1, 0);
for(int i = 1; i <= n1; i++)
{
for(int j = 1; j <= m1; j++)
{
int cc = c[p][i][j];
while(cntb[cc])
{
f(getnxt(cc), mp(i, j));
stk[++top] = Op({p, 1, 1});
cntb[cc]--, cntb[0]++;
b[i][j] = 0; a[cc][i][j] = 0, a[0][i][j] = 1;
}
}
}
flag = v[p] = 1;
break;
}
}
if(!flag)
{
puts("-1");
return 0;
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5796kb
input:
3 3 1 OOO GOG BGB OOO GGG BBB 3 1 B G B
output:
23 -4 2 3 -4 3 3 -4 2 2 -4 3 2 -2 1 2 -2 1 3 1 1 1 -2 2 2 -2 2 3 -4 3 3 1 1 1 -2 2 2 -4 3 2 1 1 1 -2 1 2 -2 1 3 -4 2 3 -4 3 3 1 1 1 -2 3 2 -2 2 2 -4 2 1 -4 3 1
result:
ok puzzle solved
Test #2:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
2 2 1 OO OO PP PP 1 2 OP
output:
-1
result:
ok puzzle solved
Test #3:
score: 0
Accepted
time: 1ms
memory: 3984kb
input:
4 8 4 11122222 33344444 55556666 77777777 NIxSHUOx DExDUIxx DANxSHIx YUANSHEN 2 3 NIy DEx 3 8 zzzzzzzz DANNSH9I YUA9SHEN 1 1 x 2 5 SHO8y DUUI8
output:
179 4 1 1 -2 2 4 -2 2 5 -2 2 6 -2 2 7 -2 2 8 -3 1 8 -2 2 3 -2 2 4 -2 2 5 -2 2 6 -2 2 7 -3 1 7 -2 2 2 -2 2 3 -2 2 4 -2 2 5 -2 2 6 -3 1 6 -2 1 4 -2 1 5 -2 1 6 -2 1 7 -2 1 8 -4 2 8 -4 3 8 -4 4 8 2 1 1 -2 3 3 -2 3 4 -2 3 5 -2 3 6 -2 3 7 -4 4 7 2 1 1 -4 4 2 2 1 1 -4 3 6 -4 4 6 2 1 1 -4 3 5 -4 4 5 2 1 1 -...
result:
ok puzzle solved
Test #4:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
2 2 1 OO OO OP PP 1 2 PP
output:
8 -2 1 2 -4 2 2 1 1 1 -4 2 1 1 1 1 -4 2 2 -1 2 1 -2 1 2
result:
ok puzzle solved
Test #5:
score: 0
Accepted
time: 0ms
memory: 3924kb
input:
2 2 1 OO OO OP PO 2 1 P P
output:
4 -4 2 2 -2 1 2 1 1 1 -2 1 2
result:
ok puzzle solved
Test #6:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
2 2 1 OO OO OP PO 2 2 PP PP
output:
-1
result:
ok puzzle solved
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3816kb
input:
2 2 1 OO OO OP PP 1 2 OP
output:
8 1 1 1 -4 2 2 -4 3 2 -1 3 1 1 1 1 -4 2 2 -1 2 1 1 1 1
result:
wrong answer Integer parameter [name=x_2] equals to 3, violates the range [1, 2]