HDU 4584(2013杭州邀请赛I题-水)

http://acm.hdu.edu.cn/showproblem.php?pid=4584

题意:找出一条HC路线,要求HC的曼哈顿距离尽量小,H的x坐标尽量小,y坐标尽量小,C的x坐标尽量小,y坐标尽量小。

思路:直接在每条边去找出答案即可,我是直接排个序就找出答案。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 1605;

struct Point {
    int x, y;
    Point(){}
    Point(int _x, int _y) {
	x = _x; y = _y;
    }
} c[N], h[N];

struct Edge {
    int dis;
    Point a, b;
    Edge() {}
    Edge(Point _a, Point _b) {
	a = _a; b = _b;
	dis = abs(a.x - b.x) + abs(a.y - b.y);
    }
} e[666666];
int n, m, cn, hn, en;
char str[45];

bool cmp(Edge a, Edge b) {
    if (a.dis != b.dis)
	return a.dis < b.dis;
    if (a.a.x != b.a.x)
	return a.a.x < b.a.x;
    if (a.a.y != b.a.y)
	return a.a.y < b.a.y;
    if (a.b.x != b.b.x)
	return a.b.x < b.b.x;
    return a.b.y < b.b.y;
}

int main() {
    while (~scanf("%d%d", &n, &m) && n + m) {
	cn = hn = en = 0;
	for (int i = 0; i < n; i++) {
	    scanf("%s", str);
	    for (int j = 0; j < m; j++) {
		if (str[j] == 'C')
		    c[cn++] = Point(i, j);
		else if (str[j] == 'H')
		    h[hn++] = Point(i, j);
	    }
	}
	for (int i = 0; i < hn; i++) {
	    for (int j = 0; j < cn; j++) {
		e[en++] = Edge(h[i], c[j]);
	    }
	}
	sort(e, e + en, cmp);
	printf("%d %d %d %d\n", e[0].a.x, e[0].a.y, e[0].b.x, e[0].b.y);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值