QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#227948#6739. Teleportlsj2009AC ✓578ms235180kbC++142.0kb2023-10-28 10:02:492023-10-28 10:02:49

Judging History

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

  • [2023-10-28 10:02:49]
  • 评测
  • 测评结果:AC
  • 用时:578ms
  • 内存:235180kb
  • [2023-10-28 10:02:49]
  • 提交

answer

#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define PII pair<int,int>
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define chkmax(a,b) a=max(a,b)
#define chkmin(a,b) a=min(a,b)
#define rep(k,l,r) for(int k=l;k<=r;++k)
#define per(k,r,l) for(int k=r;k>=l;--k)
#define cl(f,x) memset(f,x,sizeof(f))
using namespace std;
const int dx[]={-1,0,0,1};
const int dy[]={0,-1,1,0};
const int N=5e3+5;
PII fa[N][N];
PII find(int x,int y) {
    PII tmp=fa[x][y];
    if(tmp.first!=x||tmp.second!=y)
        fa[x][y]=find(tmp.first,tmp.second);
    return fa[x][y];
}
char mp[N][N];
int n,k;
bool inrange(int x,int y) {
    return x>=1&&x<=n&&y>=1&&y<=n;
}
int f[N][N];
queue<PII> q;
void update(int x,int y,int sx,int sy,int val,int k) {
    if(mp[x][y]=='*') {
		if(inrange(x+1,y+1)&&(x+1-sx)+(y+1-sy)<=k) {
			fa[x][y]={x+1,y+1};
			update(x+1,y+1,sx,sy,val,k);
		}
        return;
    }
	if(f[x][y]==-1) {
		f[x][y]=val+1;
		q.push({x,y});
	}
    PII tmp=find(x,y);
    int tx=tmp.first,ty=tmp.second;
    if(inrange(tx+1,ty+1)&&(tx+1-sx)+(ty+1-sy)<=k) {
		fa[tx][ty]={tx+1,ty+1};
        update(tx+1,ty+1,sx,sy,val,k);
    }
}
void bfs() {
    cl(f,-1); f[1][1]=0;
    q.push({1,1});
    while(!q.empty()) {
        PII tmp=q.front(); q.pop();
        int x=tmp.first,y=tmp.second;
        if(x==n&&y==n) {
            printf("%d\n",f[x][y]);
            return;
        }
        rep(i,0,3) {
            int tx=x+dx[i],ty=y+dy[i];
            if(inrange(tx,ty)&&mp[tx][ty]=='.'&&f[tx][ty]==-1) {
                f[tx][ty]=f[x][y]+1;
                q.push({tx,ty});
            }
        }
        update(x,y,x,y,f[x][y],k);
		if(y+1<=n&&k)
	        update(y+1,x,y+1,x,f[x][y],k-1);
    }
	puts("-1");
}
signed main() {
    scanf("%d%d",&n,&k);
    rep(i,1,n) {
        rep(j,1,n) {
            cin>>mp[i][j];
            fa[i][j]={i,j};
        }
    }
	if(mp[n][n]=='*')
		return 0&puts("-1");
    bfs();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 45ms
memory: 146376kb

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 28ms
memory: 146080kb

input:

975 434
.*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 34ms
memory: 148716kb

input:

966 19
..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 22ms
memory: 145784kb

input:

973 199
..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 32ms
memory: 149028kb

input:

984 95
.....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 578ms
memory: 235180kb

input:

2996 2
..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 227ms
memory: 233312kb

input:

2905 1023
.........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 247ms
memory: 235140kb

input:

2978 104
.*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...

output:

58

result:

ok 1 number(s): "58"