01 Maze (search, Unicom map)

insert image description here
This question starts to search directly, but every time you ask for a search, it will definitely time out. It can be handled like this

Using the idea of ​​connected graph, each search is to traverse all the grids of this connected graph, we will dye the grid of a connected graph with the same color, and save the number of connected graphs of this color, if you ask next time For grids that have been dyed, there is no need to search for the number of connected graphs that directly output this color.

The following codes are solved by wide search and deep search respectively

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
bool vis[10005][10005]; 
string mp[10005];
int vir[4][2]={
    
    {
    
    0,1},{
    
    1,0},{
    
    0,-1},{
    
    -1,0}};
int color[10005][10005];
int res[10005];
int p=0;
int BFS(int xx,int yy){
    
    
	int ans=0;
	queue<int> R;//行 
	queue<int> L;//列
	//初始化
	R.push(xx);
	L.push(yy);
	vis[xx][yy]=true;
	while(!R.empty()){
    
    
		int x = R.front();
		int y = L.front();
		color[x][y]=p;//搜索的地方染色
		for(int i=0;i<4;i++){
    
    
			int tx = x+vir[i][0];
			int ty = y+vir[i][1];
			if(tx<0||tx>=n||ty<0||ty>=n)continue;//越界处理 
			if(mp[x][y]=='0'){
    
    //在0格上 
				if(mp[tx][ty]=='1'&&!vis[tx][ty]){
    
    //移动到1格上 
					vis[tx][ty]=true;
					R.push(tx),L.push(ty);
				} 
			}else{
    
    //在1格上 
				if(mp[tx][ty]=='0'&&!vis[tx][ty]){
    
     
					vis[tx][ty]=true;
					R.push(tx),L.push(ty);
				}
			}
		}
		ans++;
		R.pop();
		L.pop();
	} 
	return ans;
}
int main(){
    
    
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
    
    
			cin>>mp[i];
	}
	while(m--){
    
    
		int x,y;
		scanf("%d %d",&x,&y);
		if(color[x-1][y-1]>0){
    
    //染过颜色 
			printf("%d\n",res[color[x-1][y-1]]);
		}else{
    
    //以前没有染过颜色 
			p++;//确定一种颜色 
			res[p]=BFS(x-1,y-1);//将这种颜色的联通图数量记录下来  
			printf("%d\n",res[p]);
		}
		
	}
	return 0;
}
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
string mp[10005];
int color[10005][10005];//联通图颜色
int id[10005];//id[p]表示代号为p的颜色的联通图有id[p]个 
int p=0;
int vir[4][2]={
    
    {
    
    0,1},{
    
    1,0},{
    
    0,-1},{
    
    -1,0}};
int ans;
int dfs(int x,int y){
    
    
	id[p]++;
	for(int i=0;i<4;i++){
    
    
		int tx = x+vir[i][0];
		int ty = y+vir[i][1];
		if(tx<0||tx>=n||ty<0||ty>=n)continue;//越界处理 
		if(!color[tx][ty]){
    
    
			if(mp[x][y]=='0'){
    
    
				if(mp[tx][ty]=='1'){
    
    
					color[tx][ty]=p;//搜索过的地方染色颜色
					dfs(tx,ty); 
				}
			}else{
    
    
				if(mp[tx][ty]=='0'){
    
    
					color[tx][ty]=p;//搜索过的地方染色颜色
					dfs(tx,ty); 
				}
			}
			
		} 
	}
}
int main(){
    
    
	//输入 
	cin>>n>>m;
	for(int i=0;i<n;i++){
    
    
		cin>>mp[i];
	}
	while(m--){
    
    
		int x,y;
		cin>>x>>y;
		x--,y--;
		if(color[x][y]>0){
    
    //这块联通图被染过色,就不需要搜索了 
			printf("%d\n",id[color[x][y]]);
		}else{
    
    //没有染过色,需要搜索进行染色 
			p++;//给块区域的颜色代号
			color[x][y]=p;//起始点染色 
			dfs(x,y); 
			printf("%d\n",id[p]); 
		}
		
	}
	return 0;	
}

It can also be solved by using the union check, and it will be added later

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324962036&siteId=291194637