UVA 784 Maze Exploration

来源:互联网 发布:labview 算编程吗 编辑:程序博客网 时间:2024/06/06 01:33

题意: 先找到起始位置,由起始位置开始探索迷宫,在迷宫中如果遇到墙、越界、已来过就返回,否则由该位置向四个方向继续探索。

#include <cstdio>char data[35][85];int row, column, dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};void explor(int x, int y) {if (data[x][y] == '#' || data[x][y] == 'X' || x < 0 || x == row || y < 0 || data[x][y] == 0)return;//遇到越界、已来过、墙就跳过data[x][y] = '#';//标记已来过for (int i = 0; i < 4; i++) //由该位置向 4 个方向遍历迷宫explor(x + dir[i][0], y + dir[i][1]);}int main() {int t;scanf("%d\n", &t);while (t--) {for (row = 0; gets(data[row]) && data[row][0] != '_'; row++);for (int i = 0; i < row ; i++)for (int j = 0; data[i][j] != 0; j++)if (data[i][j] == '*') //如果找到了起始点, 就从此处开始探索迷宫explor(i, j);for (int i = 0; i <= row; i++)puts(data[i]);}return 0;}


0 0
原创粉丝点击