问题 C: 远离原点

题目描述
有一个二维网格图,牛牛一开始站在原点(0,0),每一秒他可以往上下左右的某个方向移动一步,或者停留在原地
网格中有一些点是不能走的
现在问你k秒内往x方向最远能走多远,即求k秒内经过的点的最大的x坐标

输入
第一行输入两个整数n,k(0 <= n <= 47, 1 <= k <= 1000)
第二行输入n个整数x[i],表示禁走的点的x坐标 (-1000 <= x[i] <= 1000)
第二行输入n个整数y[i],表示禁走的点的y坐标 (-1000 <= y[i] <= 1000)

输出
输出一个整数,表示k秒内经过的点的最大的x坐标

样例输入
【样例输入1】
4 4
1 1 1 1
-2 -1 0 1
【样例输入2】
4 9
-1 0 0 1
0 -1 1 0
【样例输入3】
11 47
1 0 0 -1 -1 -2 -2 -3 -3 -4 -4
0 -1 1 -2 2 -3 3 -4 4 -5 5

样例输出
【样例输出1】
2
【样例输出2】
0
【样例输出3】
31

题意:每秒走一格,问你在k秒内,不经过禁走的点,求所走的坐标中最大的x坐标.
bfs搜索,,坐标要处理一下,加1000.在k秒时或者没路可走时退出.

#include <bits/stdc++.h>
using namespace std;
const int N=100;
typedef long long ll;
struct p{
    ll l,r;
}a[N];
typedef long long ll;
bool comp(p a,p b){
    return a.l<b.l;
}
int main(){
     ios::sync_with_stdio(false);
     int n;
     cin>>n;
     for(int i=1;i<=n;i++) cin>>a[i].l;
     for(int i=1;i<=n;i++){
        cin>>a[i].r;
        a[i].r+=a[i].l;
     }
     sort(a+1,a+1+n,comp);
     int end;
     ll ans=0;
     ll minn=1e18;
     for(int i=1;i<=n;i++){
        end=a[i].l;
        ans=0;
        for(int j=i-1;j>=1;j--){
            ans+=end-a[j].r;
            end-=a[j].r-a[j].l; 
         }
         end=a[i].r;
         for(int j=i+1;j<=n;j++){
            ans+=a[j].l-end;
            end+=a[j].r-a[j].l;
         }
         if(ans<minn) minn=ans;
     }
     cout<<minn<<endl;
      
     //cout<<"asf"<<endl;
     return 0;
}
原文地址:https://www.cnblogs.com/-yjun/p/10586455.html