#include <iostream> using namespace std; char maze[8][8]; void dfs(int x, int y); void print() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) cout << maze[i][j]; cout << endl; } } int main() { for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) cin >> maze[i][j]; dfs(0, 0); } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, -1, 0, 1}; void dfs(int x, int y) { if (x == 7 && y == 7) { maze[7][7] = ' '; print(); return ; } else { if (maze[x][y] == 'O' && x >= 0 && x <= 7 && y >= 0 && y <= 7) { // 如果x y能走 则置空 maze[x][y] = ' '; for (int i = 0; i < 4; i++) { maze[x][y] = ' '; int newx = x + dx[i]; int newy = y + dy[i]; dfs(newx, newy); // 若该方向不能走 则将上一个位置复原 maze[x][y] = 'O'; } } } }
标签:求解,int,void,迷宫,dfs,++,&&,maze,P1793 From: https://www.cnblogs.com/lhf123/p/17430760.html