哈尔滨理工大学软件与微电子学院程序设计竞赛 Maze dfs求地图联通快个数

链接:https://ac.nowcoder.com/acm/contest/5929/H
来源:牛客网

题目描述
多多在一个N行M列的迷宫中,迷宫只由符号 ‘+’ 或 ‘-’ 组成。如果多多在 ‘+’ 上,下一步只能走到上、下、左、右四个方向的 ‘-’ 上;如果多多在 ‘-’ 上,下一步只能走到上、下、左、右四个方向的 ‘+’ 上。多多希望能走到更多的格子上,所以他想知道:从某一位置开始能走到多少个格子?(包含开始位置) 多多会询问 Q 次,请你耐心的回答他。
输入描述:

输入第一行包含三个正整数N,M,Q (1 ≤ N ≤ 3000,1 ≤ M ≤ 3000,1 ≤ Q ≤ 30000)
接下来 N 行输入长度为 M 的字符串
接下来 Q 行每行输入一个位置坐标,代表起始位置的行和列(从1,1开始)

输出描述:

对于每次询问,输出一个正整数,表示从该位置能走到的格子数

示例1
输入
复制

3 3 4
-+-
--+
+-+
1 1
2 1
2 2
3 3

输出
复制

5
4
5
4

题意 : 给定 N ∗ M N*M NM的地图,只能从+走到-或者从-走到+,多次询问地图的某个位置map[x][y]能到达多少个位置,

  • 直接 d f s dfs dfs求联通块即可
  • ID[ ] [ ]存这个位置属于那个联通块
#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN (3072)
#define ll long long 
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
	do { \
		cout << "\033[31;1m " << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO {

	char print_f[105];
	void read() { }
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

char mtx[MAXN][MAXN], vis[MAXN][MAXN];
int pre[MAXN*MAXN];

void init() {
	int N = n * n;
	for(int i=1; i<=N; i++) pre[i] = i;
}

int fa(int x) {
	return (pre[x]==x ? pre[x] : (pre[x]=fa(pre[x])));
}

void union_xy(int x, int y) {
	int rx = fa(x), ry = fa(y);
	if(ry != rx)
		pre[ry] = rx;
}

int cnt[MAXN*MAXN], ID[MAXN][MAXN], tot;
int dr[] = { 1, -1, 0, 0 };
int dc[] = { 0, 0, 1, -1 };

//用SWAP(x)取反  '+' -> '-'  或者  '-' -> '+'
#define SWAP(x) (x=='-' ? '+' : '-')
void dfs(int r, int c, int ch) {
	vis[r][c] = true;
	ID[r][c] = tot;                //记录这个位置属于那个联通块
	cnt[tot] ++;                   //块个数 ++
	for(int i=0; i<4; i++) {
		int nr = r+dr[i], nc = c+dc[i];
		if(!mtx[nr][nc] || vis[nr][nc] || ch!=mtx[nr][nc]) continue ;
		dfs(nr, nc, SWAP(ch));     //传入取反的符号
	}
}

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	// freopen("out_main", "w", stdout);
	clock_t stime = clock();
#endif
	scanf("%d %d %d ", &n, &m, &Q);
	for(int i=1; i<=n; i++)
		scanf("%s ", mtx[i]+1);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			//记得 ++tot
			if(!vis[i][j]) ++tot, dfs(i, j, SWAP(mtx[i][j]));
		}
	}
#if 0
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++)
			printf("%d  ", ID[i][j]);
		printf("\n");
	}
#endif
	while(Q--) {
		int r, c;
		scanf("%d %d ", &r, &c);
		printf("%d\n", cnt[ID[r][c]]);
	}





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}

/**
                                                             
                                                        
  ]]`    ,]]` ]]]`     ]]]`.]]]  ,]]]]]].     ]]].     ]]].        
  @@@@`  =@@^ =@@@    =@@/ .@@@  =@@@@@@@@\.  @@@.    /@@@@.       
  @@@@@\ =@@^  =@@^  .@@@. .@@@  =@@^   ,@@@  @@@.   =@@`@@@       
  @@@.\@@/@@^   \@@` /@@`  .@@@  =@@^    @@@. @@@.  =@@^ ,@@\.      
  @@@  =@@@@^   .@@@=@@^   .@@@  =@@^   =@@@  @@@. .@@@@@@@@@^     
  @@@   ,@@@^    ,@@@@^    .@@@  =@@@@@@@@/.  @@@. @@@`    @@@`    
  [[[    .[[`     ,[[[     .[[[  ,[[[[[[.     [[[.,[[`     ,[[[    
                                                                   
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值