Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will cras

本文分享了一段针对POJ3669问题的部分代码实现过程,该问题涉及二维地图上的扩散模拟。代码使用了C++进行编写,并通过结构体存储每个扩散源的位置及时间信息,利用排序来优化处理顺序。

这是自己关于poj3669写的一部分的代码,一直出错,然后就不想写了。不知道自己哪里错了

#include<stdio.h>

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;


int Map[305][305]={0}, m;
int dx[5]={0, 0, 0, 1, -1};
int dy[5]={0, 1, -1, 0, 0};
int time[305][305];
void f(int x, int y, int t);
struct zuo
{
    int x, y;
    int t;
}a[50005];
bool cmp(zuo a, zuo b)
{
    return a.t>b.t;
}
int main()
{
    int a1, a2, a3;
    int i, j;
    cin>>m;
    for(i=0; i<m; i++)
        cin>>a[i].x>>a[i].y>>a[i].t;
    sort(a, a+m, cmp);
    for(i=0; i<m; i++)
    {
        a1 = a[i].x; a2 = a[i].y; a3 = a[i].t;
        f(a1, a2, a3);
    }
    int maxx = a[0].x;
    int maxy = a[0].y;
    for(i=1; i<m; i++)
    {
        if(maxx <= a[i].x) maxx = a[i].x;
        if(maxy <= a[i].y) maxy = a[i].y;
    }
    int maxxy = max(maxx, maxy);
    for(i=0; i<m; i++)
        cout<<a[i].x<<"\t"<<a[i].y<<"\t"<<a[i].t<<endl;
    cout<<maxx<<"\t"<<maxy<<"\t"<<maxxy<<endl;
}
void f(int x, int y, int t)
{
    int i;
    for(i=0; i<5; i++)
    {
        Map[x+dx[i]][y+dy[i]] = 1;
        time[x+dx[i]][y+dy[i]] = t;
    }
}
问题 M: The Lost Cow【Easy】 时间限制: 1.000 Sec 内存限制: 128 MB 题目描述 Farmer John has lost his prize cow Bessie, and he needs to find her! Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position x and Bessie is currently at position y (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position. Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem" (this is actually true!). The recommended solution for Farmer John to find Bessie is to move to position x+1, then reverse direction and move to position x−2, then to position x+4, and so on, in a "zig zag" pattern, each step moving twice as far from his initial starting position as before. As he has read during his study of algorithms for solving the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance |x−y| between himself and Bessie before he finds her (this is also true, and the factor of 9 is actually the smallest such worst case guarantee any strategy can achieve). Farmer John is curious to verify this result. Given x and y, please compute the total distance he will travel according to the zig-zag search strategy above until he finds Bessie. 输入 The single line of input contains two distinct space-separated integers x and y. Both are in the range 0…1,000. 输出 Print one line of output, containing the distance Farmer John will travel to reach Bessie. 样例输入 Copy 3 6 样例输出 Copy 9 模拟问题,用C++
最新发布
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值