2.4考试复盘

· · 个人记录

1.dfs01

经典 vis 先生和 cnt 博士
用迷宫上搜索那一集的知识点来填涂,遇到水就在那整个地块用vis标记,同时恳请cnt博士++,最后输出cnt博士

::::success[AC]

#include<bits/stdc++.h>
using namespace std;
char mp[200][200];
bool vis[200][200];
int n,m;
int dir[8][2]={
    {-1,0},{0,1},{1,0},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}
};
void dfs(int x,int y){
    vis[y][x]=1;
    for(auto pos:dir){
        int nx=x+pos[1],ny=y+pos[0];
        if(nx>m or ny>n or nx<1 or ny<1)continue;
        if(vis[ny][nx]==1 or mp[ny][nx]!='W')continue;
        dfs(nx,ny);
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin>>n>>m;
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mp[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]!=1 and mp[i][j]=='W'){
                ans++;
                dfs(j,i);
            }
        }
    }
    cout<<ans;
    return 0;
}

::::

2.dfs02

在 来追梦 总部外面放水,可以使水绕总部一圈后开始向内收拢包围圈,但没办法进入*的圈子内部
::::success[AC]

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={
    {-1,0},{0,1},{1,0},{0,-1}
};
char mp[600][600];
bool vis[600][600];
int n,m;
void dfs(int x,int y){
    vis[y][x]=1;
    if(mp[y][x]!='*')mp[y][x]='1';
    for(auto pos:dir){
        int nx=x+pos[1],ny=y+pos[0];
        if(nx>m+1 or ny>n+1 or nx<0 or ny<0)continue;
        if(vis[ny][nx]==1 or mp[ny][nx]=='*')continue;
        mp[ny][nx]='1';
        dfs(nx,ny);
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mp[i][j];
        }
    }
    dfs(0,0);
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
//          cout<<vis[i][j]<<mp[i][j]<<' ';
            if(mp[i][j]=='0')ans++;
        }
//      cout<<'\n';
    }
    cout<<ans;
    return 0;
}

::::

3.dfs03

只要会做第一题的就都会做这道题
思路和第一题基本一样
::::success[AC]

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={
    {-1,0},{0,1},{1,0},{0,-1}
};
char mp[300][300];
int sizes[9000000];
bool vis[600][600];
int n,m;
map<int,int>mpp;
void dfs(int x,int y,int i){
    vis[y][x]=1;
    sizes[i]++;
    for(auto pos:dir){
        int nx=x+pos[1],ny=y+pos[0];
        if(nx>m or ny>n or nx<1 or ny<1)continue;
        if(vis[ny][nx]==1 or mp[ny][nx]!='1')continue;
        dfs(nx,ny,i);
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mp[i][j];
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]!=1 and mp[i][j]=='1'){
                ans++;
                dfs(j,i,ans);
                mpp[sizes[ans]]++;
            }
        }
    } 
//  cout<<'\n';
//  for(int i=1;i<=n;i++){
//      for(int j=1;j<=m;j++){
//          cout<<vis[i][j]<<' ';
//      }
//      cout<<'\n';
//  } 
    cout<<ans<<'\n';
    for(auto x:mpp){
        cout<<x.first<<' '<<x.second<<'\n';
    }
    return 0;
}//American: Where???

::::

4.dfs04

和迷宫上搜索那集的不知道是第几题一样。都是小田,都不需要vis,只不过要所有格子都搜索一遍
::::success

#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={
    {-1,0},{0,1},{1,0},{0,-1}
};
int mp[60][60];
int n,m;
int maxcnt;
void dfs(int x,int y,int cnt){
//  cout<<x<<' '<<y<<'\n';
    maxcnt=max(maxcnt,cnt);
    for(auto pos:dir){
        int nx=x+pos[1],ny=y+pos[0];
        if(nx>m or ny>n or nx<1 or ny<1)continue;
        if(mp[ny][nx]>=mp[y][x])continue;
        dfs(nx,ny,cnt+1);
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>mp[i][j];
        }
    }
    int mmax=INT_MIN,maxx,maxy;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dfs(j,i,1);
            if(maxcnt>mmax){
                mmax=maxcnt;
                maxx=j;
                maxy=i;
            }
//          cout<<maxcnt<<'\n';
            maxcnt=0;
        }
    }
    cout<<maxy<<' '<<maxx<<'\n'<<mmax;
    return 0;
}

::::

5.dfs05

只要理解了就会觉得非常简单的一道题