求助站外题!很急!!!在线等
  • 板块灌水区
  • 楼主wangshuhan0121
  • 当前回复10
  • 已保存回复10
  • 发布时间2024/8/27 09:28
  • 上次更新2024/8/27 11:52:12
查看原帖
求助站外题!很急!!!在线等
1012159
wangshuhan0121楼主2024/8/27 09:28

站外题网址

#include<bits/stdc++.h>
using namespace std;
int s, n, m, h, t, q[40005][3];
int fx[5] = {0, 0, 1, 0, -1};
int fy[5] = {0, 1, 0, -1, 0};
int tx, ty;
bool f[205][205], r;
char a[205][205];
int sun;
int main() {
	cin >> s;
	while (s) {
		h = 1;
		t = 1;
		sun = 0;
		memset(f, 0, sizeof(f));
		int x, y, xx, yy;
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> a[i][j];
				if (a[i][j] == 'r') {
					x = i;
					y = j;
				}
				if(a[i][j] == 'a'){
					xx = i;
					yy = j;
				}
				if(a[i][j] == '#')f[i][j]=1;
			}
		}
		q[1][1] = x;
		q[1][2] = y;
		f[x][y] = 1;
		while (h <= t) {
			for (int i = 1; i <= 4; i++) {
				tx = q[h][1] + fx[i];
				tx = q[h][2] + fy[i];
				if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !f[tx][ty] && a[tx][ty] != '#') {
					t++;
					q[t][1] = tx;
					q[t][2] = ty;
					sun++;
					if (a[tx][ty] == 'x')sun++;
					f[tx][ty] = 1;
				}
				if (a[q[t][1]][q[t][2]] == 'a') {
					r = 1;
					h=t+1;
					break;
				}
			}

			h++;
		}
		if (r == 1)cout << sun << '\n';
		else cout << "Impossible" << '\n';
		r = 0;
		s--;
	}
	return 0;
}

谢谢了大佬

2024/8/27 09:28
加载中...