记录编号 526632 评测结果 AAAAAAAAAA
题目名称 [福州培训2010] 01迷宫 最终得分 100
用户昵称 Gravatar521 是否通过 通过
代码语言 C++ 运行时间 0.901 s
提交时间 2019-01-28 21:50:53 内存使用 11.07 MiB
显示代码纯文本
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
void read_int(int&x)
{
	int flag = 1;
	char ch;
	while (ch = getchar(), ch<'0' || ch>'9') if (ch == '-') flag = -1;
	x = (ch^'0');
	while (ch = getchar(), ch >= '0'&&ch <= '9')x = (x << 3) + (x << 1) + (ch^'0');
	x *= flag;
}
const int Size = 1010;
int map[Size][Size], n, m;
int ans[Size][Size];
int dx[4] = { 1,-1,0,0 }, dy[4] = { 0,0,1,-1 };
queue<pair<int, int> > q, tempq;
int main()
{
#define TEXT
#ifdef TEXT
	freopen("maze01.in", "r", stdin);
	freopen("maze01.out", "w", stdout);
#endif
	cin>>n>>m;
	int i = 1, j;
	for (char ch; i <= n;i++)
		for (j = 1; j <= n; j++) {
			cin >> ch;
			map[i][j] = ch - '0';
		}
	for (int X = 1; X <= n; X++) for (int Y = 1; Y <= n; Y++) if (ans[X][Y] == 0) {
		int step = 1;
		q.push(make_pair(X, Y)), tempq.push(make_pair(X, Y));
		ans[X][Y] = 1;
		while (!q.empty()) {
			pair<int, int> temp = q.front();
			int x = temp.first, y = temp.second;
			for (i = 0; i < 4; i++) {
				int t_x = x + dx[i], t_y = y + dy[i];
				if (t_x > 0 && t_x <= n && t_y > 0 && t_y <= n && map[x][y] ^ map[t_x][t_y] && !ans[t_x][t_y])
					step++, ans[t_x][t_y] = 1, q.push(make_pair(t_x, t_y)) ,tempq.push(make_pair(t_x, t_y));
			}
			q.pop();
		}
		while (!tempq.empty()) {
			pair<int, int> temp = tempq.front();
			tempq.pop();
			ans[temp.first][temp.second] = step;
		}
	}
	while (m--) {
		read_int(i), read_int(j);
		printf("%d\n",ans[i][j]);
	}
	return 0;
}