QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471483#5416. Fabulous Fungus Frenzyyz_lyTL 270ms34292kbC++144.0kb2024-07-10 21:31:462024-07-10 21:31:46

Judging History

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

  • [2024-07-10 21:31:46]
  • 评测
  • 测评结果:TL
  • 用时:270ms
  • 内存:34292kb
  • [2024-07-10 21:31:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int f=1,x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')
			f=-f;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}
inline void work(int k){
	if(k<0){
		putchar('-');
		k=-k;
	}
	if(k>9)
		work(k/10);
	putchar(k%10+'0');
}
/*
倒着做,一个完全相同的矩阵可以变成通用符,因为k很小,感觉dfs一下就差不多了
*/
int n,m,k,h[25],w[25],id[155],num;
char s[25][25],t[25][25],g[25][25][25];
set<pair<int,int> > q[63],p[25][63],c[63];
struct node{
	int f,x,y;
};
vector<node> ans;
void solve(int l,int r,int x,int y,set<pair<int,int> > *q){
	q[id[t[l][r]]].erase(make_pair(l,r));
	q[id[t[x][y]]].erase(make_pair(x,y));
	swap(t[l][r],t[x][y]);
	q[id[t[l][r]]].insert(make_pair(l,r));
	q[id[t[x][y]]].insert(make_pair(x,y));
}
void calc(int l,int r,int x,int y,set<pair<int,int> > *q){
	while(r<y){
		solve(l,r,l,r+1,q);
		ans.emplace_back(node{-1,l,r});
		r++;
	}
	while(l<x){
		solve(l,r,l+1,r,q);
		ans.emplace_back(node{-3,l,r});
		l++;
	}
	while(r>y){
		solve(l,r,l,r-1,q);
		ans.emplace_back(node{-2,l,r});
		r--;
	}
	while(l>x){
		solve(l,r,l-1,r,q);
		ans.emplace_back(node{-4,l,r});
		l--;
	}
}
bool dfs(set<pair<int,int> > *q){
	char gg[25][25];
	int flag=0;
	set<pair<int,int> > now[63];
	vector<int> vis;
	for(int i=1;i<=k+1;i++){
		vis.emplace_back(0);
	}
	for(int i=1;i<=num;i++){
		if(q[i].size()>c[i].size()){
			flag=1;
			for(int j=1;j<=k;j++){
				if(!vis[j]&&p[j][i].size()&&q[i].size()+q[0].size()>=p[j][i].size()){
					vis[j]=1;
					for(int x=1;x<=n;x++){
						for(int y=1;y<=m;y++){
							gg[x][y]=t[x][y];
						}
					}
					int sum=0;
					for(int d=0;d<=num;d++){
						now[d]=q[d];
						if(q[d].size()<p[j][d].size())
							sum+=p[j][d].size()-q[d].size();
					}
					if(sum>q[0].size())
						continue;
					vector<node> hh=ans;
					for(int x=1;x<=h[j];x++){
						for(int y=1;y<=w[j];y++){
							if(now[id[g[j][x][y]]].size())
								calc((*now[id[g[j][x][y]]].begin()).first,(*now[id[g[j][x][y]]].begin()).second,x,y,now);
							else
								calc((*now[0].begin()).first,(*now[0].begin()).second,x,y,now);
							now[id[t[x][y]]].erase(make_pair(x,y));
							t[x][y]=0;
						}
					}
					for(int x=1;x<=h[j];x++){
						for(int y=1;y<=w[j];y++){
							now[0].insert(make_pair(x,y));
						}
					}
					ans.emplace_back(node{j,1,1});
					if(dfs(now))
						return true;
					ans=hh;
					for(int x=1;x<=n;x++){
						for(int y=1;y<=m;y++){
							t[x][y]=gg[x][y];
						}
					}
				}
			}
		}
	}
	if(!flag){
		for(int i=0;i<=num;i++){
			now[i]=q[i];
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(now[id[s[i][j]]].size()){
					calc((*now[id[s[i][j]]].begin()).first,(*now[id[s[i][j]]].begin()).second,i,j,now);
					now[id[s[i][j]]].erase(make_pair(i,j));
				}
				else{
					calc((*now[0].begin()).first,(*now[0].begin()).second,i,j,now);
					now[0].erase(make_pair(i,j));
				}
			}
		}
		return true;
	}
	return false;
}
int main(){
	n=read();
	m=read();
	k=read();
	for(int i='0';i<='9';i++){
		id[i]=++num;
	}
	for(int i='a';i<='z';i++){
		id[i]=++num;
	}
	for(int i='A';i<='Z';i++){
		id[i]=++num;
	}
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=m;j++){
			c[id[s[i][j]]].insert(make_pair(i,j));
		}
	}
	for(int i=1;i<=n;i++){
		scanf("%s",t[i]+1);
		for(int j=1;j<=m;j++){
			q[id[t[i][j]]].insert(make_pair(i,j));
		}
	}
	for(int i=1;i<=k;i++){
		h[i]=read();
		w[i]=read();
		for(int j=1;j<=h[i];j++){
			scanf("%s",g[i][j]+1);
		}
		for(int j=1;j<=h[i];j++){
			for(int d=1;d<=w[i];d++){
				p[i][id[g[i][j][d]]].insert(make_pair(j,d));
			}
		}
	}
	if(dfs(q)){
		reverse(ans.begin(),ans.end());
		work(ans.size());
		putchar('\n');
		for(auto i:ans){
			work(i.f);
			putchar(' ');
			work(i.x);
			putchar(' ');
			work(i.y);
			putchar('\n');
		}
	}
	else
		work(-1);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3804kb

input:

3 3 1
OOO
GOG
BGB

OOO
GGG
BBB

3 1
B
G
B

output:

13
-2 3 2
-4 3 3
-1 3 2
-2 2 2
-4 2 3
-1 2 2
-2 1 3
-2 1 2
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: 3688kb

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: 3808kb

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:

222
4 1 1
-2 2 6
-3 1 6
-2 2 5
-2 2 6
-3 1 6
-2 2 4
-2 2 5
-2 2 6
-3 1 6
-4 3 2
-4 4 2
-2 4 3
-2 4 4
-2 4 5
-2 4 6
-2 4 7
-2 4 8
-2 2 2
-2 2 3
-2 2 4
-2 2 5
-2 2 6
-3 1 6
-4 2 3
-4 3 3
-4 4 3
-2 4 4
-2 4 5
-2 4 6
-2 4 7
2 1 1
-4 4 2
-2 4 3
-2 4 4
-4 3 6
-4 4 6
-2 4 7
-4 3 5
-4 4 5
2 1 1
-4 4 8
-1 4 ...

result:

ok puzzle solved

Test #4:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

2 2 1
OO
OO

OP
PP

1 2
PP

output:

8
-4 2 1
-2 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: 4032kb

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: 3804kb

input:

2 2 1
OO
OO

OP
PO

2 2
PP
PP

output:

-1

result:

ok puzzle solved

Test #7:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

2 2 1
OO
OO

OP
PP

1 2
OP

output:

7
1 1 1
-4 2 2
-1 2 1
1 1 1
-4 2 2
-1 2 1
1 1 1

result:

ok puzzle solved

Test #8:

score: 0
Accepted
time: 12ms
memory: 34292kb

input:

20 20 20
bofelagiqboebdqplbhq
qsrksfthhptcmikjohkt
qrnhpoatbekggnkdonet
aoalekbmpbisgflbhmol
djnhnlitcakltqgegqrt
fdctfarsmbnbeosdgilo
ttrsljgiratfmioajorh
gorljkihdnmnofnblfhm
bqjkaehetdjlgctmijpc
geslcskpoqjcgtbdspoa
riqthroqmmhqgprqhsba
fdiarrcomockfqdjjdkd
jsbnigfqgsfekbbnnkir
ildqctqtqrpcetaocp...

output:

9823
10 1 1
-4 2 1
-4 3 1
-4 4 1
-4 5 1
-4 6 1
-4 7 1
-4 8 1
-4 9 1
-4 10 1
-4 11 1
-4 12 1
-4 13 1
-4 14 1
-4 15 1
-4 16 1
-4 17 1
-4 18 1
-4 19 1
-4 20 1
-2 20 2
-2 20 3
-2 20 4
-2 20 5
-2 20 6
-2 20 7
-2 20 8
-2 20 9
-2 20 10
-2 20 11
-2 20 12
-2 20 13
-2 20 14
-2 20 15
-2 20 16
-2 20 17
-2 20 18...

result:

ok puzzle solved

Test #9:

score: 0
Accepted
time: 1ms
memory: 3884kb

input:

20 20 2
HbevPluVL5ORtUFcV9gf
Mrq6zdTPMPnwlN7Fpzx6
Nfp71dVuxTZp9Qet0Ca9
ugbaF34DquDdbUnk5x7V
fDFszn4PmvMpJ5BDWueJ
2YvFxKJgst8XbftPfy9T
F7Q4huk87Lrp1M7i08is
Q41E5AqLkkP3Q5qONXC2
MuM7iIzev3ZpxItvriQK
6OBdEC0sso5vdNQlrCSR
BJQtKjN6RmppsMGIYL81
yyKsWJSoKorGGblNle0r
RkKEQACh8f0bS5nPTtJH
fQgoc39vdsPAUmxlYYL...

output:

-1

result:

ok puzzle solved

Test #10:

score: 0
Accepted
time: 0ms
memory: 4476kb

input:

20 20 2
pqo3Mcpvo74RFSsJszsa
znrYm92Qr8fbqhbCTOgq
4KiMYr0kLAxPGNG15x7L
QHKmq6xaJ4PU4msuRAiv
UBfS6VUO87hRnMAjGXKX
zCgknw3FfhdifmVcT6FF
GH6ohIAzZuprlC3vMDVh
mHIJ9KlQvWxt6EgGbJkA
3SwJNhRSdEeF9BNtc9k2
mZmEuriH7Rc4ccMjqI0Y
cFfI8TC1iM4PkKziLOiN
15CUjuwudnrums3c3dsl
ekL52LiFEpzmU4vaGtuX
CfrnQtWb5zAN9oQS2fj...

output:

10436
-4 18 8
-2 18 9
-2 18 10
-2 18 11
-2 18 12
-2 18 13
-2 18 14
-2 18 15
-2 18 16
-2 18 17
-2 18 18
-2 18 19
-2 18 20
-2 16 7
-2 16 8
-2 16 9
-2 16 10
-2 16 11
-2 16 12
-2 16 13
-2 16 14
-2 16 15
-2 16 16
-2 16 17
-2 16 18
-2 16 19
-2 16 20
-4 14 10
-4 15 10
-4 16 10
-4 17 10
-2 17 11
-2 17 12
-2...

result:

ok puzzle solved

Test #11:

score: 0
Accepted
time: 19ms
memory: 4372kb

input:

20 20 2
7CDCA3gd4c8OE3Zs0VE1
vszVp5b7Vw7NnPisnZYJ
hgfA8K4aV11nlDcDasWj
hg7y388G2MOuTOpEGDBh
DDTjEdOJNQHu2pzbuigf
6kdVkqykU2dDjqjDKD2v
vmaF9cP326rpwhVIl0K6
KchHgQg3BK7Hqt9uLAX4
8klt7U0BZ2C8Ky7DQ5Jo
Ce71gbv3U9nG7pNiwO5T
SII4sonVJ3F34MELKUlD
mLfuG79wBvqb2BKKLoRf
GnBA95Uadz3lO2Dvuob1
NLqlqTyNPTp5sihp2tC...

output:

-1

result:

ok puzzle solved

Test #12:

score: 0
Accepted
time: 270ms
memory: 5004kb

input:

20 20 2
PcYcPItqOwm4yYbBIt9e
iBIDFlswIdU1gSXVvuf7
GB55VjrsjvtPiW1lI0xt
8wDgW4acuIsbjY7McQHg
cpYGIgQ5cI3Ctu4iAJj5
K1KDs608gqVk9EQM6gMF
mJVEd5nuZQnlqLZ5Q2Yc
lo5wptbLMN2J0j3ZENzE
BTQuhuUjyGD1ha8mimg5
i6ixmpshNJ7TyUNjHcKm
bS7CeGdF4L50ZcHyVi7O
0iJYFD57UR6LLANOw7w6
qjwguPgl3YE4wk57cx6f
X5rA3btz798F76GFTPx...

output:

-1

result:

ok puzzle solved

Test #13:

score: 0
Accepted
time: 6ms
memory: 3972kb

input:

20 20 2
31JzWNDIcu64mRA4bbXn
nFHHOpnj3X6pjlT9XjtS
t6kqM1qCdWZlHYvND5AZ
Q580jYLFa8htqsbzmNwu
AnogbQ49yYDbGR5uIcRJ
er06ukvBAFUZ9wspjFdO
t1FndB74Vapme74N9Fhm
TGsrhjfKJ7orOyec8PRa
oraPL0zEQhfHGdSkFuQJ
6RxaAFbZ8kOkvDQgA2yf
rTHnPaAHHluAjaC5Vf41
JGYre0sXkS6W4f5oOVch
7jDPnIDXyLX5ymXMpxo7
AGtFpLNXqPnsO8f4UC2...

output:

-1

result:

ok puzzle solved

Test #14:

score: 0
Accepted
time: 1ms
memory: 3844kb

input:

20 20 5
98XQ4BMDPs7KQbGLQM94
5gAZZoXVtAOhWkV7eVC4
lcJaFORqa6FRYQJuyP2m
pHUWWwwcE5TYjXRD1f32
DwPeneIR7ks5Dq9kOS93
VJ9XAtGqjKxz6ib93VdB
nWGU5rGN8eFWanUFUuz8
8oBdZb4d9bFzPkC6aVZ5
U78CXMM9XkU1qltE2EM4
nkzXLl0pINNatqozmpsu
GoiFmfcKy7lWUpigV87d
63xk0X2RqyPWwc3uSkT7
I4JbBl4DTchy7cFhx07y
KQ5NgDHEwoO2EYyfIkF...

output:

-1

result:

ok puzzle solved

Test #15:

score: 0
Accepted
time: 3ms
memory: 4812kb

input:

20 20 5
VnbxSwLhwt7GUidif4lW
0lIVUk241YBWXNVXIalc
uICMK6WkoB9tDO76DKV2
L8p8DG3IXFSxguONRieG
eutIQAuRQWOEqZc3ycFo
AjUGtn3X6vFViizsHwNj
bESe5O4i0QCUlaLSuVkT
MPaf6lZmcZf38WvUGLHD
bzTdwp4OJVayTmOGCvkv
znkkxaiEncYIADpGlrsB
mnLYGHXEZp1mErfJMeh0
vBi2nEG1SCLHclLTwrqW
agGGIwO0puMF52Jyk0SK
3a3IY7jkpwvjXSRMLy5...

output:

19943
-4 9 2
-4 10 2
-4 11 2
-4 12 2
-4 13 2
-4 14 2
-4 15 2
-4 16 2
-4 17 2
-4 18 2
-4 19 2
-4 20 2
-2 20 3
-2 20 4
3 1 1
-2 16 12
-3 15 12
-3 14 12
-3 13 12
-3 12 12
-3 11 12
-3 10 12
-3 9 12
-3 8 12
-3 7 12
-3 6 12
-3 5 12
-3 4 12
-3 3 12
-3 2 12
-3 1 12
-2 16 11
-2 16 12
-3 15 12
-3 14 12
-3 13 ...

result:

ok puzzle solved

Test #16:

score: 0
Accepted
time: 0ms
memory: 4460kb

input:

20 20 5
zzh9PnCVcvoyTl0XNqBR
NhRWLcbdGlpC2hYU4p7z
Q9zsPBWaybIU9LzvEJOv
cGYogJA6gn379WLXDlps
UE28n4kYuBi60G3VpJ3y
fXFdrdzuLkCclp1Qucaa
cb8vXeoIVEISF1z63mXI
akRc9rUDqJThkHgM3Glf
o0MX2ThxnjB0vjgUgzOR
PD5PmQv52G9lu5pEvwoI
2nah8lAHqPGAwxocL8kQ
Ug6vDhj7gtLnPHtrhCKQ
xjAbQPvwYHhE71R580zS
iHeGypUWdQkxoUmnLPA...

output:

10013
-2 20 7
-2 20 8
-2 20 9
-2 20 10
-2 20 11
-2 20 12
-2 20 13
-2 20 14
-2 20 15
-2 20 16
-2 20 17
-2 20 18
-2 20 19
-4 17 16
-4 18 16
-4 19 16
-4 20 16
-1 20 15
-1 20 14
-1 20 13
-4 15 14
-4 16 14
-4 17 14
-4 18 14
-4 19 14
-4 20 14
-2 20 15
-2 20 16
-4 15 9
-4 16 9
-4 17 9
-4 18 9
-4 19 9
-4 20...

result:

ok puzzle solved

Test #17:

score: 0
Accepted
time: 4ms
memory: 4248kb

input:

20 20 5
cz7yfUxuJVlDaZw6BPsE
anVcSZ5DDJPMPJjAEljh
mWcTQaRUJ21VBsk9LtMn
IU2kOtF8GrxsO4Ap77yc
aGzhzRS8gjFAsFNP8nAm
5G1OJUh0WhP43mJzhSj1
o55Q582zXKwA7idbCXyI
LvXe3CGvyW2YRTsE7KZ2
8yPYtMObYNoli6LvAYcn
TEba0LujH9bXK0S4q7pd
htdrztum0MdvYWaNJ2EJ
gX6XBJOPOFMIbtxHaVQX
cuaLLQVLomjYM1XQhfrx
v1zIJ7H4lHpG9W1xbxE...

output:

9868
-2 20 3
-2 20 4
-2 20 5
-2 20 6
-2 20 7
-2 20 8
-2 20 9
-2 20 10
-2 20 11
-2 20 12
-2 20 13
-2 20 14
-2 20 15
-2 20 16
-2 20 17
-2 20 18
-2 20 19
-2 20 20
-4 18 17
-2 18 18
-2 18 19
-2 18 20
-4 18 9
-4 19 9
-4 20 9
-2 20 10
-2 20 11
-2 20 12
-2 20 13
-2 20 14
-2 20 15
-2 16 13
-2 16 14
-2 16 15...

result:

ok puzzle solved

Test #18:

score: -100
Time Limit Exceeded

input:

20 20 5
us00j7HM7PdH2WRz3xcM
ejN853WC2u56Ob8OhaFY
dvfiTNQ0vxAvSuOCKuPH
hxt33vcCeNyWRCHbl8nC
zI3R2j64CEb8N6O1oU41
qYchY8MtgwTJZil7DlS7
vmKw9IE8U8yFVsqUVAVW
7No9WxCuw4oKt4yMFLhu
335m5dxtgl4WH5qpS3M5
DAfNe1hS6J0lDJS5j5pa
BxSda2Jrvmy5aZdkS8FW
JiifhY3xqGMTvgPhsKr8
Kn8gzxeoP5OVO2PwxfKh
cuxJdH5sFnExQUAW7ge...

output:


result: