#include <iostream> #include <stack> #include <vector> #include <string> #include <algorithm> #include <map> #include <set> #include <queue> #include <stdio.h> using namespace std; bool endres = false; vector<pair<int,int> > res; int way[4][2] = { { 0,1 }, { 1,0 }, { 0,-1 }, { -1,0 } }; void dfs(int maze[][6], int x, int y, int k) { if (x < 0 || y < 0 || x >= 5 || y >= 5 || maze[x][y] == 1) return; res[k] = pair<int, int>(x,y); if (x == 4 && y == 4) { for (int i = 0; i <= k; i++) printf("(%d, %d)\n", res[i].first, res[i].second); endres = true; return; } for (int i = 0; i < 4; i++) { if (endres) return; maze[x][y] = 1; dfs(maze, x + way[i][0], y + way[i][1], k + 1); maze[x][y] = 0; } } int main() { int maze[5+1][5+1]; for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) cin >> maze[i][j]; res.resize(5*5); dfs(maze,0,0,0); return 0; } |
Double click to view unformatted code.