结构体的简单应用HDU4584

<a target=_blank href="http://http://acm.hdu.edu.cn/showproblem.php?pid=4584" target="_blank">原题</a><a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=4584">http://acm.hdu.edu.cn/showproblem.php?pid=4584</a>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>

using namespace std;

struct point
{
	int x;
	int y;
}ph[2500],pc[2500];
struct point1
{
	int x1;
	int y1;
	int x2;
	int y2;
}ans1[2500];//最好就是要这样来定义结构体数组。




bool acmp(point1 a,point1 b)
{
	if(a.x1 == b.x1)
	{
		if(a.y1 == b.y1)
		{
			if(a.x2 == b.x2)
				return a.y2 < b.y2;
			return a.x2 < b.x2;
		}
		return a.y1 < b.y1;
	}
	return a.x1 < b.x1;
}
bool cmp(point a,point b)
{
	if(a.x == b.x)
		return a.y < b.y;
	return a.x < b.x;
};

int main()
{
	int n,m,i,j,s,ss,dis,count;
	char map[50][50];

	while(scanf("%d%d",&n,&m),n,m)//注意书写
	{
		s = 0;
		ss = 0;
		count = 0;
		int Max = 999999;
		for(i=0;i<n;i++)
		{
			getchar();
			for(j=0;j<m;j++)
			{
				scanf("%c",&map[i][j]);
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(map[i][j] == 'H')
				{
					ph[s].x = i;
					ph[s].y = j;
					s++;
				}
				if(map[i][j] == 'C')
				{
					pc[ss].x = i;
					pc[ss].y = j;
					ss++;
				}
			}
		}
		sort(ph,ph+s,cmp);//s代表的是有几个
		sort(pc,pc+ss,cmp);//将满足这两个的直接按照x和y的顺序排列
		for(i=0;i<s;i++)//(s,h),(c,ss)
		{
			for(j=0;j<ss;j++)
			{
				dis = abs(ph[i].x-pc[j].x)+abs(ph[i].y - pc[j].y);
				if(dis < Max)
					Max = dis;
			}
		}
		for(i=0;i<s;i++)
		{
			for(j=0;j<ss;j++)
			{
				if((abs(ph[i].x-pc[j].x)+abs(ph[i].y-pc[j].y)) == Max)
				{
					ans1[count].x1 = ph[i].x;
					ans1[count].y1 = ph[i].y;
					ans1[count].x2 = pc[j].x;
					ans1[count].y2 = pc[j].y;
					count++;
				}
			}
		}
		sort(ans1,ans1+count,acmp);
		printf("%d %d %d %d\n",ans1[0].x1,ans1[0].y1,ans1[0].x2,ans1[0].y2);
	}

	return 0;
}
/首先将H,和C的记录下来,放到一个结构体里面。并且对这些进行排序吗,不进行排序也是可以的。
//这道题目的关键就是对结构体要熟悉,只有这样才能做出来。
//特别是对结构体的;排序。
//在定义结构体数组时候要最好要按照上面的例子来。

原题 http://acm.hdu.edu.cn/showproblem.php?pid=4584
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值