Source
[Miao Dongdong] original
I haven't searched for any questions for a long time. I feel that my hands are a little new. I can use this question as a trainer and a water question. I can directly use DFS to access the water, or I can use BFs to do it, I am still very familiar with BFs. The idea of this question is still very clear. I marked it as soon as I walked through, but the count was not well controlled at the beginning, resulting in no result. In addition, the DFS was written recursively, be sure to control the end of recursion. Otherwise, you will not know where the error is. You should review what you have learned before;
Below is the code of water pass;
# Include <cstdio> # include <cstring> # define min (x, y) x> Y? Y: X // refer to other users' using namespace STD; int dir [4] [2] = {, 0,-, 0 }; int map [9] [9] = {, 1}, {, 1,, 0, 1}, {, 1}, {, 1}, {, 0, 1}, {, 0, 1}, {, 1, 1 },}; int C, D, M; void DFS (int A, int B, int ans) {int X, Y; if (A = C & B = d) {M = min (M, ANS); return; // return, recursively ends, returns the previous layer ;} map [a] [B] = 1; // mark the path passed by for (INT I = 0; I <4; I ++) {x = a + dir [I] [0]; y = B + dir [I] [1]; If (Map [x] [Y] = 0) // determine the path {DFS (X, Y, ANS + 1); // search map [x] [Y] = 0 ;}} int main () again () {int n, a, B; scanf ("% d", & N); While (n --) {M = 9999; // compare a large number with the count. scanf ("% d", & A, & B, & C, & D); DFS (a, B, 0); map [a] [B] = 0; printf ("% d \ n", m);} return 0 ;}