QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#314591 | #1066. ROBOTS | JohnAlfnov | 500 ✓ | 792ms | 175180kb | C++20 | 2.4kb | 2024-01-25 21:29:07 | 2024-01-25 21:29:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
char s[505][505];
int f[10][10][250005];
#define mk(x,y) (((x)-1)*w+(y))
vector<int>gg[1000005];
int dd[1000005];
vector<int>g[250005];
int N;
int dist[250005];
vector<int>pp[250005];
void bfs(int l,int r){
for(int i=0;i<=N;++i)pp[i].clear();
for(int i=1;i<=N;++i){
dist[i]=f[l][r][i];
if(dist[i]<=N)pp[dist[i]].emplace_back(i);
}
queue<int>q;
for(int i=0;i<=N;++i){
for(auto cu:pp[i])if(dist[cu]==i)q.emplace(cu);
queue<int>qq;
while(q.size()){
int x=q.front();
q.pop();
for(auto cu:g[x]){
if(dist[cu]>dist[x]+1){
dist[cu]=dist[x]+1;
qq.emplace(cu);
}
}
}
q=qq;
}
while(q.size()){
int x=q.front();
q.pop();
for(auto cu:g[x]){
if(dist[cu]>dist[x]+1){
dist[cu]=dist[x]+1;
q.emplace(cu);
}
}
}
for(int i=1;i<=N;++i)f[l][r][i]=dist[i];
}
int mb[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int main(){
int n,w,h;
scanf("%d%d%d",&n,&w,&h);
N=w*h;
for(int i=1;i<=h;++i)scanf("%s",s[i]+1);
queue<int>q;
memset(dd,63,sizeof(dd));
for(int i=1;i<=h;++i)for(int j=1;j<=w;++j){
int wz=(mk(i,j)-1)*4;
for(int k=0;k<4;++k){
int kk=k;
if(s[i][j]=='A')kk=(k+3)%4;
if(s[i][j]=='C')kk=(k+1)%4;
int dx=i+mb[kk][0],dy=j+mb[kk][1];
int zz=wz+k+1;
if(dx<1||dx>h||dy<1||dy>w||s[dx][dy]=='x'){
dd[zz]=mk(i,j);
q.emplace(zz);
}else{
int zw=(mk(dx,dy)-1)*4;
int yy=zw+kk+1;
gg[yy].emplace_back(zz);
}
}
}
while(q.size()){
int x=q.front();
q.pop();
for(auto cu:gg[x]){
dd[cu]=dd[x];
q.emplace(cu);
}
}
for(int i=1;i<=h;++i)for(int j=1;j<=w;++j)
for(int k=0;k<4;++k){
int zz=mk(i,j)*4-3+k;
if(dd[zz]<=1e9)g[mk(i,j)].emplace_back(dd[zz]);
}
memset(f,63,sizeof(f));
for(int i=1;i<=h;++i)for(int j=1;j<=w;++j){
if(isdigit(s[i][j])){
int ww=s[i][j]-'0';
f[ww][ww][mk(i,j)]=0;
}
}
for(int i=1;i<=n;++i)bfs(i,i);
for(int l=2;l<=n;++l)for(int i=1;i<=n-l+1;++i){
int j=i+l-1;
for(int k=1;k<=N;++k){
for(int s=i;s<j;++s)
f[i][j][k]=min(f[i][j][k],f[i][s][k]+f[s+1][j][k]);
for(int s=i+1;s<j;++s)for(int t=i;t<j;++t){
f[i][j][k]=min(0ll+f[i][j][k],0ll+f[i][s-1][k]+f[s][t][k]+f[t+1][j][k]);
}
}
bfs(i,j);
}
int ans=INT_MAX;
for(int i=1;i<=N;++i)ans=min(ans,f[1][n][i]);
if(ans>1e9)ans=-1;
printf("%d\n",ans);
return 0;
}
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 3ms
memory: 110072kb
input:
2 2 1 12
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 8ms
memory: 110276kb
input:
2 3 1 1x2
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 8ms
memory: 110736kb
input:
2 4 1 .12.
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 3ms
memory: 111100kb
input:
2 10 10 ...x...x.2 .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x 1x...x...x
output:
10
result:
ok single line: '10'
Test #5:
score: 0
Accepted
time: 7ms
memory: 111092kb
input:
2 10 10 .......... .xx..xx... xx..xx..x. x..xx..xx. ..xx..xx.. .xx..xx..x xx..xx..xx x..xx..xx2 ..xx..xx.. 1xx......x
output:
29
result:
ok single line: '29'
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 20
Accepted
time: 0ms
memory: 110756kb
input:
2 2 2 1C .2
output:
1
result:
ok single line: '1'
Test #7:
score: 0
Accepted
time: 12ms
memory: 111204kb
input:
2 3 1 1A2
output:
2
result:
ok single line: '2'
Test #8:
score: 0
Accepted
time: 3ms
memory: 110972kb
input:
2 3 3 .x. 1CA .x2
output:
2
result:
ok single line: '2'
Test #9:
score: 0
Accepted
time: 4ms
memory: 111576kb
input:
2 6 2 C1CA2A CCCAAA
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 15ms
memory: 111204kb
input:
2 10 10 C.CxC.CxC2 .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x .x.x.x.x.x 1xA.AxA.Ax
output:
1
result:
ok single line: '1'
Subtask #3:
score: 30
Accepted
Dependency #2:
100%
Accepted
Test #11:
score: 30
Accepted
time: 241ms
memory: 128356kb
input:
9 296 298 2..x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...xx...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x....
output:
47593
result:
ok single line: '47593'
Test #12:
score: 0
Accepted
time: 39ms
memory: 124600kb
input:
1 296 298 1..x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...xx...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x....
output:
0
result:
ok single line: '0'
Test #13:
score: 0
Accepted
time: 120ms
memory: 127904kb
input:
7 300 300 .x...x...x...x...x..4x...x...x...x...x...x...x...x..1x...x...x...x...x...x..5x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x..6x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x..7x...x...
output:
438
result:
ok single line: '438'
Test #14:
score: 0
Accepted
time: 264ms
memory: 130424kb
input:
9 296 298 1AAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxC...
output:
45376
result:
ok single line: '45376'
Test #15:
score: 0
Accepted
time: 240ms
memory: 126652kb
input:
9 296 298 4..x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...xx...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x....
output:
-1
result:
ok single line: '-1'
Subtask #4:
score: 40
Accepted
Dependency #3:
100%
Accepted
Test #16:
score: 40
Accepted
time: 666ms
memory: 175180kb
input:
9 500 500 .....................................................................................................................................................................................................................................................................................................
output:
44
result:
ok single line: '44'
Test #17:
score: 0
Accepted
time: 792ms
memory: 167672kb
input:
9 496 500 2AAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxAAAxxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxCCCxC...
output:
138163
result:
ok single line: '138163'
Test #18:
score: 0
Accepted
time: 620ms
memory: 156336kb
input:
9 496 500 4..x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...xx...x...x...x...x...x...x...x...x...x...x....
output:
-1
result:
ok single line: '-1'
Test #19:
score: 0
Accepted
time: 626ms
memory: 160580kb
input:
9 500 500 .x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x..4x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x..7x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...
output:
484
result:
ok single line: '484'
Test #20:
score: 0
Accepted
time: 737ms
memory: 162892kb
input:
9 496 500 4..x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...x...xx...x...x...x...x...x...x...x...x...x...x....
output:
126926
result:
ok single line: '126926'