本机却各只要一个getchar就完了了。按理来说不应该就是一个就够了吗。VS上两个反而出错了。(
#include<iostream>
#include<cstring>
#include<queue>
#include <cstdio>
using namespace std;
int n, m;
long long xi, yi, cnt;
int map[1010][1010], vis[1010][1010];
struct Point
{
int Px = 0;
int Py = 0;
};
void bfs(int, int);
inline int readIn();
inline bool isIn(int a);
int main()
{
cin >> n >> m;
getchar();
getchar(); //后来加上
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
map[i][j] = readIn();
}
getchar();
getchar(); //后来加上
}
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
cout << map[i][j] << " ";
}
cout << endl;
}
while (m--) {
cin >> xi >> yi;
memset(vis, 0, sizeof(vis));
bfs(xi, yi);
cout << cnt << endl;
}
return 0;
}
void bfs(int x, int y)
{
Point temp;
queue<Point>q;
int dx = x, dy = y;
temp.Px = x, temp.Py = y;
vis[x][y] = 1;
q.push(temp);
cnt = 1;
while (!q.empty()) {
dx = q.front().Px, dy = q.front().Py;
if (isIn(dx + 1) && map[dx + 1][dy] != map[dx][dy] && !vis[dx + 1][dy]) {
temp.Px = dx + 1, temp.Py = dy;
vis[dx + 1][dy] = 1;
++cnt;
q.push(temp);
}
if (isIn(dx - 1) && map[dx - 1][dy] != map[dx][dy] && !vis[dx - 1][dy]) {
temp.Px = dx - 1, temp.Py = dy;
vis[dx - 1][dy] = 1;
++cnt;
q.push(temp);
}
if (isIn(dy + 1) && map[dx][dy + 1] != map[dx][dy] && !vis[dx][dy + 1]) {
temp.Px = dx, temp.Py = dy + 1;
vis[dx][dy + 1] = 1;
++cnt;
q.push(temp);
}
if (isIn(dy - 1) && map[dx][dy - 1] != map[dx][dy] && !vis[dx][dy - 1]) {
temp.Px = dx, temp.Py = dy - 1;
vis[dx][dy - 1] = 1;
++cnt;
q.push(temp);
}
q.pop();
}
}
inline bool isIn(int a)
{
if (a <= n && a >= 1)return 1;
return 0;
}
inline int readIn()
{
int s = 0;
char ch = getchar();
if (ch >= '0'&&ch <= '9')return ch - '0';
}