[hdu 4452] Running Rabbits 模拟

本文介绍了一个名为RunningRabbits的编程竞赛题目,该题目要求模拟两只兔子在一个N×N网格上的运动轨迹,并根据特定规则进行方向变化。文章提供了完整的解题思路和实现代码,有助于理解如何通过编程解决此类模拟问题。

Running Rabbits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1657 Accepted Submission(s): 1163

Problem Description
Rabbit Tom and rabbit Jerry are running in a field. The field is an N×N grid. Tom starts from the up-left cell and Jerry starts from the down-right cell. The coordinate of the up-left cell is (1,1) and the coordinate of the down-right cell is (N,N)。A 4×4 field and some coordinates of its cells are shown below:

The rabbits can run in four directions (north, south, west and east) and they run at certain speed measured by cells per hour. The rabbits can’t get outside of the field. If a rabbit can’t run ahead any more, it will turn around and keep running. For example, in a 5×5 grid, if a rabbit is heading west with a speed of 3 cells per hour, and it is in the (3, 2) cell now, then one hour later it will get to cell (3,3) and keep heading east. For example again, if a rabbit is in the (1,3) cell and it is heading north by speed 2,then a hour latter it will get to (3,3). The rabbits start running at 0 o’clock. If two rabbits meet in the same cell at k o’clock sharp( k can be any positive integer ), Tom will change his direction into Jerry’s direction, and Jerry also will change his direction into Tom’s original direction. This direction changing is before the judging of whether they should turn around.
The rabbits will turn left every certain hours. For example, if Tom turns left every 2 hours, then he will turn left at 2 o’clock , 4 o’clock, 6 o’clock..etc. But if a rabbit is just about to turn left when two rabbit meet, he will forget to turn this time. Given the initial speed and directions of the two rabbits, you should figure out where are they after some time.

Input
There are several test cases.
For each test case:
The first line is an integer N, meaning that the field is an N×N grid( 2≤N≤20).
The second line describes the situation of Tom. It is in format “c s t”。c is a letter indicating the initial running direction of Tom, and it can be ‘W’,’E’,’N’ or ‘S’ standing for west, east,
north or south. s is Tom’s speed( 1≤s means that Tom should turn left every t hours( 1≤ t ≤1000).
The third line is about Jerry and it’s in the same format as the second line.
The last line is an integer K meaning that you should calculate the position of Tom and Jerry at K o’clock( 1 ≤ K ≤ 200).
The input ends with N = 0.

Output
For each test case, print Tom’s position at K o’clock in a line, and then print Jerry’s position in another line. The position is described by cell coordinate.

这里写图片描述

Sample Input

4
E 1 1
W 1 1
2
4
E 1 1
W 2 1
5
4
E 2 2
W 3 1
5
0

Sample Output

2 2
3 3
2 1
2 4
3 1
4 1

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4452

题意+思路:模拟每一秒两只兔子的位置
对于转弯,先跑,再看碰不碰到,再转。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char d1,d2,cc;
int x1,x2,y1,y2,v1,v2,t1,t2;
int n;
int k;
char turn(char c)
{
    if(c=='W')return 'S';
    if(c=='E')return 'N';  
        if(c=='N')return 'W';  
        if(c=='S')return 'E'; 
}
void go(char &c,int v,int &x,int &y)
{
    if(c=='W')   y-=v;
    if(c=='E')   y+=v;  
        if(c=='N')   x-=v;  
        if(c=='S')   x+=v; 
    if(x<1){x=2-x;c='S';}  
        if(x>n){x=2*n-x;c='N';}  
        if(y<1){y=2-y;c='E';}  
        if(y>n){y=2*n-y;c='W';}  
}
int main()
{
    while(scanf("%d",&n))
    {
        char s1[3],s2[3];
        if(!n) return 0;
        scanf("%s%d%d",s1,&v1,&t1);
        scanf("%s%d%d",s2,&v2,&t2);
        d1=s1[0];
        d2=s2[0];
        x1=y1=1;
        x2=y2=n;

        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
            go(d1,v1,x1,y1);
            go(d2,v2,x2,y2);
            if(x1==x2&&y1==y2)
            {
                cc=d1;d1=d2;
                d2=cc;
            }
            else
            {

                if(i%t1==0)
                d1=turn(d1);
                if(i%t2==0)
                d2=turn(d2);
            }

        }
        printf("%d %d\n%d %d\n",x1,y1,x2,y2);
    }
}
内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值