-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTron Battle.cpp
182 lines (145 loc) · 4.39 KB
/
Tron Battle.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
180
181
182
#include <iostream>
#include <string>
using namespace std;
// Size of the map
const int WIDTH = 30;
const int HEIGHT = 20;
// Map
int Map[HEIGHT][WIDTH] = {0};
// Current position
int mx, my;
// Last direction
int lastDirection = 0;
// Record the visited nodes in the map
bool writeInMap(int x0, int y0, int x1, int y1);
// Get distance for continuous unvisited nodes
void getDistance(int mx, int my, int * left, int * right, int * up, int * down);
// Get the direction with longest continuous unvisited nodes
int max(int left, int right, int up, int down);
int main()
{
int n, p, x0, y0, x1, y1, no;
int left, right, up, down;
while (1) {
// Read information from standard input
cin >> n;
cin >> p;
cin >> no >> no >> x0 >> y0 >> no >> no >> x1 >> y1;
if (p == 0) {
mx = x0;
my = y0;
} else {
mx = x1;
my = y1;
}
// Record the visited nodes in the map
writeInMap(x0, y0, x1, y1);
// Get distance for continuous unvisited nodes
getDistance(mx, my, &left, &right, &up, &down);
// Get the direction with longest continuous unvisited nodes
int x = max(left, right, up, down);
// output control varialbe
if (x == 1) {
cout << "LEFT" << endl;
lastDirection = 1;
}else{
if (x == 2) {
cout << "RIGHT" << endl;
lastDirection = 2;
} else {
if (x == 3) {
cout << "UP" << endl;
lastDirection = 3;
} else {
if (x == 4) {
cout << "DOWN" << endl;
lastDirection = 4;
}else{
cout << "LEFT" << endl;
}
}
}
}
// for debuging
cerr << "mx: " << mx << endl;
cerr << "my: " << my << endl;
cerr << "x0: " << x0 << endl;
cerr << "y0: " << y0 << endl;
cerr << "x1: " << x1 << endl;
cerr << "y1: " << y1 << endl;
cerr << "left:" << left << endl;
cerr << "right:" << right << endl;
cerr << "up:" << up << endl;
cerr << "down:" << down << endl;
}
return 0;
}
// Record the visited nodes in the map
bool writeInMap(int x0, int y0, int x1, int y1)
{
if (x0 < 30 && x1 < 30 && y0 < 20 && y1 < 20) {
Map[y0][x0] = 1;
Map[y1][x1] = 1;
return true;
} else {
return false;
}
}
// Get distance for continuous unvisited nodes
void getDistance(int mx, int my, int * left, int * right, int * up, int * down)
{
*left = *right = *up = *down = 0;
//get left distance
for (int i = mx - 1; i > 0 && Map[my][i] == 0; i--) {
cerr << "for left i is: " << i << endl;
cerr << "for left Map[my][i] is: " << Map[my][i] << endl;
*left = mx - i;
}
//get right distance
for (int i = mx + 1; i > 0 && i < 30 && Map[my][i] == 0; i++) {
cerr << "for right i is: " << i << endl;
*right = i - mx;
}
//get up distance
for (int i = my - 1; i > 0 && Map[i][mx] == 0; i--) {
*up = my - i;
}
//get down distance
for (int i = my + 1; i > 0 && i < 20 && Map[i][mx] == 0; i++) {
*down = i - my;
}
}
// Get the direction with longest continuous unvisited nodes
int max(int left, int right, int up, int down)
{
if (lastDirection == 1 && left > 0) {
return 1;
} else {
if (lastDirection == 2 && right >0) {
return 2;
} else {
if (lastDirection == 3 && up > 0) {
return 3;
} else {
if (lastDirection == 4 && down >0) {
return 4;
}
}
}
}
if (left >= right && left >= up && left >= down ) {
return 1;
} else {
if (right >= left && right >= up && right >= down ){
return 2;
}else{
if (up >= left && up >= right && up >= down ){
return 3;
}else{
if (down >= left && down >= right && down >= up) {
return 4;
}
}
}
}
}