Hangzhou Electric acm1240--asteroids!~~ Simple BFS

Source: Internet
Author: User

This topic, three-dimensional space on the BFS, give you the starting point and end point, see if you can find a way, O said to go, x means you can not go! ~

Understand the problem, you can use the queue to implement BFS to solve.

The following is the code for the AC:

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; Class Data{public:int xyz;int count;};     Char map[11][11][11];bool flag[11][11][11];char str[10];int xyz[6][3] = {0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};                   Three-dimensional six directions int n;int SX, SY, SZ; Start int ex, EY, Ez;<span style= "White-space:pre" ></span>//Endpoint int BFS ()//bfs{queue&lt ;d Ata>q;while (! Q.empty ()) Q.pop (); if (sx = = Ex && sy = = ey && Sz = ez && Map[sx][sy][sz] = = ' O ' && Map[ex] [EY]  [EZ] = = ' O ') return 0;data temp, te;int x, Y, z, temp1;temp.xyz = SX * + sy * + sz;temp.count = 0;flag[sx][sy][sz] = True Q.push (temp); while (! Q.empty ()) {temp = Q.front (); Q.pop (); for (int i = 0; i < 6; i++) {Temp1 = Temp.xyz;x = temp1/100 + xyz[i][0];temp1%= 100;y = TEMP1/10 + xyz[i][1] ; Temp1%= 10;z = Temp1 + xyz[i][2];if (x = = Ex && y = = ey && z = EZ) return Temp.count + 1;if (x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n && map[x] [Y] [Z] = ' x ' &&!flag[x][y][z]) {te.xyz = X * + y * + z;te.count = temp.count + 1; Q.push (TE); flag[x][y][z] = true;}}} return-1;} int main () {//freopen ("Data.txt", "R", stdin); int I, J, K;while (scanf ("%s%d", str, &n)! = EOF)//input processing! More trouble {GetChar (); for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {for (k = 0; k < n; k++) scanf ("%c", &map[i][j][k]) ; GetChar ();}} scanf ("%d%d%d", &sx, &sy, &sz); scanf ("%d%d%d", &ex, &ey, &ez); GetChar (); scanf ("%s", str); GetChar (); memset (flag, false, sizeof (flag)), int ans = BFS (), if (ans < 0) printf ("NO route\n"); elseprintf ("%d%d\n", N, a NS);} return 0;}


Hangzhou Electric acm1240--asteroids!~~ Simple BFS

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.