我的代码:
#include <iostream> #include <vector> using namespace std; int main() { string str; cin >> str; int dxy[][2] = { {0,1},{1,0},{0,-1},{-1,0} }; //设置偏移量,按照右转顺序:北->东->南->西 int now = 0; //设置朝向,默认0为北 int x = 0, y = 0; //初始坐标,按照x,y坐标轴 for (int i = 0; i < str.size(); i++) //遍历字符串 { switch (str[i]) { case 'G': //如果是前进指令,则坐标加上对应的偏移量 x += dxy[now % 4][0]; //%4是为了让now取值保持在1~3中 y