天天看點

UVA 532 Dungeon MasterUVA 532 Dungeon Master

UVA 532 Dungeon Master

題意:構造一個三維的迷宮,S代表目前位置,E代表出口,要判斷能不能到出口,如果能,還要輸出需要的時間(每走一步1分鐘)。 思路: 這題是用到3維數組的搜尋,一開始用DFS果斷超過,卡了半天。坑爹。。。換BFS就過了。。。 代碼:

#include <stdio.h>
#include <string.h>
struct fuck
{
    int x;
    int y;
    int z;
    int d;
} q[28888];
int d[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
int l, r, c;
char map[35][35][35];
int x, y, z;
int judge;
void bfs(int x, int y, int z)
{
    memset(q, 0, sizeof(q));
    int hx, hy, hz;
    int head = 0, rear = 1;
    q[0].x = x;
    q[0].y = y;
    q[0].z = z;
    q[0].d = 0;
    while (head < rear)
    {
	for (int i = 0; i < 6; i ++)
	{
	    hx = q[head].x + d[i][0];
	    hy = q[head].y + d[i][1];
	    hz = q[head].z + d[i][2];
	    if (map[hx][hy][hz] == 'E')
		judge = q[head].d + 1;
	    if (map[hx][hy][hz] == '.')
	    {
		map[hx][hy][hz] = '#';
		q[rear].x = hx;
		q[rear].y = hy;
		q[rear].z = hz;
		q[rear++].d = q[head].d + 1;
	    }
        }
	head ++;
    }
} 
int main()
{
    while (scanf("%d%d%d", &l, &r, &c) != EOF && l && r && c)
    {
	judge = -1;
	getchar();
	memset(map, 0, sizeof(map));
	for (int i = 0; i <= l + 1; i ++)
	    for (int j = 0; j <= r + 1; j ++)
		for (int k = 0; k <= c + 1; k ++)
		{
		    map[i][j][k] = '#';
		}
	for (int i = 1; i <= l; i ++)
	{
	    for (int j = 1; j <= r; j ++)
	    {
		for (int k = 1; k <= c; k ++)
		{
		    scanf("%c", &map[i][j][k]);
		    if (map[i][j][k] == 'S')
		    {
			x = i; y = j; z = k;
		    }
		}
		getchar();
	    }
	    getchar();
	}	
	bfs(x, y, z);
	if (judge != -1)
	    printf("Escaped in %d minute(s).\n", judge);
	else
	    printf("Trapped!\n");
    }	
    return 0;
}