HDU/HDOJ 1033 Edge(水题,模拟题)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1033

题目就是说当输入A时,顺时针旋转,输入V时,逆时针旋转,很简单。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Author: Tanky Woo
// Blog: www.WuTianQi.com
// Title: HDOJ 1033 Edge
// About: 模拟
 
#include <iostream>
#include <string>
using namespace std;
 
typedef struct Point{
    int x, y;
}Point;
 
Point p;
string s;
int dir; // 1-up, 2-down, 3-left, 4-right
 
void funA()
{
    if(dir == 1)
    {
        p.x += 10;
        dir = 4;
    }
    else if(dir == 2)
    {
        p.x -= 10;
        dir = 3;
    }
    else if(dir == 3)
    {
        p.y += 10;
        dir = 1;
    }
    else
    {
        p.y -= 10;
        dir = 2;
    }
    cout << p.x << " " << p.y << " lineto" << endl;
}
 
void funV()
{
    if(dir == 1)
    {
        p.x -= 10;
        dir = 3;
    }
    else if(dir == 2)
    {
        p.x += 10;
        dir = 4;
    }
    else if(dir == 3)
    {
        p.y -= 10;
        dir = 2;
    }
    else
    {
        p.y += 10;
        dir = 1;
    }
    cout << p.x << " " << p.y << " lineto" << endl;
}
 
int main()
{
    while(cin >> s)
    {
        cout << "300 420 moveto" << endl;
        cout << "310 420 lineto" << endl;
        dir = 4;
        p.x = 310;
        p.y = 420;
        for(int i=0; i<s.size(); ++i)
        {
            if(s[i] == 'A')
                funA();
            else
                funV();
        }
        cout << "stroke\nshowpage\n";
    }
    return 0;
}

看见网上有人这么写的,简单多了,我是枚举上下左右四个方向时的变化,那个代码是直接找到相对关系的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <memory>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <time.h>
using namespace std;
const int N = 205; 
struct point{
int x, y; 
point(int x = 0, int y = 0): x(x), y(y) {}
void set(int _x, int _y){ x = _x, y = _y; }
};
char in[N]; 
void solve(){
point st(300, 420), ed(310, 420), ted; 
printf("300 420 moveto\n310 420 lineto\n");
int i; 
for(i = 0; in[i]; i++){
   if(in[i] == 'A'){ //顺时针转
    ted.x = (ed.y - st.y) + ed.x; 
    ted.y = -(ed.x - st.x) + ed.y; 
   }else{ //逆时针旋转
    ted.x = -(ed.y - st.y) + ed.x; 
    ted.y = (ed.x - st.x) + ed.y; 
   }
   st = ed; 
   ed = ted; 
   printf("%d %d lineto\n", ed.x, ed.y);
}
printf("stroke\nshowpage\n");
}
int main(){
freopen("in.txt", "r", stdin); 
//freopen("out.txt", "w", stdout); 
while(scanf("%s", in) != EOF) solve(); 
return 0;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

发表评论

电子邮件地址不会被公开。 必填项已用*标注