View Code of Problem 3499

#include<iostream>
using namespace std;
int maze[5][5];//表示迷宫 从(0,0)出发到(4,4) 
//找路的方法 
//规定0表示没有走过的地方
//	  1表示墙 2表示可以走通的地方 3表示死路 
//规定每个地方按照下右上左的方法找路 
bool getWay(int i, int j) {
	//注意边界!!! 
	if(i>=5||j>=5||i<0||j<0) 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 {//1 2 3 都不能走 
			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++) 
//			cout<<maze[i][j]<<" "; 
//		cout<<endl;
//	}
	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.


Back to problem 3499