解题思路

  1. 从(0,0)点开始,根据输入的指令序列移动坐标
  2. 对输入字符串按分号分割成单个指令
  3. 对每个指令进行合法性验证:
    • 第一个字符必须是A/D/W/S
    • 后面必须是1-2位的数字
  4. 根据方向更新坐标:
    • A: x减去数字
    • D: x加上数字
    • W: y加上数字
    • S: y减去数字
  5. 输出最终坐标

代码

#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool isValidCommand(string& cmd) {
    if (cmd.length() < 2 || cmd.length() > 3) return false;
    if (cmd[0] != 'A' && cmd[0] != 'D' && cmd[0] != 'W' && cmd[0] != 'S') return false;
    for (int i = 1; i < cmd.length(); i++) {
        if (!isdigit(cmd[i])) return false;
    }
    return stoi(cmd.substr(1)) <= 99;
}

int main() {
    string input;
    getline(cin, input);
    
    int x = 0, y = 0;
    string cmd;
    
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == ';') {
            if (isValidCommand(cmd)) {
                int num = stoi(cmd.substr(1));
                switch(cmd[0]) {
                    case 'A': x -= num; break;
                    case 'D': x += num; break;
                    case 'W': y += num; break;
                    case 'S': y -= num; break;
                }
            }
            cmd = "";
        } else {
            cmd += input[i];
        }
    }
    // 处理最后一个命令
    if (isValidCommand(cmd)) {
        int num = stoi(cmd.substr(1));
        switch(cmd[0]) {
            case 'A': x -= num; break;
            case 'D': x += num; break;
            case 'W': y += num; break;
            case 'S': y -= num; break;
        }
    }
    
    cout << x << "," << y << endl;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static boolean isValidCommand(String cmd) {
        if (cmd.length() < 2 || cmd.length() > 3) return false;
        if (!"ADWS".contains(cmd.substring(0, 1))) return false;
        try {
            int num = Integer.parseInt(cmd.substring(1));
            return num <= 99;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] commands = sc.nextLine().split(";");
        
        int x = 0, y = 0;
        for (String cmd : commands) {
            if (!cmd.isEmpty() && isValidCommand(cmd)) {
                int num = Integer.parseInt(cmd.substring(1));
                switch (cmd.charAt(0)) {
                    case 'A': x -= num; break;
                    case 'D': x += num; break;
                    case 'W': y += num; break;
                    case 'S': y -= num; break;
                }
            }
        }
        
        System.out.println(x + "," + y);
    }
}
def is_valid_command(cmd):
    if len(cmd) < 2 or len(cmd) > 3:
        return False
    if cmd[0] not in 'ADWS':
        return False
    try:
        num = int(cmd[1:])
        return 0 <= num <= 99
    except:
        return False

def move_coordinate():
    x, y = 0, 0
    commands = input().split(';')
    
    for cmd in commands:
        if not cmd or not is_valid_command(cmd):
            continue
            
        direction = cmd[0]
        distance = int(cmd[1:])
        
        if direction == 'A':
            x -= distance
        elif direction == 'D':
            x += distance
        elif direction == 'W':
            y += distance
        elif direction == 'S':
            y -= distance
            
    print(f"{x},{y}")

if __name__ == "__main__":
    move_coordinate()

算法及复杂度

  • 算法:字符串处理 + 模拟
  • 时间复杂度:,其中n是输入字符串的长度
  • 空间复杂度:,需要存储分割后的指令列表