A Path Plan(求两个人从y轴不想交走到x轴有多少方案数)

本文解析了一道路径规划算法竞赛题目,描述了如何避免两个角色在从宿舍到教室的路径上相遇的方法。通过数学组合的方式计算不同路径的数量,并考虑路径相交的情况,最终输出安全到达目的地的不同路径数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Path Plan

时间限制: 1 Sec  内存限制: 128 MB

题目描述

WNJXYK hates Destinys so that he does not want to meet him at any time. Luckily, their classrooms and dormitories are at different places. The only chance for them to meet each other is on their way to classrooms from dormitories. 
To simple this question, we can assume that the map of school is a normal rectangular 2D net. WNJXYK’s dormitory located at (0,y_1) and his classroom located at (x_1,0). Destinys’s dormitory located at (0,y_2) and his classroom is located at (x_2,0). On their way to classrooms, they can do two kinds of movement : (x,y)→(x,y-1) and (x,y)→(x+1,y). 
WNJXYK does not want to meet Destinys so that he thinks that it is not safe to let his path to classroom from dormitory has any intersect point with Destinys ‘s. And then he wonders how many different paths for WNJXYK and Destinys arriving their classrooms from dormitories safely.

 

输入

The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains one line with four positive integers x1,x2,y1,y2 which has been explained above.

 

输出

For each test case, output one line containing “y” where y is the number of different paths modulo 10^9+7.

 

样例输入

复制样例数据

3
1 2 1 2
2 3 2 4
4 9 3 13

样例输出

3
60
16886100

 

提示


T≤1000
x1<x2,y1<y2
0 < x1,x2,y1,y2≤100000
For Test Case 1, there are following three different ways.

从(0,y1)到(x1,0)有C(x1 + y1, x1)条路,因为有两个点,所以方案数为C( y1+x1,x1 ) * C( y2+x2,x2 ),

相交的部分我是真的没理解,方案数为C( x1+y2,x1 ) * C( x2+y1,x2 ),然后减一下,就过了??。。。

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n;
int x1, x2, Y1, y2;
LL c[200005];
LL mod = 1e9 + 7;

LL inv(LL x, LL num){
	LL res = 1 % mod;
	x %= mod;
	while(num){
		if(num & 1) res = (res * x) % mod;
		x = x * x % mod;
		num >>= 1;
	}
	return res;
}

LL C(int x, int y){
	LL dx = c[x], dy = c[x - y] * c[y] % mod;
	return dx * inv(dy, mod - 2) % mod;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	c[0] = 1;
	for (int i = 1; i <= 200000; i++) c[i] = c[i - 1] * i % mod;
	scanf("%d", &n);
	while(n--){
		scanf("%d %d %d %d", &x1, &x2, &Y1, &y2);
		LL ans = C(x1 + Y1, x1) * C(x2 + y2, x2) % mod - C(x1 + y2, x1) * C(x2 + Y1, Y1) % mod;
		printf("%lld\n", (ans % mod + mod) % mod);
	}

	return 0;
}
/**/

 

#!/usr/bin/env python # -*- coding: utf-8 -*- import rospy import tf import matplotlib.pyplot as plt from nav_msgs.msg import Path from sensor_msgs.msg import LaserScan from geometry_msgs.msg import PoseStamped import numpy as np # 用于存储路径和障碍物的全局变量 path_points = [] obstacles = [] # 初始化 tf.TransformListener listener = tf.TransformListener() # 订阅路径话题,获取路径信息 def path_callback(msg): global path_points path_points = [] try: # 获取当前时间的转换信息 listener.waitForTransform("map", "base_link", rospy.Time(0), rospy.Duration(1.0)) # 从路径消息中提取坐标点,并转换为 map 坐标系 for pose in msg.poses: # 获取机器位置 (x, y) 坐标 x = pose.pose.position.x y = pose.pose.position.y # 将坐标从 base_link 转换到 map 坐标系 (trans, rot) = listener.lookupTransform("map", "base_link", rospy.Time(0)) x_map = x + trans[0] y_map = y + trans[1] path_points.append((x_map, y_map)) except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException): print("TF Transform error") # 订阅雷达数据,获取障碍物信息 def scan_callback(msg): global obstacles obstacles = [] # 提取雷达角度和距离数据 angle_min = msg.angle_min angle_increment = msg.angle_increment ranges = msg.ranges for i, r in enumerate(ranges): if r < msg.range_max and r > msg.range_min: angle = angle_min + i * angle_increment x = r * np.cos(angle) y = r * np.sin(angle) obstacles.append((x, y)) # 初始化节点和订阅器 def main(): rospy.init_node('path_and_obstacles_visualizer', anonymous=True) rospy.Subscriber('/move_base/GlobalPlanner/plan', Path, path_callback) rospy.Subscriber('/scan', LaserScan, scan_callback) # 设置可视化图形 plt.ion() fig, ax = plt.subplots() while not rospy.is_shutdown(): ax.clear() # 绘制路径 if path_points: path_x, path_y = zip(*path_points) ax.plot(path_x, path_y, label="Planned Path", color="blue") ax.set_xlim(min(path_x) - 1, max(path_x) + 1) ax.set_ylim(min(path_y) - 1, max(path_y) + 1) # 绘制障碍物 if obstacles: obs_x, obs_y = zip(*obstacles) ax.scatter(obs_x, obs_y, label="Obstacles", color="red", s=10) # 设置图形 ax.set_title("Path and Obstacles") ax.set_xlabel("X") ax.set_ylabel("Y") ax.legend() # 更新图形 plt.draw() plt.pause(0.1) # 显示图形 plt.show() # 确保图形显示 if __name__ == '__main__': try: main() except rospy.ROSInterruptException: pass 我想把rviz里面的轨迹和障碍物实现实时跟踪绘制,所以写了这个代码,但是它现在运行不了,帮我改一下,我的rviz的fixed frame是map
03-08
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值