Deep Priority Search-space for solving the maze

Source: Internet
Author: User

Deep Priority Search-space for solving the maze
1. Principle description:
The initial state of a given graph G is that all vertices have never been accessed. In G, select a vertex v as the initial starting point (source point and root node ).
The description is as follows: first, access start point V and mark it as accessed. Then, search for each adjacent vertex (subnode) W of V from start V in sequence.
If W has never been accessed, start with w as the new starting point and continue to traverse the depth first until all vertices with the same path as the Source Vertex V in the graph (the vertices that can be reached from the Source Vertex) have been accessed. If there are still unaccessed vertices in this graph, select another unaccessed vertex as the New Source Vertex to repeat the above process until all vertices in the graph have been accessed.

2. Algorithm Description
(1) determine the graph storage method.
(2) design the operations in the search process, including the storage operations for the output problem solution.
(3) If a solution to the problem is found, the result is output; otherwise, the result is traced back.
(4) generally, the node should be restored to the original state before backtracking, especially in the Multi-solution problem.

3. One implementation: The Maze problem. Given the portal, we can find the exit space.

Public class migong {/** author long */Private Static int [] [] maze = {0, 0, 0, 0, 0, 0, 0, 0, 0 }, {0, 1, 1, 1, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 1}, {0, 1, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 0 }}; Private Static int f [] [] = {1,-, 0}, {, 1,-1 }}; private Static int total; public static void main (string ar GS []) {int start = 0; int end = 0; maze [start] [end] = 3; // indicates that this path has passed fun (START, end ); system. out. println (total);} public static void fun (INT P1, int P2) {int PX; int py; // start searching for four directions for (INT I = 0; I <4; I ++) {If (check (P1, P2, I) {// check whether the four directions of the car can go PX = p1 + F [0] [I]; py = P2 + F [1] [I]; maze [PX] [py] = 3; // indicates that this path has passed if (PX = maze [0]. length-1) & (Py = maze. length-1) Out (); else fun (PX, Py) ;}} maze [P1] [P2] = 2; // dead path} // output public static void out () {for (INT I = 0; I <maze. length; I ++) {for (Int J = 0; j <maze [I]. length; j ++) {If (maze [I] [J] = 3) {system. out. print ("X"); Total ++;} else system. out. print ("*");} system. out. println () ;}// check whether the four directions of the vehicle are public static Boolean check (INT P1, int P2, int K) {p1 + = f [0] [k]; p2 + = f [1] [k]; If (P1 <0) | (P1> maze [0]. length-1) | (P2 <0) | (P2> maze. length-1) | (maze [P1] [P2]! = 0) return false; return true ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.