QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#627644 | #5416. Fabulous Fungus Frenzy | lfyszy | RE | 0ms | 0kb | C++14 | 3.4kb | 2024-10-10 16:35:36 | 2024-10-10 16:35:37 |
answer
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
using ll=long long;
using ld=long double;
using pli=pair<ll,int>;
using pi=pair<int,int>;
template<typename A>
using vc=vector<A>;
inline int read()
{
int s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline ll lread()
{
ll s=0,w=1;char ch;
while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1;
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
char in[25];
struct OP
{
int op,x,y;
OP(int op,int x,int y):op(op),x(x),y(y){}
};
struct node
{
int a[22][22];
int c[65];
int n,m;
inline void read()
{
for(int i=1;i<=n;i++)
{
scanf("%s",in+1);
for(int j=1;j<=m;j++)
{
if(in[j]<='9') a[i][j]=in[j]-'0'+1;
else if(in[j]<='Z') a[i][j]=in[j]-'A'+11;
else a[i][j]=in[j]-'a'+37;
}
}
}
inline void count()
{
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) c[a[i][j]]++;
// for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) printf("%d%c",a[i][j]," \n"[j==m]);
// putchar('\n');
}
}s,t,b[25];
int n,m,k;
vc<OP>ans;
inline void S(pi w1,pi w2)
{
if(w2.first!=w1.first) ans.push_back(OP(w2.first<w1.first?-4:-3,w1.first,w1.second));
else ans.push_back(OP(w2.second<w1.second?-2:-1,w1.first,w1.second));
swap(t.a[w1.first][w1.second],t.a[w2.first][w2.second]);
}
inline void change(int x1,int y1,int x2,int y2)
{
if(x1==x2&&y1==y2) return ;
// printf("change %d %d %d %d\n",x1,y1,x2,y2);
vc<pi>W;
while(x1!=x2)
{
W.push_back(pi(x1,y1));
if(x1<x2) x1++;else x1--;
}
while(y1!=y2)
{
W.push_back(pi(x1,y1));
if(y1<y2) y1++;else y1--;
}
W.push_back(pi(x2,y2));
for(unsigned i=1;i<W.size();i++) S(W[i-1],W[i]);
for(int i=(int)W.size()-2;i;i--) S(W[i-1],W[i]);
// for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) printf("%d%c",t.a[i][j]," \n"[j==m]);
}
inline bool run(int x,int y,int w,int id)
{
// printf("run %d %d %d %d\n",x,y,w,id);
// if(t.a[x][y]==id) return true;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(t.a[i][j]==id)
{
if(j<=w&&(i<x||(i==x&&j<y))) continue;
change(x,y,i,j);
return true;
}
return false;
}
inline bool can(node &b,int ok,int id)
{
int need=0;
for(int i=1;i<=62;i++)
{
int num=min(t.c[i],b.c[i]);
need+=b.c[i]-num,ok+=num;
}
if(t.c[63]<need||!ok) return false;
// printf("can %d %d need=%d,%d\n",ok,id,need,t.c[63]);
for(int i=1;i<=b.n;i++) for(int j=1;j<=b.m;j++) if(t.a[i][j]!=63) run(i,j,b.m,b.a[i][j]);
for(int i=1;i<=b.n;i++) for(int j=1;j<=b.m;j++) if(!run(i,j,b.m,b.a[i][j])) assert(run(i,j,b.m,63));
ans.push_back(OP(id,1,1));
for(int i=1;i<=b.n;i++) for(int j=1;j<=b.m;j++) t.a[i][j]=63;
t.count();
return true;
}
inline void output()
{
ans.pop_back();
printf("%d\n",(int)ans.size());
for(int i=(int)ans.size()-1;i>=0;i--) printf("%d %d %d\n",ans[i].op,ans[i].x,ans[i].y);
}
int main()
{
freopen("stamp.in", "r", stdin);
freopen("stamp.out", "w", stdout);
n=read(),m=read(),k=read();
s.n=n,s.m=m,s.read();s.count();
t.n=n,t.m=m,t.read();t.count();
for(int i=1;i<=k;i++) b[i].n=read(),b[i].m=read(),b[i].read(),b[i].count();
while(true)
{
bool f=0;
for(int i=1;i<=k;i++) if(can(b[i],0,i)) f=1;
if(can(s,1,114514)){ output();return 0;}
if(!f) break;
}
printf("-1\n");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
3 3 1 OOO GOG BGB OOO GGG BBB 3 1 B G B