Result of a group of test data
8 8 3
3 3
4 5
6 6
2 1
7
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path 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)
Shortest Path Length: 11
Number of shortest paths: 96
# Include <iostream> # include <string> # include <cstdio> # include <cstring> # include <cstdlib> # include <queue> # include <stack> using namespace std; const int SIZE = 102; // boundary array, in four directions, int coordinate [4] [2] =, -1, 0, 0,-1}; stack <int> sx; stack <int> sy; stack <int> sxCopy; stack <int> syCopy; int mazeBfs [SIZE] [SIZE]; // The widely searched maze int mazeDfs [SIZE] [SIZE]; // maze int oddEven [SIZE] [SIZE] for deep search; // an array of odd/even pruning status int N; // Number of maze rows int m; // Number of labyrinth columns int k; // Number of enclosed rooms int kr, kl; // number of each enclosed room and column number int p, q; // The row number and column number int r, s of Mouse a; // The row number and column number int ShortestPathLength of Mouse B; // The ShortestPahtNumber length of the ShortestPahtNumber; // Number of Shortest Paths int ans = 1; // output the Shortest Path of the nth ans // search for the shortest path length // int BFS (int p, int q, int r, int s, int len, int n, int m); int BFS (); // perform a deep search to find the number of shortest paths. // 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", & n, & m, & k )! = EOF) {memset (mazeBfs, 0, sizeof (mazeBfs); // initialize the maze memset (mazeDfs, 0, sizeof (mazeDfs )); // initialize the 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", & kr, & kl );// Enter the coordinates of the closed room. // save it to the maze. in the maze, 1 indicates the closed room, and 0 indicates the mazeBfs [kr] [kl] = 1; mazeDfs [kr] [kl] = 1;} scanf ("% d", & p, & q ); // scanf ("% d", & r, & s) of Mouse a; // coordinates of Mouse B // calculates ShortestPathLength = BFS (); if (ShortestPathLength =-1) // when there is No way to go {printf ("No Solution! \ N "); continue;} // calculate the number of ShortestPahtNumber and output all ShortestPahtNumber = 0; sx. push (p); sy. push (q); DFS (p, q, 0); // output result printf ("ShortestPathLength: % d \ n", ShortestPathLength ); printf ("ShortestPahtNumber: % d \ n", ShortestPahtNumber);} return 0;} int BFS () {queue <int> qx; // queue for storing abscissa <int> qy; // queue for storing ordinate values <int> qlen; // queue for storing length int xa, ya; // The current node coordinate int length; // The length of the current node qx. push (p); qy. push (q); qlen. push (0); mazeBfs [p] [q] = 1; wh Ile (! Qx. empty () {if (qx. front () = r) & (qy. front () = s) // determines whether the mouse B {return qlen. front ();} // temporarily Save the team header value int xx, yy, ll; xx = qx. front (); yy = qy. front (); ll = qlen. front (); // release qx after saving. pop (); qy. pop (); qlen. pop (); for (int I = 0; I <4; I ++) {// calculate the new xa = xx + coordinate [I] [0]; ya = yy + coordinate [I] [1]; length = ll; // The new point is in the Maze and has not passed if (xa> = 1) & (xa <= n) & (ya> = 1) & (ya <= m) & (mazeBfs [xa] [ya] = = 0) {// queue qx. push (xa); qy. push (ya); length + = 1; qlen. push (length); // mark the new vertex mazeBfs [xa] [ya] = 1 ;}} return-1; // if there is no path, returns 0} void DFS (int x, int y, int len) {if (x = r) & (y = s) & (len = ShortestPathLength) // find a ShortestPahtNumber ++ ShortestPahtNumber; // output the Shortest Path printf ("Shortest Path % 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"); return;} // common pruning int theoryShortestLength; // theoryShortestLength = (abs (x-r) + (abs (y-s); if (len + theoryShortestLength)> ShortestPathLength) // current Length + theoretical minimum value> shortest path length {return;} // parity pruning 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); // backtracking sx. pop (); sy. pop (); mazeDfs [xx] [yy] = 0 ;}}}