思考
考虑搜索。
因为m <= 100000,所以直接硬搜索是不行的,用预处理解决问题
A能到B则B一定能到A,进一步,在A能到的连通块中的每个格子都能在这个区域里移动且只能在这个区域移动。
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int cur;
int a[1010][1010];
int col[1010][1010];
int num[1000010];
void dfs(int x,int y)
{
if(x < 1|| x > n || y < 1 || y > n) return;
if(col[x][y] != 0) return;
num[cur]++;
col[x][y] = cur;
if(a[x][y] != a[x][y + 1]) dfs(x,y + 1);
if(a[x][y] != a[x][y - 1]) dfs(x,y - 1);
if(a[x][y] != a[x + 1][y]) dfs(x + 1,y);
if