P1141 01迷宫 题解

· · 个人记录

题目传送门

题解

看到这道题的第一眼,我就确信这道题是 BFS ,于是风风火火写了一串代码交上去。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char map[1001][1001];
bool vis[1001][1001];
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
int n;
struct point
{
    int x,y,s;
};
int bfs(int sx,int sy)
{
    memset(vis,0,sizeof(vis));
    queue<point> que;
    que.push({sx,sy,1});
    int maxx=0;
    while(!que.empty())//朴实的广搜
    {
        point k=que.front();
        que.pop();
        vis[k.x][k.y]=1;
        for(int i=0; i<=3; i++)
        {
            int nx=k.x+dx[i],ny=k.y+dy[i];
            if(nx>=1 && nx<=n && ny>=1 && ny<=n && !vis[nx][ny] && map[nx][ny]!=map[k.x][k.y]) 
                que.push({nx,ny,k.s+1});
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            maxx+=vis[i][j];//扫一遍连通块的大小
    return maxx;
}
int main()
{
    int m;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cin>>map[i][j];
        }
    }
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        cout<<bfs(a,b)<<endl;
    }
    return 0;
}

喜提 30 \text{ pts}。评测记录

没错,这么做太慢了。查看入队次数后发现,有的点被多次入队了。

因为每次只会标记队首被 visit 了,所以哪怕一个点在队伍里,也有可能被重复入队。

例如以下数据:

3 1

101
010
101

2 2

则开始对点 (2,2) 进行操作的时候,会入队:点 (1,2) 、(2,1)、(3,2) 和 (2,3)。下次取队首时(假设是 (1,2) ),则会标记它并入队 (1,1) 和 (1,3) 。但在处理到点 (2,1) 时候,会重复入队 (1,1) 。

于是我对代码进行了亿点改正,在入队时就标记 visit。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char map[1001][1001];
bool vis[1001][1001];
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
int n;
struct point
{
    int x,y,s;
};
int bfs(int sx,int sy)
{
    memset(vis,0,sizeof(vis));
    queue<point> que;
    que.push({sx,sy,1});
    int maxx=0;
    vis[sx][sy]=1;//起点先标记
    while(!que.empty())
    {
        point k=que.front();
        que.pop();
        maxx++;//点不会重复入队
        for(int i=0; i<=3; i++)
        {
            int nx=k.x+dx[i],ny=k.y+dy[i];
            if(nx>=1 && nx<=n && ny>=1 && ny<=n && !vis[nx][ny] && map[nx][ny]!=map[k.x][k.y])
                        vis[nx][ny]=1,que.push({nx,ny,k.s+1});
        }//预先标记以避免重复入队
    }

    return maxx;
}
int main()
{
    int m;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cin>>map[i][j];
        }
    }
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        cout<<bfs(a,b)<<endl;
    }
    return 0;
}
剩下的 $30 \text{ pts}$ 要写记忆化。虽然根据题目的数据限制不会询问到同一个点,但是对于某个 0-1 连通块的所有点,询问的答案是一样的。 于是,我们可以使用一个队列记录刚刚 BFS 到的所有点,并在最后统一出队更新数据。 ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; char map[1001][1001]; bool vis[1001][1001]; int res[1001][1001]; int dx[4]= {0,1,0,-1}; int dy[4]= {1,0,-1,0}; int n; struct point { int x,y,s; }; int bfs(int sx,int sy) { memset(vis,0,sizeof(vis)); queue<point> que; queue<point> mem;//记忆连通块中的所有点 que.push({sx,sy,1}); int maxx=0; vis[sx][sy]=1; while(!que.empty()) { point k=que.front(); que.pop(); mem.push(k); maxx++; for(int i=0; i<=3; i++) { int nx=k.x+dx[i],ny=k.y+dy[i]; if(nx>=1 && nx<=n && ny>=1 && ny<=n && !vis[nx][ny] && map[nx][ny]!=map[k.x][k.y]) vis[nx][ny]=1,que.push({nx,ny,k.s+1}); } } while(!mem.empty()) { point k=mem.front(); res[k.x][k.y]=maxx; mem.pop(); }//统一更新连通块中的所有点 return maxx; } int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); int m; cin>>n>>m; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>map[i][j]; } } while(m--) { int a,b; cin>>a>>b; if(res[a][b]==0) cout<<bfs(a,b)<<endl; else cout<<res[a][b]<<endl; //记忆化 } return 0; } ``` $70 \text{ pts} \implies 90 \text{ pts}$ 。[评测记录](https://www.luogu.com.cn/record/127588440) emm... 要拿到最后的 $10$ 分,就需要思考性质了。 想一想:一个点最多存在于一个连通块里。 所以,在更新不包含这个点的连通块的数据时,这个点一定不会存在于这个被更新的联通块里。 所以我们大可不用每次都 memset([说明](https://blog.csdn.net/yunhua_lee/article/details/6381866)),还可以利用逻辑判断的 “熔断” 机制$\sout\text{卡常}$。 AC 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; char map[1001][1001]; int res[1001][1001]; int dx[4]= {0,1,0,-1}; int dy[4]= {1,0,-1,0}; int n; struct point { int x,y,s; }; bool vis[1001][1001]={0};//全局数组 inline int bfs(int sx,int sy)//BFS { //memset 没有力! queue<point> que; queue<point> mem;//记忆化 que.push({sx,sy,1}); int maxx=0; vis[sx][sy]=1; while(!que.empty()) { point k=que.front(); que.pop(); mem.push(k); maxx++; for(int i=0; i<=3; i++) { int nx=k.x+dx[i],ny=k.y+dy[i]; if(nx>=1 && nx<=n && ny>=1 && ny<=n && !vis[nx][ny] && map[nx][ny]!=map[k.x][k.y]) vis[nx]ny]=1,que.push({nx,ny,k.s+1}); } } while(!mem.empty()) { point k=mem.front(); res[k.x][k.y]=maxx; mem.pop();//记忆化 } return maxx; } int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); int m; cin>>n>>m; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { cin>>map[i][j]; } } while(m--) { int a,b; cin>>a>>b; if(res[a][b]==0) cout<<bfs(a,b)<<endl; else cout<<res[a][b]<<endl;//记忆化 } return 0; } ``` 说几句题外话,CSP-J 的题目大多不怎么考察强力的逻辑思维,只不过需要稍微推一推题目的性质而已,就像 [这道题](https://www.luogu.com.cn/problem/T383720?contestId=136663)。我们在比赛中看不到自己的测试结果,所以不仅要小心谨慎,还要勤打草稿,$\sout\text{{多上撤硕}}

祝大家中秋国庆双节快乐!