生草代码求调

P1434 [SHOI2002] 滑雪

672760syc @ 2023-03-25 20:00:19

#include<bits/stdc++.h>
using namespace std;
int r,c,sum=0,a[101][101],mini,minj,maxi,maxj,mina=2147483647,maxa=-1;
int main(){
    cin>>r>>c;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            cin>>a[i][j];
        }
    }
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            if(a[i][j]<mina){
                mina=a[i][j];
                mini=i;
                minj=j;
            }
            if(a[i][j]>maxa){
                maxa=a[i][j];
                maxi=i;
                maxj=j;
            }
        }
    }
    mina=114514;
    for(int i=mini;i<=r;){
        for(int j=minj;j<=c;){
            if(mini==maxi&&minj==maxj||a[i+1][j]<a[i][j]&&a[i-1][j]<a[i][j]&&a[i][j-1]<a[i][j]&&a[i][j+1]<a[i][j]){
                cout<<sum;
                return 0;
            }
            if(a[i+1][j]>a[i][j]&&a[i+1][j]<mina){
                mina=a[i+1][j];
                mini=i+1;
            }
            if(a[i-1][j]>a[i][j]&&a[i-1][j]<mina){
                mina=a[i-1][j];
                mini=i-1;
            }
            if(a[i][j+1]>a[i][j]&&a[i][j+1]<mina){
                mina=a[i][j+1];
                minj=j+1;
            }
            if(a[i][j-1]>a[i][j]&&a[i][j-1]<mina){
                mina=a[i][j-1];
                minj=j-1;
            }
            sum++;
        }
    }
    return 0;
}

int main(){ cin>>r>>c; for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cin>>a[i][j]; } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if(a[i][j]<mina){ mina=a[i][j]; mini=i; minj=j; } if(a[i][j]>maxa){ maxa=a[i][j]; maxi=i; maxj=j; } } } mina=114514; for(int i=mini;i<=r;){ for(int j=minj;j<=c;){ if(mini==maxi&&minj==maxj||a[i+1][j]<a[i][j]&&a[i-1][j]<a[i][j]&&a[i][j-1]<a[i][j]&&a[i][j+1]<a[i][j]){ cout<<sum; return 0; } if(a[i+1][j]>a[i][j]&&a[i+1][j]<mina){ mina=a[i+1][j]; mini=i+1; } if(a[i-1][j]>a[i][j]&&a[i-1][j]<mina){ mina=a[i-1][j]; mini=i-1; } if(a[i][j+1]>a[i][j]&&a[i][j+1]<mina){ mina=a[i][j+1]; minj=j+1; } if(a[i][j-1]>a[i][j]&&a[i][j-1]<mina){ mina=a[i][j-1]; minj=j-1; } sum++; } } return 0; }


by Strange_qwq @ 2023-03-25 20:10:02

@672760syc 你这样写会TLE

深搜能过


by 672760syc @ 2023-03-25 20:19:51

@shashihan 感谢


|