POJ-2157:Maze(特殊的bfs方式)

題目鏈接

Description
Acm, a treasure-explorer, is exploring again. This time he is in a special maze, in which there are some doors (at most 5 doors, represented by ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ respectively). In order to find the treasure, Acm may need to open doors. However, to open a door he needs to find all the door’s keys (at least one) in the maze first. For example, if there are 3 keys of Door A, to open the door he should find all the 3 keys first (that’s three ‘a’s which denote the keys of ‘A’ in the maze). Now make a program to tell Acm whether he can find the treasure or not. Notice that Acm can only go up, down, left and right in the maze.
Input
The input consists of multiple test cases. The first line of each test case contains two integers M and N (1 < N, M < 20), which denote the size of the maze. The next M lines give the maze layout, with each line containing N characters. A character is one of the following: ‘X’ (a block of wall, which the explorer cannot enter), ‘.’ (an empty block), ‘S’ (the start point of Acm), ‘G’ (the position of treasure), ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ (the doors), ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ (the keys of the doors). The input is terminated with two 0’s. This test case should not be processed.
Output
For each test case, in one line output “YES” if Acm can find the treasure, or “NO” otherwise.
Sample Input
4 4
S.X.
a.X.
…XG
….
3 4
S.Xa
.aXB
b.AG
0 0
Sample Output
YES
NO
Source
POJ Monthly,Wang Yijie

題目大意

有一個迷宮,迷宮裏有最多五種門和對應的鑰匙,如果把迷宮中的某種鑰匙全部拿到就可以打開對應的門。

思路

這道題數據範圍是n,m<=20,所以dfs和bfs都能做,我這裏只打了bfs(迷宮問題當然要用bfs╭(╯^╰)╮),思路是,先從起點bfs一遍,如果能到終點就直接輸出YES,否則查看拿到的鑰匙,把能打開的門全部打開(在地圖上標記爲可以走的路),不能打開的門要保留下來,說不定下一次就能打開,然後再跑bfs,直到沒有搜到終點並且沒有搜到新的鑰匙,這時退出循環輸出NO

#define ll long long
#define vec vector<int>
#define P pair<int,int>
#define inf 0x3f3f3f3f
#define MAX 25

int M, N, num[5], sx, sy, tx, ty, key[5];
bool vis[MAX][MAX];
int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
string s[MAX];

bool check(int x, int y) {
	//超出邊界或者撞牆或者是一個門
	if (x < 0 || x >= M || y < 0 || y >= N || s[x][y] == 'X')return false;
	return true;
}

bool bfs(int sx, int sy) {
	memset(vis, 0, sizeof(vis));
	memset(key, 0, sizeof(key));//統計走過的路中所有的鑰匙集合
	vis[sx][sy] = 1; queue<P> q; 
	q.push(P(sx, sy)); vector<P> v;
	while (1) {
		if (q.empty())return false;
		while (!q.empty()) {
			int x = q.front().first, y = q.front().second; q.pop();
			for (int i = 0; i < 4; i++) {
				int xx = x + dx[i], yy = y + dy[i];
				if (check(xx, yy) && !vis[xx][yy]) {
					vis[xx][yy] = 1;
					//這是一個門,把能訪問到的門單獨加入
					if (s[xx][yy] >= 'A'&&s[xx][yy] <= 'E') {
						v.push_back(P(xx, yy));//門加入隊列
					}
					else {
						//找到一個鑰匙
						if (s[xx][yy] >= 'a'&&s[xx][yy] <= 'e')
							key[s[xx][yy] - 'a']++;//找到了新鑰匙
						q.push(P(xx, yy));
						if (xx == tx && yy == ty)return true;
					}
				}

			}
		}
		//無路可走,看看能不能開門,開了的門要刪掉,不能開的門要留下說不定下次可以
		int i = 0;
		while (i < v.size()) {
			int xx = v[i].first, yy = v[i].second;
			if (key[s[xx][yy] - 'A'] == num[s[xx][yy] - 'A'])
				q.push(P(xx, yy)), v.erase(v.begin() + i);//鑰匙夠了,可以開
			else i++;
		}
	}
}

int main() {
	while (scanf("%d %d", &M, &N) && M + N != 0) {
		memset(num, 0, sizeof(num));
		for (int i = 0; i < M; i++) {
			cin >> s[i];
			for (int j = 0; j < N; j++) {
				if (s[i][j] == 'S')sx = i, sy = j;
				else if (s[i][j] == 'G')tx = i, ty = j;
				else if (s[i][j] >= 'a'&&s[i][j] <= 'e')
					num[s[i][j] - 'a']++;
			}
		}

		if (bfs(sx, sy))printf("YES\n");
		else printf("NO\n");
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章