#include<iostream> using namespace std; int maze[5][5]; //0表示没有走过 1表示墙 2表示走过 3表示思路 bool GetWay(int i, int j) { if(i<0||i>=5||j<0||j>=5) return false; if(maze[4][4]==2) return true; else { //下右上左 if(maze[i][j]==0) { maze[i][j]=2; if(GetWay(i+1,j)) { return true; }else if(GetWay(i,j+1)) { return true; }else if(GetWay(i-1,j)) { return true; }else if(GetWay(i,j-1)) { return true; }else { maze[i][j]=3; return false; } }else { return false; } } } int main() { //输入 for(int i=0; i<5; i++) { for(int j=0; j<5; j++) cin>>maze[i][j]; } GetWay(0,0); //输出 for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { if(maze[i][j]==2) cout<<"("<<i<<", "<<j<<")"<<endl; } } } |
Double click to view unformatted code.