鱼弦:公众号【红尘灯塔】,CSDN博客专家、内容合伙人、新星导师、全栈领域优质创作者 、51CTO(Top红人+专家博主) 、github开源爱好者(go-zero源码二次开发、游戏后端架构 https://github.com/Peakchen)
问题描述
给定一个初始位置和一系列绘制指令,模拟一台绘图机器在纸平面上绘制线条的路径。
输入
- 初始位置:一个二维坐标表示当前绘图机器的位置。
- 绘制指令:一系列字符串,每个字符串表示一个绘制指令,格式为 "X offsetY",其中 X 表示横向移动的距离,offsetY 表示纵向偏移量。
输出
- 绘制路径:一个由二维坐标组成的列表,表示绘图机器绘制的路径。
示例
初始位置: (0, 0)
绘制指令: ["1 2", "2 -1", "1 1", "-1 2"]
输出:
[
(0, 0),
(1, 2),
(3, 1),
(2, 3),
(1, 5)
]
算法实现
Java
import java.util.ArrayList;
import java.util.List;
public class DrawingMachine {
private static class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
public static List<Point> drawPath(int startX, int startY, String[] instructions) {
List<Point> path = new ArrayList<>();
int x = startX;
int y = startY;
for (String instruction : instructions) {
String[] parts = instruction.split(" ");
int dx = Integer.parseInt(parts[0]);
int dy = Integer.parseInt(parts[1]);
x += dx;
y += dy;
path.add(new Point(x, y));
}
return path;
}
public static void main(String[] args) {
int startX = 0;
int startY = 0;
String[] instructions = {"1 2", "2 -1", "1 1", "-1 2"};
List<Point> path = drawPath(startX, startY, instructions);
for (Point point : path) {
System.out.println(point);
}
}
}
JavaScript
function drawPath(startX, startY, instructions) {
const path = [];
let x = startX;
let y = startY;
for (const instruction of instructions) {
const [dx, dy] = instruction.split(' ').map(Number);
x += dx;
y += dy;
path.push({ x, y });
}
return path;
}
const startX = 0;
const startY = 0;
const instructions = ["1 2", "2 -1", "1 1", "-1 2"];
const path = drawPath(startX, startY, instructions);
console.log(path);
Python
def draw_path(start_x, start_y, instructions):
path = []
x = start_x
y = start_y
for instruction in instructions:
dx, dy = map(int, instruction.split())
x += dx
y += dy
path.append((x, y))
return path
if __name__ == '__main__':
start_x = 0
start_y = 0
instructions = ["1 2", "2 -1", "1 1", "-1 2"]
path = draw_path(start_x, start_y, instructions)
print(path)
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Point {
int x;
int y;
} Point;
Point *drawPath(int startX, int startY, char *instructions) {
Point *path = malloc(sizeof(Point) * 100);
int pathIndex = 0;
int x = startX;
int y = startY;
char *instruction = strtok(instructions, " ");
while (instruction != NULL) {
int dx = atoi(instruction
int dy = atoi(strtok(NULL, " "));
x += dx;
y += dy;
path[pathIndex++] = (Point){x, y};
instruction = strtok(NULL, " ");
}
path[pathIndex] = (Point){-1, -1}; // Sentinel value
return path;
}
int main() {
int startX = 0;
int startY = 0;
char instructions[] = "1 2 2 -1 1 1 -1 2";
Point *path = drawPath(startX, startY, instructions);
int index = 0;
while (path[index].x != -1) {
printf("(%d, %d)\n", path[index].x, path[index].y);
index++;
}
free(path);
return 0;
}
C++
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
struct Point {
int x;
int y;
};
vector<Point> drawPath(int startX, int startY, string instructions) {
vector<Point> path;
int x = startX;
int y = startY;
stringstream ss(instructions);
string instruction;
while (getline(ss, instruction, ' ')) {
int dx, dy;
istringstream iss(instruction);
iss >> dx >> dy;
x += dx;
y += dy;
path.push_back({x, y});
}
return path;
}
int main() {
int startX = 0;
int startY = 0;
string instructions = "1 2 2 -1 1 1 -1 2";
vector<Point> path = drawPath(startX, startY, instructions);
for (const Point& point : path) {
cout << "(" << point.x << ", " << point.y << ")" << endl;
}
return 0;
}
部署测试搭建
上述代码提供了 Java、JavaScript、Python、C 和 C++ 版本的绘图机器算法实现。您可以根据需要选择合适的语言进行部署和测试。
部署
您可以将上述代码编译为可执行程序或将其打包为模块进行部署。
测试
您可以使用以下方法来测试该算法:
- **手动测试:**可以编写一些测试用例并手动运行代码来验证结果。
- **自动化测试:**可以使用单元测试框架来编写自动化测试用例。
示例测试用例
初始位置: (0, 0)
绘制指令: ["1 2", "2 -1", "1 1", "-1 2"]
预期输出:
[
(0, 0),
(1, 2),
(3, 1),
(2, 3),
(1, 5)
]
您可以使用上述测试用例来测试您的代码。
注意:
- 上述代码仅供参考,您可能需要根据实际需求进行修改和完善。
- 确保您的代码经过充分测试,以确保其正确性和可靠性。