Codeforces 253c广搜

题目

C. Text Editor

time limit per test

1 second

memory limit per test

256 megabytes

input

input.txt

output

output.txt

Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none — in writing a composition.

As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?

Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains ai characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).

Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key:

  • "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1;
  • "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1;
  • "Right": if the cursor can move to the right in this line (c < ar + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key;
  • "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key.

You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r1, c1) to position (r2, c2).

Input

The first line of the input contains an integer n (1 ≤ n ≤ 100) — the number of lines in the file. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 105), separated by single spaces. The third line contains four integers r1, c1, r2, c2(1 ≤ r1, r2 ≤ n, 1 ≤ c1 ≤ ar1 + 1, 1 ≤ c2 ≤ ar2 + 1).

Output

Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (r1, c1) to position (r2, c2).

Examples

input

Copy

4
2 1 6 4
3 4 4 2

output

Copy

3

input

Copy

4
10 5 6 4
1 11 4 2

output

Copy

6

input

Copy

3
10 1 10
1 10 1 1

output

Copy

3

Note

In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.

123

12

123s567

1t345

One of the possible answers in the given sample is: "Left", "Down", "Left".

这个题目相较于真实的光标移动情况还是比较简单的。在广搜的时候只需要用四个判断是否能够移动在判断那个点是否被经过,很标准的广搜板子题。

#include <iostream>
#include <cmath>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
typedef  long long ll;
using namespace std;
struct ha
{
    int lin,row,dtep;
};
int x0,y0,n;
queue<ha>q;
bool fla[110][100009];
int a[1000];
int  bfs()
{
    while(q.size())
    {
        ha o=q.front(),jk;
        q.pop();

        int x=o.row,y=o.lin,z=o.dtep;
        jk.dtep=z+1;
        if(x==x0&&y==y0)
            return z;
        if(x!=1)
        {
            jk.row=x-1;
            jk.lin=min(y,a[x-1]);
            if(!fla[jk.row][jk.lin])
            {
                fla[jk.row][jk.lin]=true;
                q.push(jk);

            }
        }
        if(x!=n)
        {
            jk.row=x+1;
            jk.lin=min(y,a[x+1]);
            if(!fla[jk.row][jk.lin])
            {
                fla[jk.row][jk.lin]=true;
                q.push(jk);

            }
        }
        if(y!=0)
        {
            jk.row=x;
            jk.lin=y-1;
            if(!fla[jk.row][jk.lin])
            {
                fla[jk.row][jk.lin]=true;
                q.push(jk);

            }
        }
        if(y!=a[x])
        {
            jk.row=x;
            jk.lin=y+1;
            if(!fla[jk.row][jk.lin])
            {
                fla[jk.row][jk.lin]=true;
                q.push(jk);

            }
        }
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        a[i]++;
    }
    memset(fla,false,sizeof(fla));
    int an,bn;
    cin>>an>>bn>>x0>>y0;
    ha io;
    io.row=an,io.lin=bn;
    io.dtep=0;
    while(q.size())q.pop();
    q.push(io);
    fla[io.row][io.lin]=true;
    cout<<bfs()<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值