迷宮問題,求最短路徑長度、最短路徑條數、所有最短路徑,DFS+BFS+棧+隊列

來源:互聯網
上載者:User

一組測試資料得到的結果

8 8 3
3 3
4 5
6 6
2 1
7 7

最短路徑   1 :
(2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (7, 1) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   2 :
(2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (6, 2) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   3 :
(2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (6, 2) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   4 :
(2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (6, 2) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   5 :
(2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑   6 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (6, 2) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   7 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (6, 2) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   8 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (6, 2) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑   9 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (6, 2) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  10 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  11 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  12 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  13 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  14 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  15 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  16 :
(2, 1) (3, 1) (4, 1) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  17 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (6, 2) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  18 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (6, 2) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  19 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  20 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  21 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  22 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  23 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  24 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  25 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  26 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  27 :
(2, 1) (3, 1) (4, 1) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  28 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  29 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  30 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  31 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  32 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  33 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  34 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  35 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  36 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  37 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  38 :
(2, 1) (3, 1) (4, 1) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  39 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (6, 2) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  40 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  41 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  42 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  43 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  44 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  45 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  46 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  47 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  48 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  49 :
(2, 1) (3, 1) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  50 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  51 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  52 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  53 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  54 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  55 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  56 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  57 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  58 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  59 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  60 :
(2, 1) (3, 1) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  61 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (7, 2) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  62 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  63 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  64 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  65 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  66 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  67 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  68 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  69 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  70 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  71 :
(2, 1) (2, 2) (3, 2) (4, 2) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  72 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (7, 3) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  73 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  74 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (6, 3) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  75 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  76 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  77 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  78 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (5, 3) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  79 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  80 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  81 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  82 :
(2, 1) (2, 2) (3, 2) (4, 2) (4, 3) (4, 4) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  83 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4) (5, 4) (6, 4) (7, 4) (7, 5) (7, 6) (7, 7)

最短路徑  84 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4) (5, 4) (6, 4) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  85 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4) (5, 4) (5, 5) (6, 5) (7, 5) (7, 6) (7, 7)

最短路徑  86 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4) (5, 4) (5, 5) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  87 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (3, 5) (3, 6) (4, 6) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  88 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (3, 5) (3, 6) (4, 6) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  89 :
(2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (3, 5) (3, 6) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  90 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (3, 5) (3, 6) (4, 6) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  91 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (3, 5) (3, 6) (4, 6) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  92 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (3, 5) (3, 6) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  93 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (3, 6) (4, 6) (5, 6) (5, 7) (6, 7) (7, 7)

最短路徑  94 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (3, 6) (4, 6) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  95 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (3, 6) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑  96 :
(2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) (2, 7) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7)

最短路徑長度: 11

最短路徑條數: 96

 

#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <stack>using namespace std;const int SIZE = 102;//邊界數組,四個方向,按照下、右、上、左的順序int coordinate[4][2] = {1,0, 0,1, -1,0, 0,-1};stack<int> sx;stack<int> sy;stack<int> sxCopy;stack<int> syCopy;int mazeBfs[SIZE][SIZE];  //廣搜用的迷宮int mazeDfs[SIZE][SIZE];  //深搜用的迷宮int oddEven[SIZE][SIZE];  //奇偶剪枝狀態數組int n;  //迷宮行數int m;  //迷宮列數int k;  //封閉房間數int kr, kl; //每個封閉房間的行號和列號int p, q;  //小鼠a的行號和列號int r, s;  //小鼠b的行號和列號int ShortestPathLength; //最短路徑的長度int ShortestPahtNumber; //最短路徑的條數int ans = 1; //輸出第ans條最短路徑//廣搜求最短路徑長度//int BFS(int p, int q, int r, int s, int len, int n, int m);int BFS();//深搜求最短路徑條數//void DFS(int x, int y, int r, int s, int len, int n, int m, int shortlength);void DFS(int x, int y, int len);int main(){    while (scanf("%d%d%d", &n, &m, &k) != EOF)    {        memset(mazeBfs, 0, sizeof(mazeBfs));  //初始化迷宮        memset(mazeDfs, 0, sizeof(mazeDfs));        //奇偶剪枝數組初始化        for (int i=0; i<=n; i++)        {            if (i%2 == 1)            {               for (int j=0; j<=m; j++)               {                   if (j%2 == 1)                   {                       oddEven[i][j] = 1;                   }                   else                   {                       oddEven[i][j] = 0;                   }               }            }            else            {                for (int j=0; j<=m; j++)                {                    if (j%2 == 1)                    {                        oddEven[i][j] = 0;                    }                    else                    {                        oddEven[i][j] = 1;                    }                }            }        }        for (int i=1; i<=k; i++)        {            scanf("%d%d", &kr, &kl);  //輸入封閉房間的座標            //存入迷宮中,迷宮中,1代表封閉房間,0代表可以走            mazeBfs[kr][kl] = 1;            mazeDfs[kr][kl] = 1;        }        scanf("%d%d", &p, &q);  //小鼠a的座標        scanf("%d%d", &r, &s);  //小鼠b的座標        //求最短路徑長度        ShortestPathLength = BFS();        if (ShortestPathLength == -1) //沒路可走時        {            printf("No Solution!\n");            continue;        }        //求最短路徑條數及輸出所有的最短路徑        ShortestPahtNumber = 0;        sx.push(p);        sy.push(q);        DFS(p, q, 0);        //輸出結果        printf("最短路徑長度: %d\n\n", ShortestPathLength);        printf("最短路徑條數: %d\n\n", ShortestPahtNumber);    }    return 0;}int BFS(){    queue<int> qx;  //存橫座標的隊列    queue<int> qy;  //存縱座標的隊列    queue<int> qlen;  //存長度的隊列    int xa, ya; //當前節點座標    int length; //到達當前節點長度    qx.push(p);    qy.push(q);    qlen.push(0);    mazeBfs[p][q] = 1;    while (!qx.empty())    {        if ((qx.front()==r) && (qy.front()==s)) //判斷是否到達小鼠b        {            return qlen.front();        }        //臨時儲存隊頭值        int xx, yy ,ll;        xx = qx.front();        yy = qy.front();        ll = qlen.front();        //儲存完之後,出隊        qx.pop();        qy.pop();        qlen.pop();        for (int i=0; i<4; i++)        {            //算第i方向上的新值            xa = xx + coordinate[i][0];            ya = yy + coordinate[i][1];            length = ll;            //新的點在迷宮內,且沒有走過            if ((xa>=1) && (xa<=n) && (ya>=1) && (ya<=m) && (mazeBfs[xa][ya]==0))            {                //入隊                qx.push(xa);                qy.push(ya);                length += 1;                qlen.push(length);                //標記新點                mazeBfs[xa][ya] = 1;            }        }    }    return -1;  //如果沒有路,返回0}void DFS(int x, int y, int len){    if ((x==r) && (y==s) && (len==ShortestPathLength))  //找到一條最短路徑    {        ShortestPahtNumber++;        //輸出最短路徑        printf("最短路徑 %3d :\n", ans++);        int j = sx.size();        for (int i=1; i<=j; i++)        {           sxCopy.push(sx.top());           sx.pop();           syCopy.push(sy.top());           sy.pop();        }        for (int i=1; i<=j; i++)        {            printf("(%d, %d) ", sxCopy.top(), syCopy.top());            sx.push(sxCopy.top());            sxCopy.pop();            sy.push(syCopy.top());            syCopy.pop();        }        printf("\n\n");        return ;    }    //一般剪枝    int theoryShortestLength; //當前節點到終點的理論最小值    theoryShortestLength = (abs(x-r)) + (abs(y-s));    if ((len+theoryShortestLength) > ShortestPathLength) //當前長度+理論最小值>最短路徑長度    {        return ;    }    //奇偶剪枝    if ((ShortestPathLength-len)%2 != ((abs(oddEven[x][y]-oddEven[r][s])) % 2))    {        return ;    }    for (int i=0; i<4; i++)    {        int xx, yy;        xx = x + coordinate[i][0];        yy = y + coordinate[i][1];        if ((xx>=1) && (xx<=n) && (yy>=1) && (yy<=m) && (mazeDfs[xx][yy]==0))        {            sx.push(xx);            sy.push(yy);            mazeDfs[xx][yy] = 1;            DFS(xx, yy, len+1);            //回溯            sx.pop();            sy.pop();            mazeDfs[xx][yy] = 0;        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.