目錄導航
- 圖解
- 體會領悟:
- 代碼實作(Java):
- C語言版
- C++版
為了復習遞回,而模擬學習的,所以迷宮不大,總體是8行7列,
圖解
A為起點,B為終點,

如下圖在A和B之間設定擋板被隔絕之后,結果如下,


體會領悟:
我做這個模擬之后對遞回的理解就是像這個{{{}}}的字串,只有最中間的括號配對成功了,同時發送一個成功的信號給第二括號,他才能開始配對,就像第一個本方法呼叫第二個本方法,但是第一個本方法還只執行一半沒有執行完,他要等第二個本方法將結果回傳給他,他才能完成自己的作業,而第二個本方法又依賴于第三個本方法回傳的結果,直到規定的邊界條件被觸發,結果一層一層地回傳,
PS:是不是有套娃的既視感,
代碼實作(Java):
package demo;
public class MiGong {
public static void main(String[] args) {
//為0,表示沒走過;為1,表示擋板或墻;為2,表示走過,可以走通;為3表示死路,走不通
//構建迷宮上下圍墻
int[][] array = new int[8][7];
for(int i = 0;i < 7;i++) {
array[0][i] = 1;
array[7][i] = 1;
}
//設定擋板,此處可自行變換,來模擬出不同的路徑
array[3][0] = 1;array[3][1] = 1;
for(int i = 0;i < 8;i++) {
array[i][0] = 1;
array[i][6] = 1;
}
//設定迷宮的左右圍墻
for(int i = 0;i < 8;i++) {
for(int j = 0;j < 7;j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
findWay(array,1,1);
System.out.println("----------------------");
for(int i = 0;i < 8;i++) {
for(int j = 0;j < 7;j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static boolean findWay(int[][] array,int i,int j) {
if(array[6][5] == 2) {//當此處為2,表示已經走到目的地了
return true;
}else {
if(array[i][j] == 0) {
array[i][j] = 2;//假設可以走,下面采用下右上左的尋找策略(尋找策略跟起點和終點的方位有關)
if(findWay(array,i+1,j)) {
return true;
}else if(findWay(array,i,j+1)) {
return true;
}else if(findWay(array,i-1,j)) {
return true;
}else if(findWay(array,i,j-1)) {
return true;
}else {//此點下右上左都不行,則為死路,為3
array[i][j] = 3;
return false;
}
}else {//為1,表示擋板或墻不能走;為2,已經走過沒有意義;為3,死路走不通的
return false;
}
}
}
}
C語言版
工具為codeblocks
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool findWay(int array[][7],int i,int j){
if(array[6][5] == 2){
return true;
}else{
if(array[i][j] == 0){
array[i][j] = 2;
if(findWay(array,i+1,j)){
return true;
}else if(findWay(array,i,j+1)){
return true;
}else if(findWay(array,i-1,j)){
return true;
}else if(findWay(array,i,j-1)){
return true;
}else{
array[i][j] = 3;
return false;
}
}else{
return false;
}
}
}
int main()
{
int map[8][7];
int i,j;
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
map[i][j] = 0;
}
}
for(i = 0;i < 7;i++){
map[0][i] = 1;
map[7][i] = 1;
}
for(i = 0;i < 8;i++){
map[i][0] = 1;
map[i][6] = 1;
}
map[3][0] = 1;map[3][1] = 1;
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
printf("%2d",map[i][j]);
}
printf("\n");
}
findWay(map,1,1);
printf("---------------------\n");
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
printf("%2d",map[i][j]);
}
printf("\n");
}
return 0;
}

C++版
#include <iostream>
#include <vector>
using namespace std;
bool findWay(int (*array)[7],int i,int j);
bool findWay(int (*array)[7],int i,int j){
if(array[6][5] == 2){
return true;
}else{
if(array[i][j] == 0){
array[i][j] = 2;
if(findWay(array,i+1,j)){
return true;
}else if(findWay(array,i,j+1)){
return true;
}else if(findWay(array,i-1,j)){
return true;
}else if(findWay(array,i,j-1)){
return true;
}else{
array[i][j] = 3;
return false;
}
}else{
return false;
}
}
}
int main()
{
int map[8][7];
int i,j;
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
map[i][j] = 0;
}
}
for(i = 0;i < 7;i++){
map[0][i] = 1;
map[7][i] = 1;
}
for(i = 0;i < 8;i++){
map[i][0] = 1;
map[i][6] = 1;
}
map[3][0] = 1;map[3][1] = 1;
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
cout<<map[i][j]<<" ";
}
cout<<"\n"<<endl;
}
findWay(map,1,1);
printf("---------------------\n");
for(i =0;i < 8;i++){
for(j = 0;j < 7;j++){
cout<<map[i][j]<<" ";
}
cout<<"\n"<<endl;
}
return 0;
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/260565.html
標籤:java
