Nyist OJ 58 least steps (DFS search)

Source: Internet
Author: User
Minimum steps time limit: 3000 MS | memory limit: 65535 kb difficulty: 4
Description

There is a maze, with 0 ~ 8 rows and 0 ~ 8 columns:

1, 1, 1, 1, 1, 1
, 1
, 1
, 1
, 1
, 0, 1
, 0, 1
, 0, 0, 1
1, 1, 1, 1, 1, 1

0 indicates the road, and 1 indicates the wall.

Now, enter the coordinates of a road as the start point, and then enter the coordinates of a road as the end point. Can you take at least a few steps to reach the end point from the start point?

(Note: one step refers to a coordinate point from the coordinate point to its upper and lower left adjacent coordinate points, for example, from (3, 1) to (4, 1 ).)

Input
Enter an integer N (0 <n <= 100) In the first line, indicating that N groups of test data exist;
Next n rows, each row has four integers, a, B, c, d (0 <= A, B, C, D <= 8), representing the row and column of the start point respectively, the row and column at the end.
Output
The output takes at least a few steps.
Sample Input
23 1  5 73 1  6 7
Sample output
1211
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 ;}


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.