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 ;}