QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#473918 | #5416. Fabulous Fungus Frenzy | hyxle | WA | 5ms | 4148kb | C++14 | 4.7kb | 2024-07-12 15:00:49 | 2024-07-12 15:00:49 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define R register
#define PII pair<int,int>
#define mk(x,y) make_pair(x,y)
using namespace std;
const int N=25,range=65;
const int xx[]={0,0,0,1,-1},yy[]={0,1,-1,0,0};
int n,m,k;
struct Matrix{
int h,w,cnt[range];char a[N][N];
inline char to_ch(int x){
if(!x)return '*';
if(x>0&&x<27)return char(x+'a'-1);
if(x>26&&x<53)return char(x-27+'A');
return char(x-53+'0');
}
inline int to_num(char x){
if(x=='*')return 0;
if(x>='a'&&x<='z')return x-'a'+1;
if(x>='A'&&x<='Z')return x-'A'+27;
return x-'0'+53;
}
inline void getmat(){
memset(cnt,0,sizeof cnt);
for(R int i=1;i<=h;++i)for(R int j=1;j<=w;++j){char ch;cin>>ch;a[i][j]=to_num(ch);++cnt[to_num(ch)];}
}
}st,ed,tu[N];
vector<pair<int,PII> > res;
inline void Swap(PII which,int dir){
res.emplace_back(mk(dir,mk(which.first,which.second)));
// cerr<<which.first<<' '<<which.second<<' '<<which.first+xx[-dir]<<' '<<which.second+yy[-dir]<<'\n';
swap(ed.a[which.first][which.second],ed.a[which.first+xx[-dir]][which.second+yy[-dir]]);
}
inline void Push(int which_tu,PII x,PII y){//按压图章
res.emplace_back(mk(which_tu,x));
for(R int i=x.first;i<=y.first;++i){
for(R int j=x.second;j<=y.second;++j){
--ed.cnt[ed.a[i][j]];ed.a[i][j]=0;++ed.cnt[0];
}
}
}
inline bool can(int which_tu){//判断能否盖这个章
int sum=0,sum2=0;
for(R int i=1;i<range;++i){
if(tu[which_tu].cnt[i]>=ed.cnt[i])sum+=tu[which_tu].cnt[i]-ed.cnt[i];
if(ed.cnt[i]&&tu[which_tu].cnt[i]){
sum2=1;//需要用上
}
}
return (sum2&&(sum<=ed.cnt[0]));//缺的能用通配符插入且有必要盖
}
int edx,edy;
inline void swap_(int sx,int sy,int ex,int ey,int opt){
if(sy>ey){
while(sx<ex)Swap(mk(sx,sy),-3),++sx;
while(sx>ex)Swap(mk(sx,sy),-4),--sx;
while(sy<ey)Swap(mk(sx,sy),-1),++sy;
while(sy>ey)Swap(mk(sx,sy),-2),--sy;
}else{
while(sy<ey)Swap(mk(sx,sy),-1),++sy;
while(sy>ey)Swap(mk(sx,sy),-2),--sy;
while(sx<ex)Swap(mk(sx,sy),-3),++sx;
while(sx>ex)Swap(mk(sx,sy),-4),--sx;
}
}
inline void output(Matrix x){
for(R int i=1;i<=n;++i){
for(R int j=1;j<=m;++j){
cerr<<ed.to_ch(ed.a[i][j])<<' ';
}
cerr<<'\n';
}
cerr<<'\n';
}
inline void SWAP(PII x,PII y){//不影响原局面的情况下交换两个点
swap_(x.first,x.second,y.first,y.second,1);
}
inline void turn_to(int which_tu){//将某一个图章凑齐
for(R int i=1;i<=tu[which_tu].h;++i){
for(R int j=1;j<=tu[which_tu].w;++j){
if(ed.a[i][j]!=tu[which_tu].a[i][j]){//不一样
bool fl=0;
for(R int l=1;l<=n;++l){
if(fl)break;
for(R int o=1;o<=m;++o){
if(l<=tu[which_tu].h&&o<=tu[which_tu].w)continue;
if(ed.a[l][o]==tu[which_tu].a[i][j]){
fl=1;SWAP(mk(l,o),mk(i,j));break;
}
}
}
if(!fl){
for(R int l=1;l<=tu[which_tu].h;++l){
if(fl)break;
for(R int o=1;o<=tu[which_tu].w;++o){
if(l<=i&&o<=j)continue;//已经排好了,不能动
if(ed.a[l][o]==tu[which_tu].a[i][j]){
fl=1;SWAP(mk(l,o),mk(i,j));break;
}
}
}
}
if(!fl){//只能找*
for(R int l=1;l<=n;++l){
if(fl)break;
for(R int o=1;o<=m;++o){
if(l<=tu[which_tu].h&&o<=tu[which_tu].w)continue;
if(ed.a[l][o]==0){
fl=1;SWAP(mk(l,o),mk(i,j));break;
}
}
}
}
if(!fl){
for(R int l=1;l<=tu[which_tu].h;++l){
if(fl)break;
for(R int o=1;o<=tu[which_tu].w;++o){
if(l<=i&&o<=j)continue;//已经排好了,不能动
if(ed.a[l][o]==0){
fl=1;SWAP(mk(l,o),mk(i,j));break;
}
}
}
}
}
}
}
// output(ed);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
cin>>n>>m>>k;st.h=ed.h=n,st.w=ed.w=m;
st.getmat(),ed.getmat();tu[0].h=n,tu[0].w=m;
for(R int i=1;i<=n;++i){
for(R int j=1;j<=m;++j){
tu[0].a[i][j]=st.a[i][j];
}
}
for(R int i=0;i<range;++i)tu[0].cnt[i]=st.cnt[i];
for(R int i=1;i<=k;++i){cin>>tu[i].h>>tu[i].w;tu[i].getmat();}
while(1){
bool fl=0;
for(R int i=1;i<=k;++i){
if(can(i)){
fl=1;
// cerr<<i<<'\n';
// output(ed);
turn_to(i);Push(i,mk(1,1),mk(tu[i].h,tu[i].w));
// output(ed);
}
}
if(!fl)break;
}
int sum=0;
for(R int i=1;i<range;++i){
if(st.cnt[i]!=ed.cnt[i]){
if(st.cnt[i]>ed.cnt[i]){
sum+=st.cnt[i]-ed.cnt[i];
}
if(ed.cnt[i]>st.cnt[i])sum=-2147483647;
}
}
if(sum<ed.cnt[0]){
cout<<"-1\n";
return 0;
}
if(can(0))turn_to(0);
int len=res.size();
cout<<len<<'\n';
for(R int i=len-1;i>=0;--i)cout<<res[i].first<<' '<<res[i].second.first<<' '<<res[i].second.second<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
input:
3 3 1 OOO GOG BGB OOO GGG BBB 3 1 B G B
output:
41 -2 3 3 -3 2 3 -3 1 3 -2 3 2 -2 3 3 -3 2 3 -3 1 3 -4 3 3 -1 3 2 -1 3 1 -2 2 3 -3 1 3 -2 2 2 -4 2 3 -4 3 3 -2 1 3 -4 2 3 -2 1 2 -2 1 3 1 1 1 -2 3 2 -3 2 2 -3 1 2 -2 2 2 -2 2 3 -4 3 3 -2 1 2 1 1 1 -2 3 2 -3 2 2 -3 1 2 -2 2 2 -4 3 2 -2 1 2 -2 1 3 -4 2 3 -4 3 3 1 1 1 -2 1 2 -4 2 2 -4 3 2
result:
ok puzzle solved
Test #2:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
2 2 1 OO OO PP PP 1 2 OP
output:
-1
result:
ok puzzle solved
Test #3:
score: 0
Accepted
time: 0ms
memory: 3612kb
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:
214 3 1 1 -2 1 2 -2 1 3 -2 1 4 -2 1 5 -4 2 5 -4 3 5 -4 4 5 1 1 1 -4 3 3 -4 4 3 -1 4 2 -2 2 3 -2 2 4 -3 1 4 -2 2 2 -2 2 3 -2 2 4 -3 1 4 -2 1 4 -2 1 3 -2 1 4 -2 1 2 -2 1 3 -2 1 4 3 1 1 -4 2 1 -4 3 1 -4 4 1 2 1 1 -4 4 8 -1 4 7 -1 4 6 -1 4 5 -1 4 4 -1 4 3 -4 4 7 -1 4 6 -1 4 5 -1 4 4 -1 4 3 -1 4 2 -4 4 6...
result:
ok puzzle solved
Test #4:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
2 2 1 OO OO OP PP 1 2 PP
output:
11 -2 2 2 -3 1 2 -4 2 2 -1 2 1 -4 2 1 1 1 1 -4 2 2 -2 1 2 -4 2 2 1 1 1 -4 2 1
result:
ok puzzle solved
Test #5:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
2 2 1 OO OO OP PO 2 1 P P
output:
6 -2 2 2 -3 1 2 -4 2 2 -2 1 2 1 1 1 -2 1 2
result:
ok puzzle solved
Test #6:
score: 0
Accepted
time: 0ms
memory: 3648kb
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: 3544kb
input:
2 2 1 OO OO OP PP 1 2 OP
output:
10 1 1 1 -4 2 2 -1 2 1 -2 1 2 -4 2 2 1 1 1 -4 2 2 -1 2 1 -2 1 2 1 1 1
result:
ok puzzle solved
Test #8:
score: 0
Accepted
time: 1ms
memory: 3748kb
input:
20 20 20 bofelagiqboebdqplbhq qsrksfthhptcmikjohkt qrnhpoatbekggnkdonet aoalekbmpbisgflbhmol djnhnlitcakltqgegqrt fdctfarsmbnbeosdgilo ttrsljgiratfmioajorh gorljkihdnmnofnblfhm bqjkaehetdjlgctmijpc geslcskpoqjcgtbdspoa riqthroqmmhqgprqhsba fdiarrcomockfqdjjdkd jsbnigfqgsfekbbnnkir ildqctqtqrpcetaocp...
output:
8497 8 1 1 -2 1 2 -2 1 3 -2 1 4 -2 1 5 -2 1 6 -2 1 7 -2 1 8 -2 1 9 -2 1 10 -2 1 11 -2 1 12 -2 1 13 -4 2 13 -4 3 13 -4 4 13 -4 5 13 -4 6 13 -4 7 13 -4 8 13 -4 9 13 -4 10 13 -4 11 13 -4 12 13 -4 13 13 -4 14 13 -4 15 13 -4 16 13 -4 17 13 -4 18 13 -4 19 13 -4 20 13 8 1 1 -2 1 2 -2 1 3 -2 1 4 -2 1 5 -2 1...
result:
ok puzzle solved
Test #9:
score: 0
Accepted
time: 0ms
memory: 3596kb
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: -100
Wrong Answer
time: 5ms
memory: 4148kb
input:
20 20 2 pqo3Mcpvo74RFSsJszsa znrYm92Qr8fbqhbCTOgq 4KiMYr0kLAxPGNG15x7L QHKmq6xaJ4PU4msuRAiv UBfS6VUO87hRnMAjGXKX zCgknw3FfhdifmVcT6FF GH6ohIAzZuprlC3vMDVh mHIJ9KlQvWxt6EgGbJkA 3SwJNhRSdEeF9BNtc9k2 mZmEuriH7Rc4ccMjqI0Y cFfI8TC1iM4PkKziLOiN 15CUjuwudnrums3c3dsl ekL52LiFEpzmU4vaGtuX CfrnQtWb5zAN9oQS2fj...
output:
33386 2 1 1 -2 20 15 -3 19 15 -3 18 15 -3 17 15 -3 16 15 -3 15 15 -3 14 15 -3 13 15 -3 12 15 -3 11 15 -3 10 15 -3 9 15 -3 8 15 -3 7 15 -3 6 15 -3 5 15 -3 4 15 -3 3 15 -3 2 15 -3 1 15 -2 20 14 -2 20 15 -3 19 15 -3 18 15 -3 17 15 -3 16 15 -3 15 15 -3 14 15 -3 13 15 -3 12 15 -3 11 15 -3 10 15 -3 9 15 -...
result:
wrong answer puzzle remain unsolved