Test Message

b689: 2. 棕櫚迷宮

內容

img1


輸入

7 9
#########
##.....##
##.###.##
##.#...##
##.#.##..
#..#....#
#########

7 9
######.##
##.....##
##.####.#
##.#....#
##.#.##.#
#..#....#
#########

10 10
##########
#....#####
#.##.....#
#...####.#
#######..#
##....#.##
##.##.#..#
##.##.##.#
##..#....#
###.######

輸出

6 2
6 2
4 4


解題思路

題目保證單向、無叉路且僅有一個入口,所以用 dfs 遍歷到最深處後輸出該點即可。


完整程式碼

AC (2ms, 108KB)
#include <stdio.h>

int x, y;
char map[25][25];

void dfs()
{
map[x][y] = '#';
if (map[x + 1][y] == '.')
++x, dfs();
else if (map[x - 1][y] == '.')
--x, dfs();
else if (map[x][y + 1] == '.')
++y, dfs();
else if (map[x][y - 1] == '.')
--y, dfs();
}

void getStart(int n, int m)
{
for (int i = 0; i <= n; i++)
{
if (map[i][0] == '.')
{
x = i, y = 0; return;
}
else if (map[i][m] == '.')
{
x = i, y = m; return;
}
}
for (int i = 1; i < m; i++)
{
if (map[0][i] == '.')
{
x = 0, y = i; return;
}
else if (map[n][i] == '.')
{
x = n, y = i; return;
}
}
}

int main()
{
int n, m;
while (scanf(" %d %d", &n, &m) == 2)
{
getchar();
for (int i = 0; i < n; i++)
gets(map[i]);
getStart(n - 1, m - 1);
dfs();
printf("%d %d\n", x + 1, y + 1);
}
return 0;
}