Edge
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2842 Accepted Submission(s): 1796
Problem Description
For products that are wrapped in small packings it is necessary that the sheet of paper containing the directions for use is folded until its size becomes small enough. We assume that a sheet of paper is rectangular and only folded along lines parallel to its initially shorter edge. The act of folding along such a line, however, can be performed in two directions: either the surface on the top of the sheet is brought together, or the surface on its bottom. In both cases the two parts of the rectangle that are separated by the folding line are laid together neatly and we ignore any differences in thickness of the resulting folded sheet.
After several such folding steps have been performed we may unfold the sheet again and take a look at its longer edge holding the sheet so that it appears as a one-dimensional curve, actually a concatenation of line segments. If we move along this curve in a fixed direction we can classify every place where the sheet was folded as either type A meaning a clockwise turn or type V meaning a counter-clockwise turn. Given such a sequence of classifications, produce a drawing of the longer edge of the sheet assuming 90 degree turns at equidistant places.
Input
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.
Output
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.
Sample Input
Sample Output
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
Source
University of Ulm Local Contest 2003
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1033
读懂题目是关键呀,A顺时针转90度,也就是右转,V逆时针转90度,就是左转,
然后就没有然后了。。。
AC代码:
#include <cstdio>
#include <cstring>
int main()
{
char a[210];
int len;
while(gets(a))
{
len = strlen(a);
printf("300 420 moveto\n310 420 lineto\n");
int x=300,y=420;
int x1=310,y1=420;
for(int i=0; i<len; i++)
{
if(a[i]=='V')//left
{
if(x==x1)//竖直方向
{
if(y1<y)//down---->right
{
x=x1;
y=y1;
x1+=10;
}
else//up----->left
{
x=x1;
y=y1;
x1-=10;
}
}
else//水平方向
{
if(x1>x)//right----->up
{
x=x1;
y=y1;
y1+=10;
}
else
{
x=x1;
y=y1;
y1-=10;
}
}
}
else//right
{
if(x==x1)//shuzhi
{
if(y1<y)//down---->left
{
x=x1;
y=y1;
x1-=10;
}
else//up----->left
{
x=x1;
y=y1;
x1+=10;
}
}
else//shuiping
{
if(x1>x)//right----->down
{
x=x1;
y=y1;
y1-=10;
}
else
{
x=x1;
y=y1;
y1+=10;
}
}
}
printf("%d %d lineto\n",x1,y1);
}
printf("stroke\nshowpage\n");
}
return 0;
}
另外一种思路,先根据当前方向(上下左右),从而确定下一步(转向后)的方向。
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
char str[250];
char flag;
while(scanf("%s",str)!=EOF)
{
int x = 310,y=420;
int len = strlen(str);
printf("300 420 moveto\n310 420 lineto\n");
flag = 'r';//开始时方向向右
for(int i=0; i<len; i++)
{
switch(flag)
{
case 'r':
if(str[i]=='A')
{
y-=10;
flag='d';
}
else
{
y+=10;
flag='u';
}
break;
case 'd':
if(str[i]=='A')
{
x-=10;
flag='l';
}
else
{
x+=10;
flag='r';
}
break;
case 'u':
if(str[i]=='A')
{
x+=10;
flag='r';
}
else
{
x-=10;
flag='l';
}
break;
case 'l':
if(str[i]=='A')
{
y+=10;
flag='u';
}
else
{
y-=10;
flag='d';
}
break;
}
printf("%d %d lineto\n",x,y);
}
printf("stroke\nshowpage\n");
}
return 0;
}