• cf1063B Labyrinth (bfs)


    可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时

    然后判一判,bfs就好了

    dfs会T惨...

     1 #include<bits/stdc++.h>
     2 #define pa pair<int,int>
     3 #define CLR(a,x) memset(a,x,sizeof(a))
     4 using namespace std;
     5 typedef long long ll;
     6 const int maxn=2000+10;
     7 
     8 inline ll rd(){
     9     ll x=0;char c=getchar();int neg=1;
    10     while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();}
    11     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    12     return x*neg;
    13 }
    14 
    15 struct Node{
    16     int x,y,l,r;
    17     Node(int a=0,int b=0,int c=0,int d=0){
    18         x=a,y=b,l=c,r=d;
    19     }
    20 };
    21 bool mp[maxn][maxn],vis[maxn][maxn];
    22 int N,M,X,Y,L,R;
    23 int ml[maxn][maxn][2],mr[maxn][maxn][2],ans;
    24 char s[maxn];
    25 queue<Node> q;
    26 
    27 void bfs(){
    28     q.push(Node(X,Y,L,R));
    29     while(!q.empty()){
    30         Node p=q.front();q.pop();
    31         int l=p.l,r=p.r,x=p.x,y=p.y;
    32         if(l<0||r<0) continue;
    33         if(x<=0||y<=0||x>N||y>M) continue;
    34         if(!mp[x][y]) continue;
    35         if((l<ml[x][y][0]||(l==ml[x][y][0]&&r<=ml[x][y][1]))&&(r<mr[x][y][1]||(r==mr[x][y][1]&&l<=mr[x][y][0]))) continue;
    36         if(!vis[x][y]) vis[x][y]=1,ans++;
    37         if(l>=ml[x][y][0]) ml[x][y][0]=l,ml[x][y][1]=r;
    38         if(r>=mr[x][y][1]) mr[x][y][0]=l,mr[x][y][1]=r;
    39         q.push(Node(x,y+1,l,r-1));q.push(Node(x,y-1,l-1,r));
    40         q.push(Node(x+1,y,l,r));q.push(Node(x-1,y,l,r));
    41     }
    42 }
    43 
    44 int main(){
    45     //freopen(".in","r",stdin);
    46     int i,j,k;
    47     N=rd(),M=rd();
    48     X=rd(),Y=rd();
    49     L=rd(),R=rd();
    50     CLR(ml,-1);CLR(mr,-1);
    51     for(i=1;i<=N;i++){
    52         scanf("%s",s+1);
    53         for(j=1;j<=M;j++){
    54             mp[i][j]=(s[j]=='.');
    55         }
    56     }
    57     bfs();
    58     printf("%d
    ",ans);
    59     return 0;
    60 }
  • 相关阅读:
    numpy 支持切片取值,使用ix_ 也可以取到
    pyinstaller 打包文件成 exe
    matplotlib 直方图概率不为1
    小提琴图 ValueError: object arrays are not supported
    pip 生成 requirements.txt
    CF1515E(连续段 dp)
    Lg7 月赛(构造,树形 dp)
    [省选联考 2020 A 卷] 组合数问题 题解报告
    愤怒的小 N 题解报告
    CF32E 题解
  • 原文地址:https://www.cnblogs.com/Ressed/p/9794616.html
Copyright © 2020-2023  润新知