需要處理好數(shù)據(jù)的輸入,我在這里WA了幾次
1
2
#include <iostream>
3
#include <cstdio>
4
#include <cstring>
5
using namespace std;
6
7
const int SIZE = 50;
8
const int MAX = 200;
9
const int LEN = 5000;
10
11
struct QUILT
12

{
13
char effect[2][5];
14
}quilt[6];
15
16
struct NODE
17

{
18
int map[SIZE][SIZE];
19
int ht, wt;
20
}node[MAX];
21
22
char exp[LEN];
23
int gLen, gPos, temp[SIZE][SIZE], start;
24
bool mark;
25
26
void Init()
27

{
28
strcpy(quilt[0].effect[0],"//");
29
strcpy(quilt[0].effect[1],"/+");
30
strcpy(quilt[1].effect[0],"\\\\");
31
strcpy(quilt[1].effect[1],"+\\");
32
strcpy(quilt[2].effect[0],"+/");
33
strcpy(quilt[2].effect[1],"//");
34
strcpy(quilt[3].effect[0],"\\+");
35
strcpy(quilt[3].effect[1],"\\\\");
36
strcpy(quilt[4].effect[0],"--");
37
strcpy(quilt[4].effect[1],"--");
38
strcpy(quilt[5].effect[0],"||");
39
strcpy(quilt[5].effect[1],"||");
40
41
gLen = gPos = 0;
42
}
43
44
bool Is(char ch)
45

{
46
if ( ch == ';' || ch == ',' || ch == '(' || ch == ')'
47
|| isalpha(ch) )
48
return true;
49
return false;
50
}
51
52
inline int GetValue(const int& s)
53

{
54
switch(s)
55
{
56
case 0:
57
return 1;
58
case 1:
59
return 2;
60
case 2:
61
return 3;
62
case 3:
63
return 0;
64
case 4:
65
return 5;
66
case 5:
67
return 4;
68
}
69
70
return -1;
71
}
72
73
void Turn(const int& p)
74

{
75
int i, j, k, l, t;
76
77
for ( i = 0, k = node[p].ht - 1; i < node[p].ht; ++i, --k )
78
for ( j = 0, l = 0; j < node[p].wt; ++j, ++l )
79
{
80
temp[j][i] = GetValue( node[p].map[k][l] );
81
}
82
83
t = node[p].ht; node[p].ht = node[p].wt; node[p].wt = t;
84
85
for ( i = 0; i < node[p].ht; ++i )
86
for ( j = 0; j < node[p].wt; ++j )
87
node[p].map[i][j] = temp[i][j];
88
}
89
90
bool Sew(const int& a, const int& b)
91

{
92
if ( node[a].ht != node[b].ht )
93
{
94
mark = true;
95
return false;
96
}
97
98
int i, j, k;
99
100
for ( i = 0; i < node[a].ht; ++i )
101
for ( j = node[a].wt, k = 0; k < node[b].wt; ++j, ++k )
102
node[a].map[i][j] = node[b].map[i][k];
103
104
node[a].wt += node[b].wt;
105
106
return true;
107
}
108
109
int Solve()
110

{
111
if ( mark )
112
return -1;
113
int a = 0, b;
114
115
if ( exp[start] == 's' )
116
{
117
start += 4;
118
a = Solve();
119
start++;
120
121
if ( a == -1 )
122
return -1;
123
124
b = Solve();
125
126
if ( b == -1 )
127
return -1;
128
129
if ( !Sew( a, b ) )
130
a = -1;
131
start++;
132
}
133
else if ( exp[start] == 't' )
134
{
135
start += 5;
136
a = Solve();
137
start++;
138
if ( a == -1 )
139
return -1;
140
Turn( a );
141
}
142
else if ( exp[start] == 'A' || exp[start] == 'B' )
143
{
144
a = gPos;
145
node[gPos].wt = node[gPos].ht = 1;
146
147
if ( exp[start] == 'B' )
148
node[gPos].map[0][0] = 4;
149
else
150
node[gPos].map[0][0] = 0;
151
gPos++;
152
start++;
153
}
154
155
return a;
156
}
157
158
void Output( const int& p )
159

{
160
int i, j, k;
161
162
for ( i = 0; i < node[p].ht; ++i )
163
{
164
for ( k = 0; k < 2; ++k )
165
{
166
for ( j = 0; j < node[p].wt; ++j )
167
{
168
for ( int l = 0; l < 2; ++l )
169
printf("%c", quilt[node[p].map[i][j]].effect[k][l]);
170
}
171
printf("\n");
172
}
173
}
174
}
175
176
int main()
177

{
178
// freopen("1.txt", "r", stdin);
179
180
Init();
181
182
char ch;
183
int p, t = 1;
184
gLen = gPos = 0;
185
mark = false;
186
187
while ( cin >> ch )
188
{
189
if ( Is(ch) )
190
{
191
if ( ch == ';' )
192
{
193
exp[gLen++] = ch;
194
195
printf("Quilt %d:\n", t);
196
197
start = 0;
198
p = Solve();
199
200
if ( p == -1 || mark )
{
201
printf("error\n");
202
}
203
else
{
204
Output(p);
205
}
206
t++;
207
gLen = gPos = 0;
208
mark = false;
209
}
210
else
211
exp[gLen++] = ch;
212
}
213
}
214
215
return 0;
216
}
217