Java 这一步减少了1
Java 这一步减少了1,java,algorithm,Java,Algorithm,x和y机器人的当前坐标 dir\u x,dir\u y-机器人当前方向向量的坐标。该向量的长度为10,我们从指向x轴的向量开始:(10,0) 在每个步骤中,solve功能都会考虑到当前到目标点的距离,并根据需要更新min\u d。如果没有剩余的时间了,或者我们离目标点太远了,我们会立即停止前进。否则我们试着1)向前2)转45度 当机器人向前移动时(考虑当前方向)x变为x+diru\x,y变为y+diru\y。方向向量保持不变 当机器人转向时,其坐标保持不变,但方向向量发生变化。参见(我们的常数c
x
和y
机器人的当前坐标
dir\u x
,dir\u y
-机器人当前方向向量的坐标。该向量的长度为10,我们从指向x
轴的向量开始:(10,0)
在每个步骤中,solve
功能都会考虑到当前到目标点的距离,并根据需要更新min\u d
。如果没有剩余的时间了,或者我们离目标点太远了,我们会立即停止前进。否则我们试着1)向前2)转45度
x
变为x+diru\x
,y
变为y+diru\y
。方向向量保持不变以下是Algrid答案的Java代码:
public class Main {
static double min_d = 1e+10;
// diagonal distance factor cos(45), needs to multiply with hypotenuse
static double c = 0.707110678;
static double xt = 7.0;
static double yt = 17.0;
public static void solve(int time, double x, double y, double dirX, double dirY) {
double d = Math.sqrt(Math.pow((x - xt), 2) + Math.pow((y - yt), 2));
if( d < min_d )
min_d = d;
if( time == 0 || (d-time * 10) >= min_d ){
return;
}
solve(time - 1, x + dirX, y + dirY, dirX, dirY);
solve(time - 1, x, y, c * (dirX - dirY), c * (dirX + dirY));
}
public static void main(String[] args)
{
solve(9, 0.0, 0.0, 10.0, 0.0);
}
}// Class END
公共类主{
静态双最小值=1e+10;
//对角线距离因子cos(45),需要与斜边相乘
静态双c=0.707110678;
静态双xt=7.0;
静态双yt=17.0;
公共静态无效解算(int-time,双x,双y,双dirX,双dirY){
double d=Math.sqrt(Math.pow((x-xt),2)+Math.pow((y-yt),2));
如果(d=min|d){
返回;
}
求解(时间-1,x+dirX,y+dirY,dirX,dirY);
求解(时间-1,x,y,c*(dirX-dirY),c*(dirX+dirY));
}
公共静态void main(字符串[]args)
{
求解(9,0.0,0.0,10.0,0.0);
}
}//班尾
以下是Algrid答案的Java代码:
public class Main {
static double min_d = 1e+10;
// diagonal distance factor cos(45), needs to multiply with hypotenuse
static double c = 0.707110678;
static double xt = 7.0;
static double yt = 17.0;
public static void solve(int time, double x, double y, double dirX, double dirY) {
double d = Math.sqrt(Math.pow((x - xt), 2) + Math.pow((y - yt), 2));
if( d < min_d )
min_d = d;
if( time == 0 || (d-time * 10) >= min_d ){
return;
}
solve(time - 1, x + dirX, y + dirY, dirX, dirY);
solve(time - 1, x, y, c * (dirX - dirY), c * (dirX + dirY));
}
public static void main(String[] args)
{
solve(9, 0.0, 0.0, 10.0, 0.0);
}
}// Class END
公共类主{
静态双最小值=1e+10;
//对角线距离因子cos(45),需要与斜边相乘
静态双c=0.707110678;
静态双xt=7.0;
静态双yt=17.0;
公共静态无效解算(int-time,双x,双y,双dirX,双dirY){
double d=Math.sqrt(Math.pow((x-xt),2)+Math.pow((y-yt),2));
如果(d=min|d){
返回;
}
求解(时间-1,x+dirX,y+dirY,dirX,dirY);
求解(时间-1,x,y,c*(dirX-dirY),c*(dirX+dirY));
}
公共静态void main(字符串[]args)
{
求解(9,0.0,0.0,10.0,0.0);
}
}//班尾
我们是否假设Probot从(0,0)开始?我们是否假设坐标以厘米为单位?也就是说,(5,5)是否对应于原点右侧的5厘米,以及上方的5厘米?@JimMischel从(0,0)开始,机器人确实面向+x方向。对于单位cm,同样是肯定的,因为当您尝试使用上述假设评估给定的输入时,您会得到给定的输出结果。这证明它在cm中。你试过回溯吗?2^24似乎可以回溯。@algrid不,我还没有假定Probot从(0,0)开始?我们是否假设坐标以厘米为单位?也就是说,(5,5)是否对应于原点右侧的5厘米,以及上方的5厘米?@JimMischel从(0,0)开始,机器人确实面向+x方向。对于单位cm,同样是肯定的,因为当您尝试使用上述假设评估给定的输入时,您会得到给定的输出结果。这证明它在cm中。你试过回溯吗?2^24似乎可以回溯。@algrid不,我不认为它有效,但我在理解代码时遇到问题,请您简要解释一下代码。只有函数中的部分solve
以及为什么min_d=1e+10
?@MirwiseKhan我为答案添加了更多解释。感谢它起作用,但我在理解代码时遇到问题,请您简要解释一下代码。只有函数solve
中的部分,以及为什么min_d=1e+10
?@MirwiseKhan我为答案添加了更多解释
import java.awt.Point;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int time = 0;
Point p = new Point(0, 0);
System.out.println("Enter time: ");
time = s.nextInt();
System.out.println( " X: ");
p.x = s.nextInt();
System.out.println( " Y: " );
p.y = s.nextInt();
if(!(time > 0 && time <= 24)){
s.close();
System.err.println("INPUT ERROR!");
return;
}
// Initialize bot facing +x direction
Bot b = new Bot(p, Direction.EAST);
int line = Calculation.getCloseLine(p);
while(time != 0 && b.getDirectionInt() != line){
// Rotate the face towards the point
b.rotate();
time--;
}
s.close();
}
}
import java.awt.Point;
public class Bot {
private Point location;
private Direction direction;
public Bot(){
location = new Point(0, 0);
direction = Direction.EAST;
}
public Bot(Point loc, Direction dir){
location = loc;
direction = dir;
}
public Point move(){
return location;
}
public int rotate(){
direction.incrementDir();
return direction.getDirection();
}
public int getDirectionInt(){
return direction.getDirection();
}
}
import networkx as nx
def one_second(G):
# In each step you can either go forward or turn. Create one edge
# for each node and these two possible moves.
orientation_vec = [(10000000, 0), (7071067, -7071067), (0, -10000000),
(-7071067, -7071067), (-10000000, 0), (-7071067, 7071067),
(0, 10000000), (7071067, 7071067)]
for node in G.nodes():
# edge to next orientation
G.add_edge(node, (node[0], node[1], (node[2] + 1)%8))
# edge going forward
G.add_edge(node, (node[0] + orientation_vec[node[2]][0],
node[1] + orientation_vec[node[2]][1],
node[2]))
def create_graph(max_time):
G = nx.Graph()
G.add_node(n=(0, 0, 0))
for t in range(max_time):
one_second(G)
print(len(G.nodes()))
import math
def find_closest_path(paths, end):
min_dist = end[0]**2 + end[1]**2
best_path = None
for key in paths.keys():
end_path = paths[key][-1]
path_to_end = (end_path[0] - end[0])**2 + (end_path[1]-end[1])**2
if path_to_end < min_dist:
min_dist = path_to_end
best_path = paths[key]
min_dist = math.sqrt(min_dist)/10**6
x = [p[0]/10**6 for p in best_path]
y = [p[1]/10**6 for p in best_path]
return min_dist, x, y
def robot_path(end, max_time):
create_graph(max_time)
paths = nx.single_source_shortest_path(G, (0, 0, 0), cutoff=max_time)
return find_closest_path(paths, end)
from pylab import *
import matplotlib.pyplot as plt
def plot_robot_path(x,y, end):
assert len(x) == len(y)
color=['b']*len(x) + ['r']
fig = plt.figure()
ax = fig.add_subplot(111)
scatter(x+[end[0]], y+[end[1]], s=100 ,marker='o', c=color)
[plot([x[i], x[i+1]], [y[i], y[i+1]], '-', linewidth=3 ) for i in range(len(x)-1)]
left,right = ax.get_xlim()
low,high = ax.get_ylim()
arrow(left, 0, right -left, 0, length_includes_head = True, head_width = 0.15 )
arrow(0, low, 0, high-low, length_includes_head = True, head_width = 0.15 )
grid()
show()
end1=(5*10**6, 5*10**6)
max_time = 24
min_dist1, x1, y1 = robot_path(end1, max_time)
print(min_dist1)
plot_robot_path(x1, y1, (5, 5))
max_time=9
end2=(7*10**6, 17*10**6)
min_dist2, x2, y2 = robot_path(end2, max_time)
print(min_dist2)
plot_robot_path(x2, y2, (7, 17))
import math
min_d = 1e+10
c = 0.70710678
xt = 5.0
yt = 5.0
def solve(remaining_t, x, y, dir_x, dir_y):
global min_d
d = math.sqrt((x - xt)**2 + (y - yt)**2)
if d < min_d:
min_d = d
if remaining_t == 0 or d - remaining_t * 10 >= min_d:
return
solve(remaining_t - 1, x + dir_x, y + dir_y, dir_x, dir_y)
solve(remaining_t - 1, x, y, c * (dir_x - dir_y), c * (dir_x + dir_y))
solve(24, 0.0, 0.0, 10.0, 0.0)
print(min_d)
public class Main {
static double min_d = 1e+10;
// diagonal distance factor cos(45), needs to multiply with hypotenuse
static double c = 0.707110678;
static double xt = 7.0;
static double yt = 17.0;
public static void solve(int time, double x, double y, double dirX, double dirY) {
double d = Math.sqrt(Math.pow((x - xt), 2) + Math.pow((y - yt), 2));
if( d < min_d )
min_d = d;
if( time == 0 || (d-time * 10) >= min_d ){
return;
}
solve(time - 1, x + dirX, y + dirY, dirX, dirY);
solve(time - 1, x, y, c * (dirX - dirY), c * (dirX + dirY));
}
public static void main(String[] args)
{
solve(9, 0.0, 0.0, 10.0, 0.0);
}
}// Class END