Poj 3984 Maze problem bfs

Source: Internet
Author: User

After learning this question, I learned a lot about it. First, I learned a lot about it, especially for beginners like me. I can learn some tips.


Then, we can learn different ideas and ideas from others' codes.

 

 

This is to find the shortest path. The first thought is to use bfs, but after finding the shortest, I don't know how to output it? I'm in a hurry. Why ???

 


The basic broad search questions have already been well developed, but the record path is still not fully understood, and the general direction is still clear, record, and recursive output ....

 


Then I found the well-written code .. Then I added the "localization" process ~~~~~~~ ·

# Include <iostream> # include <stdio. h> # include <string. h> using namespace std; int map [5] [5], vis [5] [5], pre [100]; struct loca {int x; int y ;} list [50]; int dir [8] = {, 0,-, 0,-}; int judge (int x, int y) {if (x> = 1 & x <5 & y> = 0 & y <5 &&! Map [x] [y]) return 1; return 0;} void print (int x) // recursive output. Now, cainiao is learning... {Int t; t = pre [x]; if (t = 0) {printf ("(0, 0) \ n"); printf ("(% d, % d) \ n ", list [x]. x, list [x]. y); return;} print (t); printf ("(% d, % d) \ n", list [x]. x, list [x]. y);} void bfs () {int I, head, tail; int x, y, xx, yy; memset (vis, 0, sizeof (vis )); head = 0; tail = 1; list [0]. x = 0; list [0]. y = 0; pre [0] =-1; while (head <tail) {x = list [head]. x; y = list [head]. y; if (x = 4 & y = 4) {print (head); return;} (I = 0; I <8; I + = 2) {xx = x + dir [I]; yy = y + dir [I + 1]; if (! Vis [xx] [yy] & judge (xx, yy) {vis [xx] [yy] = 1; list [tail]. x = xx; list [tail]. y = yy; pre [tail] = head; tail ++;} head ++;} return ;}int main (void) {int I, j; for (I = 0; I <5; I ++) for (j = 0; j <5; j ++) scanf ("% d ", & map [I] [j]); bfs (); return 0 ;}# include <iostream >#include <stdio. h> # include <string. h> using namespace std; int map [5] [5], vis [5] [5], pre [100]; struct loca {int x; int y ;} list [50]; int dir [8] = {0, 1, 0, -, 0,-}; int judge (int x, int y) {if (x> = 1 & x <5 & y> = 0 & y <5 &&! Map [x] [y]) return 1; return 0;} void print (int x) // recursive output. Now, cainiao is learning... {Int t; t = pre [x]; if (t = 0) {printf ("(0, 0) \ n"); printf ("(% d, % d) \ n ", list [x]. x, list [x]. y); return;} print (t); printf ("(% d, % d) \ n", list [x]. x, list [x]. y);} void bfs () {int I, head, tail; int x, y, xx, yy; memset (vis, 0, sizeof (vis )); head = 0; tail = 1; list [0]. x = 0; list [0]. y = 0; pre [0] =-1; while (head <tail) {x = list [head]. x; y = list [head]. y; if (x = 4 & y = 4) {print (head); return ;}for (I = 0; I <8; I ++ = 2) {xx = x + d Ir [I]; yy = y + dir [I + 1]; if (! Vis [xx] [yy] & judge (xx, yy) {vis [xx] [yy] = 1; list [tail]. x = xx; list [tail]. y = yy; pre [tail] = head; tail ++;} head ++;} return ;}int main (void) {int I, j; for (I = 0; I <5; I ++) for (j = 0; j <5; j ++) scanf ("% d ", & map [I] [j]); bfs (); return 0 ;}

 

Related Article

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.