#include<bits/stdc++.h> using namespace std; int n, fy[4]={0,-1,0,1}, fx[4]={-1,0,1,0}; // 调整方向顺序为左、上、右、下 char a[21][21]; // 根据题目N≤20,数组大小设为21足够 bool found = false; // 使用bool类型更合适 void printPoint(int x, int y) { cout << '(' << x << ',' << y << ')'; } void dfs(int x, int y) { if(x == n && y == n) { // 到达终点 found = true; return; } a[x][y] = '1'; // 标记为已访问 for(int i = 0; i < 4; i++) { int tx = x + fx[i]; int ty = y + fy[i]; if(tx > 0 && ty > 0 && tx <= n && ty <= n && a[tx][ty] == '0') { cout << "->"; // 先输出箭头再输出点 printPoint(tx, ty); dfs(tx, ty); if(found) { return; } } } } int main() { cin >> n; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { cin >> a[i][j]; } } printPoint(1, 1); // 输出起点 dfs(1, 1); return 0; } /************************************************************** Problem: 1431 User: zhengzihao Language: C++ Result: Wrong Answer ****************************************************************/