A job week2

Meaning of the questions:
 stuff have a map to find sister paper the map. Map display, 0 can go, can not go 1 represents the upper left corner is the entrance to the lower right corner is the sister paper, these two positions to ensure zero. Now that we know the map, then stuff it is not difficult to find the sister paper, you compile a program, write stuff to find the shortest route sister paper.
Input Input is a 5 × 5 two-dimensional array, just the two numbers 0,1, representation positions in FIG. Output: a plurality of output lines, represents the shortest path from upper left to lower right corner coordinates sequentially passes, formatted as shown in the sample. Ensure data has a unique solution.
The Sample INPUT
0. 1 0 0 0
0. 1 0. 1 0
0. 1 0. 1 0
0 0 0. 1 0
0. 1 0. 1 0
the Sample Output
(0, 0)
(. 1, 0)
(2, 0)
(. 3, 0)
( 3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
( . 4,. 4)
Hint coordinates (x, y) represents the x-y line column, row, column numbering starts from 0, and at the upper left corner as the origin.  
Further note that, the coordinates of the comma-separated output should be a space.
Ideas:
Definition of a structure, which defines two coordinates x, y. When the vertical and horizontal travels further judge, defined x, y by bfs function, changes coordinates, the definition of a recording mark array through the row, while walking coordinates are unlabeled and not out of the boundary wall and not, this marker coordinates, and the push point determination operation is continued until the destination come. , Are sequentially output by the output stack acts shortest way point coordinate storage.
Code:
#include
#include
#include
#include
#include
the using namespace STD;
int A [. 6] [. 6];
int disx [] = {. 1, -1,0,0};
int DISY [] = {0,0 ,. 1, -1};
struct the Node
{
int X, Y;
};
the Node pre [. 6] [. 6];
void BFS ()
{
int VIS [. 6] [. 6] = {0};
the Node Node;
node.x 0 =; node.y = 0;
Queue Q;
q.push (Node);
VIS [0] [0] =. 1;
the while (! q.empty ())
{
the Node q.front = now ();
Q. pop ();
if(now.x4 && now.y4)
{
return;
}
for(int i=0;i<4;i++)
{
int newx=now.x+disx[i];
int newy=now.y+disy[i];
node.x=newx;node.y=newy;
if(newx>=0 && newx<5 && newy>=0 && newy<5 && !vis[newx][newy] && !a[newx][newy])
{
vis[newx][newy]=1;
pre[newx][newy]=now;
q.push(node);
}
}
}
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
bfs();
stack s;
Node node;
node.x=4;node.y=4;
while(node.x!=0 || node.y!=0)
{
s.push(node);
node = pre [node.x] [node.y
}
s.push(Node{0,0});
while(!s.empty())
{
node=s.top();
s.pop();
printf("(%d, %d)\n",node.x,node.y);

}
return 0;

}

Published 19 original articles · won praise 0 · Views 222

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104738401