您的位置:首页 > 其它

杭电 1033【转向】

2016-06-05 15:55 162 查看

Edge

[align=left]Input[/align]
The input contains several test cases, each on a separate line. Each line contains a nonempty string of characters A and V describing the longer edge of the sheet. You may assume that the length of the string is less than 200. The
input file terminates immediately after the last test case.

 
[align=left]Output[/align]
For each test case generate a PostScript drawing of the edge with commands placed on separate lines. Start every drawing at the coordinates (300, 420) with the command "300 420 moveto". The first turn occurs at (310, 420) using the
command "310 420 lineto". Continue with clockwise or counter-clockwise turns according to the input string, using a sequence of "x y lineto" commands with the appropriate coordinates. The turning points are separated at a distance of 10 units. Do not forget
the end point of the edge and finish each test case by the commands stroke and showpage.

You may display such drawings with the gv PostScript interpreter, optionally after a conversion using the ps2ps utility.



 
[align=left]Sample Input[/align]

V
AVV

 
[align=left]Sample Output[/align]

300 420 moveto
310 420 lineto
310 430 lineto
stroke
showpage
300 420 moveto
310 420 lineto
310 410 lineto
320 410 lineto
320 420 lineto
stroke
showpage

 
/*
*转向,A表示向右转,V表示向左转,每次是走长度为10,开始转向之前要向右先走10个长度
*每次转向是相对于上次行走的方向
*/
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
//按照题目要求a作为横坐标初始化为300,c作为纵坐标初始化为420,在之后的代码里表现
int a=300,b,c,n,x,y,i;
//按照题目要求输入的转向命令最长为200
char d[200];
while(cin>>d)
{
//使用strlen函数在n中获取到转向命令的长度
n=strlen(d);
//按照题目要求转向之前有要先向右走一次
b=310;c=420;
//按照输出要求输出起始地址然后输出moveto,输出第一次行走后的地址输出lineto开始转向
cout<<a<<" "<<c<<" "<<"moveto"<<endl;
cout<<b<<" "<<c<<" "<<"lineto"<<endl;
//x表示横坐标,y表示纵坐标,起先需要向右行走一次所以x记录为1,y记录为0.
x=1;y=0;
for(i=0;i<n;i++)
{
//如果遇到右转指令需要通过之前的行走方向确定相对右转后的走向
if(d[i]=='A')
{
//如果x等于1表示之前是向右走的,此时的相对右转需要向下走,而横轴不动所以y记为-1,x记为0;纵轴坐标减10,以下同理
if(x==1)
{y=-1;x=0;c-=10;}
else if(x==-1)
{y=1;x=0;c+=10;}
else if(y==1)
{x=1;y=0;b+=10;}
else if(y==-1)
{x=-1;y=0;b-=10;}
}
//如果遇到左转指令也需要通过之前的行走方向确定相对左转后的走向,与上方代码同理
if(d[i]=='V')
{
if(x==1)
{y=1;x=0;c+=10;}
else if(x==-1)
{y=-1;x=0;c-=10;}
else if(y==1)
{x=-1;y=0;b-=10;}
else if(y==-1)
{x=1;y=0;b+=10;}
}
//每进行一次转向后输出目前所在坐标,并输出lineto,接着通过n以内的for循环进行下一次的转向
cout<<b<<" "<<c<<" "<<"lineto"<<endl;
}
//转向均进行完成后,按照代码要求输出stroke和showpage两行字符串
cout<<"stroke"<<endl<<"showpage"<<endl;
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: