天天看點

HDU1033 Edge

HDU1033 Edge

現在有一個固定初始坐标(300.420)的點,它按照一定的操作指令序列每向前走10個機關長度的距離就左轉(V)或右轉(A)。現在要每個操作指令序列來輸出這個點的軌迹(并遵循一定的格式)。

輸入:每一行是一個執行個體。每行包含一個非空的長度不超過200的由A和V構成的字元串。以檔案結束符表示輸入結束。

輸出:對于每個輸入執行個體,以以下的例子作為輸入标準。

HDU1033 Edge

以上圖檔代表兩個執行個體:V和AVV

執行個體V的輸出為:

300 420 moveto

310 420 lineto

310 430 lineto

stroke

showpage

執行個體AVV的輸出為:

300 420 moveto

310 420 lineto

310 410 lineto

320 410 lineto

320 420 lineto

stroke

showpage

分析:由此可得輸出中,300 420 moveto 與310 420 lineto與stroke 與showpage是必須的,然後中間的 X,Y lineto 是起始點通過執行A或V所到達的一個個後續點。點的目前運作方向隻有4個0,1,2,3分别代表左,右,上,下。執行A操作時,方向對應會改變成以下4個,上,下,右,左。執行V操作時,方向對應會改變成以下4個,下,上,左,右。

另外4個方向,左,右,上,下,對應的x,y坐标增減也是一目了然的。

一步一步模拟即可。

AC代碼:

#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
 
const int L=0,R=1,U=2,D=3;//代表左右上下4方向
int dir_A[]={U,D,R,L};//執行A右轉操作時4個方向變為對應的UDRL方向
int dir_V[]={D,U,L,R};//執行V左轉操作時4個方向變為對應的DULR方向
int dx[]={-10,10,0,0};//左右上下4方向對應的X坐标增量
int dy[]={0,0,10,-10};//y增量
 
int main()
{
    string com;
   while(cin>>com)//讀指令
    {
       cout<<"300 420 moveto"<<endl;
       cout<<"310 420 lineto"<<endl;
        intx=310,y=420;//初始的坐标
        intcur_dir = 1;//初始的方向始終向右
        for(inti=0;i<com.size();i++)
        {
 
            charc=com[i];//指令
            intnew_dir ;//新的方向
           if(c=='A')
            {
               new_dir = dir_A[cur_dir];
               x+=dx[new_dir];//新方向X增量
               y+=dy[new_dir];//新方向Y增量
            }
            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;
}