【模拟+数组表示移动方向】HDU-1033 Edge

在这里插入图片描述
在这里插入图片描述

注解

1、模拟题。
2、难点在于四个方向的表示和移动。用数组表示,代码简洁清晰。先找出来下一步的方向,然后在对应的X和Y矩阵上进行坐标的变化。对应代码:

				int new_dir;
                if(c=='A'){
                    new_dir = dir_A[cur_dir];
                }
                else{
                    new_dir = dir_V[cur_dir];
                }
                X += dx[new_dir];
                Y += dy[new_dir];
                cur_dir = new_dir;

3、此题题意比较难理解,用中文简单叙述就是:V表示向左走,A表示向右走。重点关注人的朝向(上下左右四个方向)。朝向不同,走的方向也不同。每次走10个单位长度的距离。

代码

#include <iostream>

using namespace std;

//four directions: up=0, right=1, down=2, left=3
const int U=0,R=1,D=2,L=3;
int dir_A[] = {R, D, L, U};
int dir_V[] = {L, U, R, D};
int dx[] = {0, 10, 0, -10};
int dy[] = {10, 0, -10, 0};

int main() {
    string s;
    while(cin>>s) {
        cout<<"300 420 moveto"<<endl;
        cout<<"310 420 lineto"<<endl;
        int cur_dir = 1;
        int X = 310;
        int Y = 420;
        for(int i=0; i<s.length(); i++){
            char c = s.at(i);
            int new_dir;
            if(c=='A'){
                new_dir = dir_A[cur_dir];
            }
            else{
                new_dir = dir_V[cur_dir];
            }
            X += dx[new_dir];
            Y += dy[new_dir];
            cur_dir = new_dir;
            cout<<X<<" "<<Y<<" lineto"<<endl;
        }
        cout<<"stroke"<<endl;
        cout<<"showpage"<<endl;
    }
    return 0;
}

结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值