Codeforces Round #676 (Div. 2) D. Hexagons(思维)

本文介绍了一道编程题目,涉及在二维六边形网格中寻找从起点(0,0)到给定坐标(x,y)的最短路径成本,通过预处理方向成本并根据目标区域调整策略,提供了解决算法和代码实例。

题目链接:https://codeforc.es/contest/1421/problem/D

Lindsey Buckingham told Stevie Nicks “Go your own way”. Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.

Consider a hexagonal tiling of the plane as on the picture below.
在这里插入图片描述

Nicks wishes to go from the cell marked (0,0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0,0) to (1,1) will take the exact same cost as going from (−2,−1) to (−1,0). The costs are given in the input in the order c1, c2, c3, c4, c5, c6 as in the picture below.
在这里插入图片描述

Print the smallest cost of a path from the origin which has coordinates (0,0) to the given cell.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers x and y (−109≤x,y≤109) representing the coordinates of the target hexagon.

The second line of each test case contains six integers c1, c2, c3, c4, c5, c6 (1≤c1,c2,c3,c4,c5,c6≤109) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).

Output
For each testcase output the smallest cost of a path from the origin to the given cell.

Example

input

2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

output

18
1000000000000000000
题意

开始在坐标为 (0, 0) 的六边形内,要走到给出的 (x, y) 六边形,给出往每个方向走的花费,问走到目标的最小花费。

分析

我们可以发现其实这道题就是在原本只有 x 轴和 y 轴的直角坐标系的基础上又添加了一条坐标。我们将这些坐标画出来就是这样:
在这里插入图片描述
图里两条红色的坐标就是我们熟悉的 x 和 y 轴,蓝色的轴我们不妨叫他 z 轴,这三根轴将图划分为了六个象限。

我们举个例子:
在这里插入图片描述

如图,如果我们要走到坐标 (-3, -2) ,那么可能的最短的路径可能有一号和二号两条路,我们如何区分这两条路谁更优呢,可以得到:
一号路长度为:c4 + c4 + c3;
二号路长度为:c3 + c3 + c4 + c5。
c3 + c5 移动的坐标等效于 c4 ,但是他们的花费可能是不同的,所以我们可以在开始预处理出每个方向的最短花费:
val[i] = min(dir[i], dir[i-1] + dir[i+1])(dir 即 c,注意边界)

这样我们就可以简化问题,如果目标点在一、三、四、六象限,直接先从 z 轴走,再通过 x 轴或 y 轴的方向走即可。

同样的,如果目标在二或五象限,我们就可以直接忽略 z 轴的方向,直接从 x 轴和 y 轴的方向走到目标(就和直角坐标系一样了)。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int T;
int x,y;
int val[7];
int dir[7];

int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&x,&y);
		for(int i=1;i<=6;i++)
			scanf("%d",&dir[i]);
		for(int i=1;i<=6;i++)//预处理 
			val[i] = min(dir[i], dir[i-1==0?6:i-1] + dir[i+1==7?1:i+1]);
		ll ans = 0;
		if(1ll * x * y <= 0)//目标在第二或第五象限或在x轴或y轴上 
		{
			if(x > 0) ans += 1ll * abs(x) * val[6];
			else ans += 1ll * abs(x) * val[3];
			if(y > 0) ans += 1ll * abs(y) * val[2];
			else ans += 1ll * abs(y) * val[5];
		}
		else//目标在第一或三、四、六象限或z轴上 
		{
			if(x > 0)
			{
				int temp = min(abs(x), abs(y));
				ans += 1ll * temp * val[1];
				x -= temp;
				y -= temp;
				ans += 1ll * abs(x) * val[6];
				ans += 1ll * abs(y) * val[2];
			}
			else
			{
				int temp = min(abs(x), abs(y));
				ans += 1ll * temp * val[4];
				x += temp;
				y += temp;
				ans += 1ll * abs(x) * val[3];
				ans += 1ll * abs(y) * val[5];
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值