QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471521#5416. Fabulous Fungus Frenzyyz_lyWA 3ms21120kbC++144.0kb2024-07-10 21:44:362024-07-10 21:44:36

Judging History

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

  • [2024-07-10 21:44:36]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:21120kb
  • [2024-07-10 21:44:36]
  • 提交

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){
	if(clock()>10000){
		work(-1);
		exit(0);
	}
	char gg[25][25];
	int flag=0;
	set<pair<int,int> > now[63];
	bool vis[155];
	for(int i=1;i<=num;i++){
		vis[i]=0;
		if(q[i].size()>c[i].size()){
			vis[i]=1;
			flag=1;
		}
	}
	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;
	}
	for(int j=k;j;j--){
		int ff=0;
		for(int i=1;i<=num;i++){
			if(vis[i]){
				if(p[j][i].size())
					ff=1;
			}
		}
		if(!ff)
			continue;
		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;
		ff=0;
		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);
					ff=1;
				}
				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;
			}
		}
		if(!ff)
			continue;
		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];
			}
		}
	}
	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: 4056kb

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: 1ms
memory: 3828kb

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

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:

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

result:

ok puzzle solved

Test #4:

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

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

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

input:

2 2 1
OO
OO

OP
PO

2 2
PP
PP

output:

-1

result:

ok puzzle solved

Test #7:

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

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: -100
Wrong Answer
time: 3ms
memory: 21120kb

input:

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

output:

-1

result:

wrong answer solvable puzzle unsolved