1
作者:
Baiye959
,
2022-09-30 11:38:17
,
所有人可见
,
阅读 186
#include <iostream>
#include <stack>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
stack<PII> stk;
const int dir[4][2]={{0,1}, {1,0}, {0,-1}, {-1,0}};
int a[15][15];
int n;
int dfs(int x, int y){
if(x==n-2 && y==n-2){
stk.push({x, y});
return 1;
}
if(x<=0 || x>=n-1 || y<=0 || y>=n-1 || a[x][y]) return 0;
for(int i=0;i<4;i++){
a[x][y]=1;
if(dfs(x+dir[i][0], y+dir[i][1])==1){
stk.push({x, y});
return 1;
}
}
a[x][y]=0;
return 0;
}
int main()
{
cin >> n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin >> a[i][j];
}
}
int p;
p=dfs(1, 1);
if(!p) cout << "NO";
else{
while(!stk.empty()){
auto cur=stk.top();
int x=cur.first;
int y=cur.second;
cout << '(' << x << ',' << y << ')';
stk.pop();
}
}
return 0;
}