Gym 102318D - Editor Navigation BFS

题目链接:https://nanti.jisuanke.com/t/44820
题意:一段文章有好几行 让你把某个地方的光标用上下左右移动到指定位置 问你怎样移动次数最少
思路:BFS即可。

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
int n,a,b,c,d;
int cnt[150];
int dir[4][2]={-1,0,1,0,0,-1,0,1};//上下左右
bool con[150][100];
struct node{
    int x,y,dep;
    bool operator<(const node &oth)const{
        return dep>oth.dep;
    }
};
void bfs(int a,int b){
    cl(con,false);
    priority_queue<node> q;
    node tp;
    tp.x=a,tp.y=b;
    tp.dep=0;
    q.push(tp);
    while(!q.empty()){
        tp=q.top();
        q.pop();
        if(tp.x==c&&tp.y==d){
            printf("%d\n",tp.dep);
            return;
        }
        rep(i,0,3){
            node tp1;
            tp1.x=tp.x+dir[i][0];
            tp1.y=tp.y+dir[i][1];
            tp1.dep=tp.dep+1;
            if(tp1.x<1||tp1.x>n) continue;//越界不行
            if(tp1.x==1&&tp1.y<0) continue;
            if(tp1.x==n&&tp1.y>cnt[n]) continue;
            //若没越界
            if(i==0||i==1){//往上下移动
                if(tp1.y>cnt[tp1.x]){
                    tp1.y=cnt[tp1.x];
                }
                if(!con[tp1.x][tp1.y]){
                    con[tp1.x][tp1.y]=true;
                    q.push(tp1);
                }
            }else{//往左或者往右
                if(tp1.y<0){
                    if(!con[tp1.x-1][cnt[tp1.x-1]]){
                        con[tp1.x-1][cnt[tp1.x-1]]=true;
                        tp1.x--;
                        tp1.y=cnt[tp1.x];
                        q.push(tp1);
                    }
                }else if(tp1.y>cnt[tp1.x]){
                    if(!con[tp1.x+1][0]){
                        con[tp1.x+1][0]=true;
                        tp1.x++;
                        tp1.y=0;
                        q.push(tp1);
                    }
                }else{
                    if(!con[tp1.x][tp1.y]){
                        con[tp1.x][tp1.y]=true;
                        q.push(tp1);
                    }
                }
            }
        }
    }  
}
int main() {
	int t;
    sd(t);
    while(t--){
        sd(n);
        rep(i,1,n){
            sd(cnt[i]);
        }
        sdd(a,b);sdd(c,d);
        bfs(a,b);
    }
	return 0;
}

You are using a very simple text editor to create a document. You have typed in several lines, with the flashing cursor advancing as you type, but then you see a mistake on a previous line. Unfortunately, the mouse doesn’t work! You will have to press arrow keys to move the cursor back to the position where you can fix the mistake. Of course, you want to get to this position as quickly as possible.

企业微信截图_15859732067369.png

This simple editor uses a monospace font, so each character is exactly the same width. The cursor can be at the beginning of the line (before the first character), end of the line (after the last character), or at a horizontal position between two characters on a given line. The following keys can be pressed to move the cursor (each keypress is independent of any preceding keypress):

企业微信截图_15859733617651.png

企业微信截图_15859733782760.png

The Problem:

Given the line lengths of a text file that was loaded in this simple editor, along with the current cursor position and a different, desired cursor position (e.g., to fix a mistake), you are to determine the minimum number of keypresses, using arrow keys only, required to move the cursor to the desired position.

The Input:

The first input line contains a positive integer, n, indicating the number of editor navigation scenarios to process. Each scenario will occupy exactly 4 input lines. The first line of each scenario contains an integer f (1 ≤ f ≤ 120), indicating the number of lines of text in the file that is loaded in the editor. The next input line contains f integers, s1 to sf, where each value si (0 ≤ si ≤ 80) indicates the number of characters on line i of the file; the values will be separated by exactly one space. A value si = 0 means that there are no characters on line i. The newline character (common character indicating end of a line) does not count as a character on the line. The third input line of each scenario will contain two integers (separated by a space) providing the current cursor position in the file: rc (1 ≤ rc ≤ f) and cc (0 ≤ cc ≤ 80), where rc represents the line of the file, counting from 1 (as with i), and cc represents the horizontal position of the cursor on that line, 0 for the beginning (before the first character). It is guaranteed that the cursor position is valid, so if, for instance, rc = 17 and s17 = 56, then 0 ≤ cc ≤ 56; the maximum value of cc is the end of the line. The fourth input line of each scenario is similar to the third and indicates the desired cursor position to begin fixing the mistake, i.e., this line consists of two integers separated by a space, rm (1 ≤ rm ≤ f) and cm (0 ≤ cm ≤ 80). The constraints on the values for rc and cc also apply to rm and cm.

The Output:

For each scenario in the input, output a single integer on a line by itself indicating the minimum number of keypresses needed to move the cursor from its current position (rc, cc) to the desired position (rm, cm) for fixing the mistake.
样例输入

2
7
39 20 57 54 14 38 31
7 31
3 39
3
15 30 20
1 12
3 3

样例输出

21
8

样例解释

For Case #1, one possible sequence for the minimum number of keypresses to move the cursor from its current position to the desired position is: Up, Up, Right, Up, Left, Up, Left 15 times.

发布了120 篇原创文章 · 获赞 12 · 访问量 5246

猜你喜欢

转载自blog.csdn.net/weixin_43735161/article/details/105412335