跳到正文

个人记录

P1141 01迷宫 题解

Splay_trees约 10 分钟
原文
文章信息与版本

文章信息

发布
2023年10月4日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 10 分钟

版本

  1. v1当前

题目传送门

题解

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

C++
#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 pts30 \text{ pts}。评测记录

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

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

例如以下数据:

代码
3 1

101
010
101

2 2

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

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

C++
#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 pts  ⟹  70 pts30 \text{ pts} \implies 70 \text{ pts} 。评测记录

剩下的 30 pts30 \text{ pts} 要写记忆化。虽然根据题目的数据限制不会询问到同一个点,但是对于某个 0-1 连通块的所有点,询问的答案是一样的。

于是,我们可以使用一个队列记录刚刚 BFS 到的所有点,并在最后统一出队更新数据。

C++
#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 pts  ⟹  90 pts70 \text{ pts} \implies 90 \text{ pts} 。评测记录

emm…

要拿到最后的 1010 分,就需要思考性质了。

想一想:一个点最多存在于一个连通块里。

所以,在更新不包含这个点的连通块的数据时,这个点一定不会存在于这个被更新的联通块里。

所以我们大可不用每次都 memset(说明),还可以利用逻辑判断的 “熔断” 机制\sout\text{卡常}。

AC 代码:

C++
#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 的题目大多不怎么考察强力的逻辑思维,只不过需要稍微推一推题目的性质而已,就像 这道题。我们在比赛中看不到自己的测试结果,所以不仅要小心谨慎,还要勤打草稿,\sout\text{{多上撤硕}}

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

原文