Plus maze topic in-depth search

Topic: Defining a two-dimensional array:
int maze[5][5] = {
 0, 1, 0, 0, 0,
 0, 1, 0, 1, 0,
 0, 0, 0, 0, 0,
 0, 1, 1, 1, 0,
 0, 0, 0, 1, 0,
};
It represents a labyrinth, in which 1 represents the wall, 0 way to go, can only go sideways or vertically to go, can not go sideways, requiring programmed to find out from the top left to the bottom right corner of the number of pieces of route.

Code
#include <iostream>
#include <cstdio>
using namespace std;
bool G[10][10],VIS[10][10];
int d[5]= {-1,0,1,0,-1};
int n,m,nx,ny,ex,ey,CNT;
void dfs(int x,int y) {
 if (x ==ex&&y ==ey) {
  CNT++;
  return;
 }
 for(int k=0; k<4; k++) {
  int l=x+d[k];
  int r=y+d[k+1];
  if (l>=0&&r>=0&&l<=n&&r<=m&&!G [l][r]&&!VIS [l][r]) {       //注意起始位置为(0,0),
   VIS [l][r]=true;
   dfs (l,r);
   VIS [l][r]=false;  //回溯
  }
 }
 return;
}
int main () {
 int t,zx,zy;
 n=4;m=4;
 nx=0;ny=0;ex=4;ey=4;
 G [nx] [] = 0;
 G [0] [. 1] = to true;
 G [. 1] [. 1] = to true;
 G [. 1] [. 3] = to true;
 G [. 3] [. 1] = to true;
 G [. 3] [2] = to true;
 G [. 3] [. 3] = to true;
 G [. 4] [. 3] = to true;
 DFS (NX, NY);
 COUT << the CNT;                  how much output line //
 return 0;
}

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11618559.html