Write Clp(R) Rules to Find Paths Through a Maze Prolog
Maze problem (shortest circuit + record path)
Define 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 maze, one represents the wall, 0 means that the road can be leaving, can only walk or vertically, can not be oblique, ask the program to find the shortest route from the upper left corner to the lower right corner.
Input
A 5 × 5 two-dimensional array represents a maze. Data guarantees have unique solutions.
Output
The shortest path in the upper left to the lower right corner, the format is shown in the format.
Sample Input
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
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
Searching for a path to add an array or two arrays to save the path
code show as below
#include <bits/stdc++.h> using namespace std; int to[ 10 ] [ 2 ] = { 0 , 1 , 1 , 0 , 0 , - 1 , - 1 , 0 } ; int a[ 10 ] [ 10 ] ,book[ 10 ] [ 10 ] ; struct z{ int x,y,l,t1[ 110 ] ,t2[ 110 ] ; // T1 and T2 arrays to record path }u,v; queue<z> q; int main ( ) { int i,j,tx,ty; for (i= 0 ;i< 5 ;i++ ) for (j= 0 ;j< 5 ;j++ ) scanf ( "%d" , &a[i] [j] ) ; memset (book, 0 , sizeof (book) ) ; u.x= 0 ; u.y= 0 ; u.l= 0 ; u.t1[ 0 ] = 0 ; u.t2[ 0 ] = 0 ; book[ 0 ] [ 0 ] = 0 ; q. push (u) ; while ( !q. empty ( ) ) { u=q. front ( ) ; q. pop ( ) ; if (u.x== 4 &&u.y== 4 ) { for (i= 0 ;i<u.l;i++ ) printf ( "(%d, %d)\n" ,u.t1[i] ,u.t2[i] ) ; printf ( "(4, 4)\n" ) ; } v=u; for (i= 0 ;i< 4 ;i++ ) { tx=u.x+to[i] [ 0 ] ; ty=u.y+to[i] [ 1 ] ; if (tx>= 0 &&tx< 5 &&ty>= 0 &&ty< 5 && !a[tx] [ty] && !book[tx] [ty] ) { book[tx] [ty] = 1 ; v.x=tx; v.y=ty; v.l=u.l+ 1 ; v.t1[v.l] =tx; v.t2[v.l] =ty; q. push (v) ; } } } return 0 ; }
Intelligent Recommendation
Maze_A (maze shortest path problem)
Title Dongdong has a map and wants to find sister paper through the map. The map shows that 0 means you can walk, 1 means you cannot walk, the upper left corner is the entrance, and the lower right co...
bfs shortest path maze problem
Using bfs to find the shortest path can be understood as enumerating all the possibilities of each step until the conditions are found, and the first path found using bfs must be the shortest. The pri...
BFS shortest path-maze problem
http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5...
18720 Maze problem (shortest path)
Title description Description The maze is an n*m matrix. Players need to start at the entrance of the maze (coordinates 1, 1), find a path to the exit (n, m). Please judge whether the player can get o...
More Recommendation
Maze problem (shortest path) ---- BFS
The maze is a matrix of n * m, and the player needs the labyrinth entrance (coordinates 1, 1), looking for the path to the exit (N, M). Please determine if the player will come out from the maze, if y...
Maze problem (shortest path) (BFS)
Maze problem (BFS version) Topic description Give a maze of N × N. The labyrinth is composed of channels and walls, and each step can be moved to the adjacent upper and lower left and right chan...
Maze problem (output shortest path)
Original link Ideological analysis: The output shortest path only needs to be used to mark which direction from which direction is changed....
Maze shortest path (bfs + path record)
Define a two-dimensional array: int maze[5][5] = { }; It represents a labyrinth, where 1 is the wall and 0 is the path that can be walked. It can only be moved sideways or vertically, not diagonally. ...
Copyright DMCA © 2018-2021 - All Rights Reserved - www.programmersought.com User Notice
Write Clp(R) Rules to Find Paths Through a Maze Prolog
Source: https://www.programmersought.com/article/116310019614/
0 Response to "Write Clp(R) Rules to Find Paths Through a Maze Prolog"
Post a Comment