求助
  • 板块灌水区
  • 楼主多喝岩浆
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/11/18 20:22
  • 上次更新2023/11/4 00:13:15
查看原帖
求助
528287
多喝岩浆楼主2021/11/18 20:22

P1141

70分代码:

#include<bits/stdc++.h>

using namespace std;
const int N = 1e3 + 10;
int n, m;
char a[N][N];
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
struct sb {
	int x, y, false_or_true;
};
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		string b;
		cin >> b;
		for (int j = 1; j <= b.size (); j++)
			a[i][j] = b[j - 1];
	}
	while (m--) {
		int x, y;
		cin >> x >> y;
		queue <sb> Q;
		Q.push ((sb){x, y, a[x][y] - '0'});
		bool f[N][N];
		memset (f, false, sizeof (f));
		f[x][y] = true;
		int ans = 0;
		while (Q.size ()) {
			sb b = Q.front ();
			int xx = b.x;
			int yy = b.y;
			int z_o = b.false_or_true;
			Q.pop ();
			for (int i = 0; i < 4; i++) {
				int tx = xx + dx[i];
				int ty = yy + dy[i];
				if (tx < 1 || ty < 1 || tx > n || ty > n || f[tx][ty] || a[tx][ty] == z_o + '0') continue;
				ans++;
				f[tx][ty] = true;
				Q.push ((sb){tx, ty, a[tx][ty] - '0'});
			}
		}
		cout << ans + 1 << endl;
	}		
	return 0;
}

50分代码:

#include<bits/stdc++.h>

using namespace std;
const int N = 1e3 + 10;
int n, m;
char a[N][N];
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
struct sb {
	int x, y, false_or_true;
};
int vis[N][N];
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		string b;
		cin >> b;
		for (int j = 1; j <= b.size (); j++)
			a[i][j] = b[j - 1];
	}
	for (int a1 = 1; a1 <= n; a1++) for (int b1 = 1; b1 <= n; b1++) {
		int x, y;
		x = a1; y = b1;
		queue <sb> Q;
		Q.push ((sb){x, y, a[x][y] - '0'});
		bool f[N][N];
		memset (f, false, sizeof (f));
		f[x][y] = true;
		int ans = 0;
		while (Q.size ()) {
			sb b = Q.front ();
			int xx = b.x;
			int yy = b.y;
			int z_o = b.false_or_true;
			Q.pop ();
			for (int i = 0; i < 4; i++) {
				int tx = xx + dx[i];
				int ty = yy + dy[i];
				if (tx < 1 || ty < 1 || tx > n || ty > n || f[tx][ty] || a[tx][ty] == z_o + '0') continue;
				ans++;
				f[tx][ty] = true;
				Q.push ((sb){tx, ty, a[tx][ty] - '0'});
			}
		}
		vis[a1][b1] = ans + 1;
	}		
	while (m--) {
		int x, y;
		cin >> x >> y;
		cout << vis[x][y] << endl;
	}
	return 0;
}
2021/11/18 20:22
加载中...