提交时间:2023-10-28 10:01:05
运行 ID: 107796
#include<iostream> #include<vector> using namespace std; const int way[4][2] = {{1, 1}, {0, 1}, {1, 0}, {1, -1}, {1, -1}, {-1, 0}, {0, -1}, {-1, -1}}, N = 110; int a[N][N], m, n; bool s = false, b[N][N], vis[N][N]; struct xy{int x, y;}; vector<xy> vec; void f(int x, int y){ if(s) return; if(x == m-1 && y == n-1){ s = true; return; } for(int i = 0; i < 4; i++){ int ax = x + way[i][0], ay = y + way[i][1]; if(ax < 0 || ax >= m || ay < 0 || ay >= n) continue; if(b[ax][ay] || vis[ax][ay]) continue; vis[ax][ay] = true; vec.push_back({ax+1, ay+1}); f(ax, ay); if(s) return; vec.pop_back(); vis[ax][ay] = false; } } int main(){ cin >> m >> n; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cin >> b[j][i]; } } vec.push_back({1, 1}); f(0, 0); for(int i = 0; i < vec.size(); i++) cout << vec[i].y << ' ' << vec[i].x << endl; return 0; }