QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163730#6739. Teleportucup-team1209#AC ✓628ms198016kbC++201.9kb2023-09-04 14:36:522023-09-04 14:36:52

Judging History

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

  • [2023-09-04 14:36:52]
  • 评测
  • 测评结果:AC
  • 用时:628ms
  • 内存:198016kb
  • [2023-09-04 14:36:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,y,x) for (int i=(y);i>=(x);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define templ template<typename T>
templ bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
    #ifdef zqj
    freopen("a.in","r",stdin);
    #endif
}
typedef long long ll;
#define sz 5555


int n,K;
char s[sz][sz];
int vis[sz][sz];

int in(int x,int y){return x&&y&&x<=n&&y<=n&&s[x][y]!='*';}

pii fa[sz][sz]; int dis[sz][sz];
pii getfa(pii t) {
    if (!vis[t.fir][t.sec]||fa[t.fir][t.sec]==t) return t;
    if (getfa(fa[t.fir][t.sec])==fa[t.fir][t.sec]) return fa[t.fir][t.sec];
    dis[t.fir][t.sec]+=dis[fa[t.fir][t.sec].fir][fa[t.fir][t.sec].sec];
    return fa[t.fir][t.sec]=getfa(fa[t.fir][t.sec]);
}

int main() {
    file();
    ios::sync_with_stdio(false),cin.tie(0);
    scanf("%d %d",&n,&K);
    rep(i,1,n) scanf("%s",s[i]+1);
    vis[1][1]=1;
    rep(i,1,n) rep(j,1,n) if (in(i,j)) {
        int x=j+1,y=i,cc=1;
        while (!in(x,y)&&min(x,y)<=n&&cc<K) swap(x,y),++x,++cc;
        if (in(x,y)) fa[i][j]=pii{x,y},dis[i][j]=cc;
        else fa[i][j]=pii{i,j},dis[i][j]=0;
    }
    queue<pii>q;
    q.push({1,1});
    static int fx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    while (q.size()) {
        auto [x,y]=q.front(); q.pop();
        if (x==n&&y==n) return cout<<vis[x][y]-1<<'\n',0;
        rep(i,0,3) {
            int xx=x+fx[i][0],yy=y+fx[i][1];
            if (in(xx,yy)&&!vis[xx][yy]) vis[xx][yy]=vis[x][y]+1,q.push({xx,yy});
        }
        while (getfa({x,y})!=pii{x,y}&&!vis[fa[x][y].fir][fa[x][y].sec]&&dis[x][y]<=K) {
            auto [xx,yy]=fa[x][y];
            vis[xx][yy]=vis[x][y]+1,q.push({xx,yy});
        }
    }
    cout<<-1<<'\n';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9928kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 3ms
memory: 9920kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 43ms
memory: 44180kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 14ms
memory: 42768kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 13ms
memory: 40236kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 15ms
memory: 44408kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 11ms
memory: 42828kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 628ms
memory: 198016kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 118ms
memory: 157880kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 131ms
memory: 166616kb

input:

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

output:

58

result:

ok 1 number(s): "58"