nyvj-minimum number of steps

Source: Internet
Author: User

minimum number of steps Time limit:3000ms Memory limit:128000kb 64-bit integer IO Format:java class Name:submit Status

This has a maze, with 0~8 rows and 0~8 columns:

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

0 represents the road, and 1 represents the wall.

Now enter the coordinates of a road as a starting point, and then, as the end point of entering a road's coordinates, ask at least a few steps to get to the end from the starting point.

(Note: One step is to move from one sitting punctuation to the next to the left and right adjacent coordinate points, such as: from (3,1) to (4,1). Input The first line to enter an integer n (0<n<=100), indicating that there are n sets of test data;
Then n rows, each row has four integers a,b,c,d (0<=a,b,c,d<=8) representing the row, column, and end line of the starting point, respectively. output outputs are at least a few steps away. Sample Input

2
3 1  5 7
3 1  6 7
Sample Output
11

The Submit Status idea is simple

But there are many mistakes.

First time WA code

#include <stdio.h> int a,b,c,d,step,min=999999;
int dir[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
                  int mg[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,
1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,};
    void Dfs (int x,int y,int z) {//printf ("*\n");
        if (X==c&&y==d) {if (z<min) {min=z;
    } return;
        } for (int i=0; i<4; i++) {int dx=x+dir[i][0];
        int dy=y+dir[i][1]; if (dx>=0&&dy>=0&&dx<=8&&dy<=8&&mg[dx][dy]==0) {//printf ("
            %d%d*\n ", dx,dy);
            Mg[dx][dy]=1;
            DFS (DX,DY,Z+1);
            printf ("%d%d$\n", dx,dy);
        mg[dx][dy]=0; }}} int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%d%d%d%d", &a,&b,&c,&d);
        DFS (a,b,0);
    printf ("%d\n", min);
 }
}

WA had one time because forgot to reset for min

Plus min=9999, it's a.

#include <stdio.h> int a,b,c,d,min=999999;
int dir[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
                  int mg[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,
1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,};
    void Dfs (int x,int y,int z) {//printf ("*\n");
        if (X==c&&y==d) {if (z<min) {min=z;
    } return;
        } for (int i=0; i<4; i++) {int dx=x+dir[i][0];
        int dy=y+dir[i][1]; if (dx>=0&&dy>=0&&dx<=8&&dy<=8&&mg[dx][dy]==0) {//printf ("
            %d%d*\n ", dx,dy);
            Mg[dx][dy]=1;
            DFS (DX,DY,Z+1);
            printf ("%d%d$\n", dx,dy);
        mg[dx][dy]=0;
 }}} int main () {   int n;
    scanf ("%d", &n);
        while (n--) {min=99999;
        scanf ("%d%d%d%d", &a,&b,&c,&d);
        DFS (a,b,0);
    printf ("%d\n", min);
} 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.