current position:Home>Question e: snake walking, please help me

Question e: snake walking, please help me

2022-02-02 18:54:08 CSDN Q & A

The phenomenon and background of the problem

Xiao Sun and Xiao Wang are playing a snake walking game : In a n That's ok m In the matrix of columns , From the top left corner (0,0) Start , Snake walking to the lower right corner is (n-1,m-1).
Xiao Sun asked Xiao Wang : If I stand now (x, y) position , Guess how many steps I took ?
Xiao Wang thought for a moment and had the answer , Have you been able to calculate the result quickly ?
As shown in the figure below , On the left is 3x4 The number of steps from the matrix to each point , For ease of understanding ,(0,0) from 1 Count up . The picture on the right is 4x3 Examples of matrices :

img

Input
The input contains multiple sets of data , One line for each group of data .
Enter four integers per line n,m,x,y(n <= 2* 104, m <= 2*104, 0<= x < n, 0 <= y < m), The number of rows of the matrix 、 The number of columns in a matrix 、 The row coordinates of the query point 、 Column coordinates of query points .

Output
Output one row per group of data , It means how many steps Xiao Sun has taken to reach (x,y) This position .

The sample input Copy
3 4 0 2
4 3 2 1
Sample output Copy
6
8

Today I did a topic , It's a little difficult , But I wrote it for a long time , Tested, almost passed , At school oj Hand it in , good heavens , Running error

#include<stdio.h>int a[21000][21000];int main(void){    while(1)    {        int m,n,x,y,t = 1,o = 0,p = 0;        scanf("%d %d %d %d",&m,&n,&x,&y);        //o , m Said line ,p,n The column         a[o][p] = t;        while(1)        {            if(n > p + 1) a[o][++p] = ++t;            else            {                a[++o][p] = ++t;            }            if(o == m - 1 && p == n - 1)                break;            while(o < m - 1 && p > 0)            {                a[++o][--p] = ++t;            }            if(o == m - 1)                a[o][++p] = ++t;            else                a[++o][p] = ++t;            if(o == m - 1 && p == n - 1)                break;            while(o > 0 && p < n - 1)                a[--o][++p] = ++t;        }        printf("%d",a[x][y]);    }    return 0;}
Operation results and error reporting contents

img

My solution ideas and tried methods

Try to make the array a little bigger But it doesn't work

What I want to achieve



Refer to the answer 1:



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021854056478.html

Random recommended