#include<bits/stdc++.h>
using namespace std;
int n;
char maze[21][21]; // 迷宫矩阵
int path[400][2]; // 存储路径坐标,最多400步(20x20)
int pathLength = 0; // 当前路径长度
// 方向数组:左、上、右、下
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
// 打印路径
void printPath() {
for (int i = 0; i < pathLength; i++) {
if (i != 0) cout << "->";
cout << "(" << path[i][0] << "," << path[i][1] << ")";
}
cout << endl;
}
// 深度优先搜索
bool dfs(int x, int y) {
// 记录当前路径点
path[pathLength][0] = x;
path[pathLength][1] = y;
pathLength++;
// 到达终点
if (x == n && y == n) {
printPath();
return true;
}
// 标记当前位置已访问
maze[x][y] = '1';
// 按照左、上、右、下的顺序搜索
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
// 检查是否在迷宫范围内且可以通行
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n && maze[nx][ny] == '0') {
if (dfs(nx, ny)) {
return true; // 找到路径,立即返回
}
}
}
// 回溯:路径长度减1
pathLength--;
return false;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> maze[i][j];
}
}
dfs(1, 1); // 从起点开始搜索
return 0;
}
/**************************************************************
Problem: 1431
User: zhengzihao
Language: C++
Result: Accepted
Time:37 ms
Memory:2076 kb
****************************************************************/