Class MouseMaze { Public: MouseMaze (int * maze, int width, int height, int startx, int starty, int endx, int endy) { This-> maze = maze; This-> width = width; This-> height = height; This-> startx = startx; This-> starty = starty; This-> endx = endx; This-> endy = endy; } Int visit (int x, int y) { Maze [x * width + y] = 1; If (x = endx & y = endy) { Output (); Return TRUE; } Int success = FALSE; If (success = FALSE & x + 1 <width & maze [(x + 1) * width + y] = 0) { Success = visit (x + 1, y ); } If (success = FALSE & x-1> = 0 & maze [(x-1) * width + y] = 0) { Success = visit (x-1, y ); } If (success = FALSE & y + 1 { Success = visit (x, y + 1 ); } If (success = FALSE & y-1> = 0 & maze [x * width + Y-1] = 0) { Success = visit (x, y-1 ); } If (success = FALSE) { Maze [x * width + y] = 0; } Return success; } Void visitAll (int x, int y) { Maze [x * width + y] = 1; If (x = endx & y = endy) { Output (); Maze [x * width + y] = 0; Return; } If (x + 1 <width & maze [(x + 1) * width + y] = 0) { VisitAll (x + 1, y ); } If (x-1> = 0 & maze [(x-1) * width + y] = 0) { VisitAll (x-1, y ); } If (y + 1 { VisitAll (x, y + 1 ); } If (Y-1> = 0 & maze [x * width + Y-1] = 0) { VisitAll (x, y-1 ); } Maze [x * width + y] = 0; } Void output () { If (maze = NULL) { Return; } For (int I = 0; I <width; I ++) { For (int j = 0; j { Printf ("% d \ t", maze [I * width + j]); } Printf ("\ n "); } Printf ("------------------------------------------------------------- \ n "); } Protected: Private: Int * maze; Int width; Int height; Int startx; Int starty; Int endx; Int endy; }; Int _ tmain (int argc, _ TCHAR * argv []) { Int maze [9] [9] = {2, 2, 2, 2, 2, 2, 2, 2 }, {2, 0, 0, 0, 0, 0, 0, 0, 2 }, {2, 0, 2, 2, 0, 2, 2, 0, 2 }, {2, 0, 2, 0, 0, 2, 0, 0, 2 }, {2, 0, 2, 0, 2, 0, 2, 0, 2 }, {2, 0, 0, 0, 0, 0, 2, 0, 2 }, {2, 2, 0, 2, 2, 0, 2, 2 }, {2, 0, 0, 0, 0, 0, 0, 0, 2 }, {2, 2, 2, 2, 2, 2, 2, 2, 2 }}; MouseMaze m (& maze [0] [0], 9, 9, 1, 1, 7, 7 ); M. visitAll (1, 1 ); Return 0; } |