玄关70分求条
  • 板块P1141 01迷宫
  • 楼主lyc_qwq
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/11/22 21:17
  • 上次更新2024/11/22 22:43:45
查看原帖
玄关70分求条
1128671
lyc_qwq楼主2024/11/22 21:17
#include<bits/stdc++.h>

using namespace std;

inline int read()
{
	int x = 0 , t = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-')
		{
			t = -1;
		}
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * t;
}
inline void write(int x)
{
	if(x < 0)
	{
		putchar('-');
	}
	if(x > 9)
	{
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 1010;
int n , m;
int o , oo;
int vis[N][N];
int sum;
int dx[4] = {0 , 1 , 0 , -1};
int dy[4] = {-1 , 0 , 1 , 0};
char a[N][N];
struct node
{
	int x;
	int y;
};

void bfs(int x , int y)
{
	queue <node> q;
	memset(vis , 0 , sizeof(vis));
	sum=0;
	q.push({x , y });
	vis[x][y]=1;
	sum=1;
	while(q.size())
	{
		node tmp = q.front();
		int tx = tmp.x;
		int ty = tmp.y;
		q.pop();
		for(int i = 0; i < 4; ++ i)
		{
			int ttx = tx + dx[i];
			int tty = ty + dy[i];
			if(ttx >= 1 && ttx <=n && tty >= 1 && tty <= n && a[ttx][tty] != a[tx][ty] && vis[ttx][tty] == 0)
			{
				q.push({ttx , tty });
				vis[ttx][tty] = 1;
				sum ++;
			}
		}
	}
}

int main()
{
	n = read() , m = read();
	for(int i = 1;i <= n; ++ i)
	{
		for(int j = 1;j <= n; ++ j)
		{
			cin >> a[i][j];
		}
	}
	while(m --)
	{
		sum = 0;
		o = read() , oo = read();
		bfs(o , oo);
		write(sum);
		cout << '\n';
	}
	return 0;
}

请各位大佬帮本蒟蒻调一下代码

2024/11/22 21:17
加载中...