生化武器2【搜索】

In the warm-up, Gogo has used the mysterious “Pidun Shu”, and he found it very powerful. So he decides to perform it to his good friend SJ alone. Gogo performs it in a small room. He disappears suddenly and left the “chemical weapon”, which diffuses to up, down, left and right. And SJ also escapes to the four directions. Within one second, the gas moves first, then, SJ moves. Each time they move one grid. SJ does not want to be poisoned. So he ask you to calculate that SJ can reach the all safe places after t seconds(Of course in the middle, you can not be “poison” too).

當氣體不能擴散的時候,人就覺得安全了,他就不會動了。但,當人不能動的時候,氣體一樣會擴散。

數據保證有且只有一個’G’和一個’S’。


這道題搜索不難,有一個大坑需要注意/(ㄒoㄒ)/~~WA在這好幾次

  1. 一旦氣體無法擴散,人也就不會走了,可以直接break。如何判斷氣體無法擴散呢?只要一次擴散過程中氣體的格數不再增加即可。

然後我們按照,G可以覆蓋S和。, S只能覆蓋。的原則來進行擴散,直到所有時間都用光了或者全場沒有任何S可以行動。

string a[MAX];
ll gx, gy, sx, sy, M, N, T, t;
ll dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };

int main() {
	cin >> T;
	while (T--) {
		cin >> M >> N >> t;
		for (int i = 1; i <= M; i++) {
			cin >> a[i];
			for (int j = 0; j < N; j++) {
				if (a[i][j] == 'G')gx = i, gy = j;
				else if (a[i][j] == 'S')sx = i, sy = j;
			}
		}
		//q1存儲poison擴散的地方,q2存儲用戶可以跑到的地方
		queue<P> q1, q2; q1.push(P(gx, gy));
		vector<P> v1, v2; q2.push(P(sx, sy));
		ll cnt = 1;//記錄S的個數
		//q1.size()>0,氣體不能擴散的時候,人就不會動了
		for (int i = 1; i <= t && cnt > 0 && q1.size() > 0; i++) {
			v1.clear(); v2.clear(); cnt = 0; ll sign = 0;//標記氣體是否擴散了
			while (!q1.empty()) {
				ll tx = q1.front().first, ty = q1.front().second; q1.pop();
				for (int j = 0; j < 4; j++) {
					ll x = tx + dx[j], y = ty + dy[j];
					if (x<1 || x>M || y < 0 || y >= N || a[x][y] == 'X' || a[x][y] == 'G')
						continue;//已經是病毒或者是牆
					if (a[x][y] == '.' || a[x][y] == 'S') {//可以擴散
						a[x][y] = 'G'; v1.push_back(P(x, y)); sign = 1;
					}
				}
			}
			if (!sign)break;//氣體不擴散,人就不動了
			for (unsigned i = 0; i < v1.size(); i++)q1.push(v1[i]);
			while (!q2.empty()) {
				ll tx = q2.front().first, ty = q2.front().second; q2.pop();
				if (a[tx][ty] == 'G')continue;//這個地方已經被侵染了
				for (int j = 0; j < 4; j++) {
					ll x = tx + dx[j], y = ty + dy[j];
					if (x<1 || x>M || y < 0 || y >= N || a[x][y] == 'X' || a[x][y] == 'S' || a[x][y] == 'G')
						continue;
					a[x][y] = 'S'; v2.push_back(P(x, y));
				}
			}
			for (unsigned j = 0; j < v2.size(); j++)q2.push(v2[j]);
			for (int j = 1; j <= M; j++)
				for (int k = 0; k < N; k++)
					if (a[j][k] == 'S')cnt++;
		}
		cnt = 0;
		for (int j = 1; j <= M; j++)
			for (int k = 0; k < N; k++)
				if (a[j][k] == 'S')cnt++;
		if (cnt > 0) 
			for (int i = 1; i <= M; i++)
				cout << a[i] << endl;
		else cout << "No" << endl;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章