In the previous article "using C # to develop smartphone software: Push box (d)", I introduced the Common/findpath.cs source program file. In this article, we introduce the improved Common/findpath.cs source program files. In other words, the idea of "using C # to develop smartphone software: Push Box (iv)" was implemented in the second comment, and the map ushort[,] Maps was changed to byte[,] map. The following are the improved Findpath classes:
The following is a reference fragment:
1 using System;
2 using System.Drawing;
3 using System.Collections.Generic;
4
5 Namespace Skyiv.Ben.PushBox.Common
6 {
7///
8///to find the shortest route
9///
Static Class Findpath
11 {
static size[] offsets = {New Size (0, 1), New size (1, 0), new size (0,-1), New size (-1, 0)};
static direction[] Directions = {Direction.south, direction.east, Direction.north, direction.west};
14
15///
16///to find the shortest route
17///
18///Map
19///starting point
20///Destinations
21///Shortest Route
public static Queue Seek (byte[,] map, point from, point to)
23 {
Queue movequeue = new Queue (); Route
int value; A quantity related to the distance from the destination, change rule: => 2 => 1 => 3 => 2 => 1 => 3 => 2 => 1
(map, to, out value)//found a route
27 {
here = from; Starting point (i.e. the position of the worker)
-NBR = new Point (); Four-week neighbor
.
for (value = (value + 1)% 3 + 1; this is!= to; value = (value + 1)% 3 + 1)//step towards destination
31 {
for (int i = 0; i < offsets. Length; i++)
33 {
NBR = Fcl.add (here, offsets[i]); Start looking around for neighbors
(Block.value) (MAP[NBR. Y, NBR. X] = = value)//Just go in this direction
36 {
Notoginseng Movequeue.enqueue (Directions[i]); The route extends to the destination
break;
39}
40}
here = NBR; Keep moving,
.
42}
43}
Block.cleanallmark (map); Clear all signs, restore the scene
return movequeue; The route being sought, if the destination is not reached, the length of the route is zero
46}
47
48///
49///to find the shortest route, use breadth First search
50///
51///Map
52///Destinations
53///Output: The value marked when the search completes
54///is successful
static bool Seek (byte[,] map, point to, out int value)
56 {
Queue q = new Queue ();
Block.mark (ref map[to. Y, to. X], 1); Start looking back from your destination and mark your destination as 1
point NBR = Point.empty; Four-week neighbor
.
for (;;)
61 {
value = Block.value (map[to. Y, to. X])% 3 + 1; A quantity associated with a distance from the destination, used as a marker, a variation of the law:
for (int i = 0; i < offsets. Length; i++)//1 => 2 => 3 => 1 => 2 => 3 => 1 => 2 => 3 =>
64 {
NBR = Fcl.add (To, offsets[i]); Start looking around for neighbors
Block.isman if (MAP[NBR. Y, NBR. X])) break; Arrive at the starting point (i.e. the position of the worker)
-Block.isblank (MAP[NBR. Y, NBR. X])///can go the road
68 {
Block.mark (ref MAP[NBR. Y, NBR. X], value); Mark, prevent the road from coming again
Q.enqueue (NBR); Join the queue, wait to continue looking for
71}
72}
-Block.isman (MAP[NBR. Y, NBR. X])) break; Reach the starting point
if (q.count = 0) return false; Unable to reach the starting point
to = Q.dequeue (); Out team, keep looking, this is breadth first search, because the front has been able to walk around all the way to join the queue.
76}
return true; Find a route
78}
79}
80}