90分求助,第二个点RE,用的深搜
  • 板块P1141 01迷宫
  • 楼主benjie857
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/10/14 18:00
  • 上次更新2023/11/5 10:47:03
查看原帖
90分求助,第二个点RE,用的深搜
327960
benjie857楼主2020/10/14 18:00
#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;	
}

2020/10/14 18:00
加载中...