forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10024.cpp
179 lines (166 loc) · 2.7 KB
/
10024.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <bits/stdc++.h>
using namespace std;
// roll dir
enum
{
NORTH = 0,
SOUTH = 1,
WEST = 2,
EAST = 3,
NONE = 4,
};
// cube face
enum
{
DOWN = 0,
UP = 1,
RIGHT = 2,
LEFT = 3,
FRONT = 4,
BACK = 5,
};
struct Pos
{
int i;
int j;
};
short board[6][6];
short ok_cube[6] = {1, 1, 1, 1, 1, 1};
void search(Pos *pos, int direction, short *cube);
void roll(short *cube, int direction);
int roll_east(short *cube);
int roll_west(short *cube);
int roll_north(short *cube);
int roll_south(short *cube);
int main()
{
int caseNum = 0;
Pos first_pos = {-1, -1};
short cube[6] = {0, 0, 0, 0, 0, 0};
cin >> caseNum;
for (; caseNum != 0; caseNum--)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
cin >> board[i][j];
if (board[i][j] != 0 && first_pos.i == -1)
{
first_pos.i = i;
first_pos.j = j;
}
}
}
// put cube on the first pos
cube[DOWN] = 1;
board[first_pos.i][first_pos.j] = 2;
search(&first_pos, NONE, cube);
if (0 == memcmp(ok_cube, cube, sizeof(cube)))
{
cout << "correct" << endl;
}
else
{
cout << "incorrect" << endl;
}
if (caseNum > 1)
{
cout << endl;
}
first_pos.i = -1;
first_pos.j = -1;
memset(cube, 0, sizeof(cube));
}
return 0;
}
void search(Pos *pos, int direction, short *cube)
{
// mark the pos has been visited
board[pos->i][pos->j] = 2;
// roll west
if (pos->j > 0 && board[pos->i][pos->j - 1] == 1)
{
pos->j--;
roll_west(cube);
search(pos, EAST, cube);
pos->j++;
}
// roll east
if (pos->j < 5 && board[pos->i][pos->j + 1] == 1)
{
pos->j++;
roll_east(cube);
search(pos, WEST, cube);
pos->j--;
}
// roll north
if (pos->i > 0 && board[pos->i - 1][pos->j] == 1)
{
pos->i--;
roll_north(cube);
search(pos, SOUTH, cube);
pos->i++;
}
// roll south
if (pos->i < 5 && board[pos->i + 1][pos->j] == 1)
{
pos->i++;
roll_south(cube);
search(pos, NORTH, cube);
pos->i--;
}
roll(cube, direction);
}
void roll(short *cube, int direction)
{
switch (direction)
{
case NORTH:
roll_north(cube);
break;
case SOUTH:
roll_south(cube);
break;
case WEST:
roll_west(cube);
break;
case EAST:
roll_east(cube);
break;
case NONE:
break;
}
}
int roll_east(short *cube)
{
cube[RIGHT] = cube[UP];
cube[UP] = cube[LEFT];
cube[LEFT] = cube[DOWN];
cube[DOWN] = 1;
return 0;
}
int roll_west(short *cube)
{
cube[LEFT] = cube[UP];
cube[UP] = cube[RIGHT];
cube[RIGHT] = cube[DOWN];
cube[DOWN] = 1;
return 0;
}
int roll_south(short *cube)
{
cube[FRONT] = cube[UP];
cube[UP] = cube[BACK];
cube[BACK] = cube[DOWN];
cube[DOWN] = 1;
return 0;
}
int roll_north(short *cube)
{
cube[BACK] = cube[UP];
cube[UP] = cube[FRONT];
cube[FRONT] = cube[DOWN];
cube[DOWN] = 1;
return 0;
}