/* ***********************************************
Author :PeterBishop
Created Time :2019年04月09日 星期二 19时13分09秒
File Name :t.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
using namespace std;
#define MAX 0x3f3f3f3f
struct Node{
int x[2], h;
};
Node node[1100];
int N, X, Y, MAXh;
int dp[1100][2];
int cmp( const void *a, const void *b ){
if( ((Node*)a)->h != ((Node*)b)->h ){
return ((Node*)b)->h - ((Node*)a)->h;
}else{
return ((Node*)a)->x[0] - ((Node*)b)->x[0];
}
}
int main(){
int T;
freopen("in.txt","r",stdin);
scanf( "%d", &T );
while( T-- ){
scanf( "%d%d%d%d", &N, &X, &Y, &MAXh );
for( int i = 1; i <= N; i++ ){
scanf( "%d%d%d", &node[i].x[0], &node[i].x[1], &node[i].h );
}
node[0].x[0] = X;
node[0].x[1] = X;
node[0].h = Y;
node[N+1].x[0] = -20000;
node[N+1].x[1] = 20000;
node[N+1].h = 0;
qsort( &node[1], N, sizeof( Node ), cmp );
memset( dp, 0x3f, sizeof( dp ) );
dp[0][0] = 0;
dp[0][1] = 0;
for( int i = 0; i <= N; i++ ){
for( int j = 0; j < 2; j++ ){
if( dp[i][j] != MAX ){
int temp = -1;
for( int k = i + 1; k <= N + 1; k++ ){
if( abs( node[k].h - node[i].h ) > MAXh ){
temp = -1;
break;
}
if( node[k].x[0] <= node[i].x[j] && node[k].x[1] >= node[i].x[j] ){
temp = k;
break;
}
}
if( temp == -1 ){
continue;
}
if( temp != N + 1 ){
dp[temp][0] = min( dp[temp][0], dp[i][j] + abs( node[temp].x[0] - node[i].x[j] ) + abs( node[temp].h - node[i].h ) );
dp[temp][1] = min( dp[temp][1], dp[i][j] + abs( node[temp].x[1] - node[i].x[j] ) + abs( node[temp].h - node[i].h ) );
}else{
dp[temp][0] = min( dp[temp][0], dp[i][j] + abs( node[temp].h - node[i].h ) );
}
}
}
}
cout << dp[N+1][0] << endl;
}
return 0;
}
1661 下台阶(左右方向细节有点烦)
原创PeterBishop ©著作权
©著作权归作者所有:来自51CTO博客作者PeterBishop的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Spring的自动注入小细节
Spring的自动注入小细节
spring 自动注入 byName byType -
函数递归经典题目——汉诺塔,青蛙跳台阶
递归常见例题——斐波那契、汉诺塔、青蛙跳台阶
递归 n的阶乘 i++ -
ElementUI的el-table表头自动吸顶,支持左右固定列
在使用el-table的时候,在数据多的情况下滚动表格会看不到表头是什么,为了用户体验可以使用表头吸顶来展示,如此丝滑的操作谁见了都说好。
Element el-table vue 表头吸顶 -
渤海银行有点火,李伏安有点烦
突然被“拉黑”,意味着什么?
风控 数据质量 二级 -
vscode编写代码时怎么不使用左右方向箭头来移动光标
vscode编写代码时怎么不使用左右方向箭头来移动光标
vscode 编写代码 移动光标 快捷键 方向键 -
C语言之控制台读取上下左右方向键指令
首先,可以检测任何按键键值// 首先,检测任何按键的代码 #include<stdio.h>#include<conio.h>i...
c语言 开发语言 #include 键值 方向键