TLE?!

P11228 [CSP-J 2024] 地图探险

milai @ 2024-10-30 20:13:40

这位蒟蒻在考场的代码差不多是这样的(70pts)

#include<bits/stdc++.h>
using namespace std;
int xt[5]={0,1,0,-1};
int yt[5]={1,0,-1,0};
char mp[1145][1145];
bool b[1145][1145];
int main(){
    int T,n,m,k,x,y,d,xto,yto;
    cin>>T;
    while(T--){
        memset(b,0,sizeof(b));
        cin>>n>>m>>k>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        b[x][y]=1;
        for(int i=1;i<=k;i++){
            xto=x+xt[d],yto=y+yt[d];
            while(mp[xto][yto]!='.'||xto<1||yto<1||xto>n||yto>m){
                d=(d+1)%4;
                xto=x+xt[d],yto=y+yt[d];
//              cout<<xto<<' '<<yto<<"\n";
                i++;
            }
            b[xto][yto]=1;
            x=xto,y=yto;
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                ans+=b[i][j];
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
} 

by goIdie @ 2024-10-30 20:22:14

@milai 要判断i是否大于k


by milai @ 2024-10-30 20:22:25

但这好像只能解决那个WA(想起来考场上写了呼


by milai @ 2024-10-30 20:23:17

@goIdie 不会吧,顶多循环k此(虽然没啥用,但是不会T啊


by __log__ @ 2024-10-30 20:23:23

试试?

in:

1
3 3 10
2 2 1
.#.
#.#
.#.

out:

1

by milai @ 2024-10-30 20:24:00

@log e

死循环力!


by goIdie @ 2024-10-30 20:24:04

@milai 改成100pts了

#include<bits/stdc++.h>
using namespace std;
int xt[5]={0,1,0,-1};
int yt[5]={1,0,-1,0};
char mp[1145][1145];
bool b[1145][1145];
int T,n,m,k,x,y,d,xto,yto;
int main(){

    cin>>T;
    while(T--){
        memset(b,0,sizeof(b));
        cin>>n>>m>>k>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        b[x][y]=1;
        for(int i=1;i<=k;i++){
            xto=x+xt[d],yto=y+yt[d];
            while(mp[xto][yto]!='.'||xto<1||yto<1||xto>n||yto>m){
                d=(d+1)%4;
                xto=x+xt[d],yto=y+yt[d];
//              cout<<xto<<' '<<yto<<"\n";
                i++;
                if(i>k){
                    break;
                }
            }
            if(i>k){
                break;
            }
            b[xto][yto]=1;
            x=xto,y=yto;
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                ans+=b[i][j];
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
} 

by milai @ 2024-10-30 20:24:18

丸辣


by goIdie @ 2024-10-30 20:24:49

@milai 祝贺米来J组T2成功挂分


by milai @ 2024-10-30 20:24:50

考场70pts辣


by milai @ 2024-11-06 20:05:23

@goIdie 爽了,程序发下来发现我考场写了那个防死循环的,总分210


上一页 |