Untitled
unknown
c_cpp
2 years ago
3.1 kB
14
Indexable
#include <iostream>
using namespace std;
class Map {
public:
int x_max, y_max;
bool arr[50][100];
Map () {
//cout << "Map created" << endl;
cin >> x_max >> y_max;
for (int i = 0; i < x_max; i++) {
for (int j= 0; j < y_max; j++) {
arr[i][j] = false;
}
}
}
void writeDanger (int x, int y) {
arr[x][y] = true;
}
bool isDangerous (int x, int y) {
return arr[x][y];
}
bool inBorder (int x, int y) {
if (x >= 0 && y >= 0) {
if (x <= x_max && y <= y_max) {
return true;
}
return false;
}
return false;
}
};
Map* world;
class Android {
public:
bool alive;
int pos_x, pos_y;
char dir;
string cmd;
Android (int x, int y, char f) {
alive = true;
pos_x = x;
pos_y = y;
dir = f;
}
void Recv_cmd () {
cin >> cmd;
}
void venture () {
for (int i = 0; i < cmd.length (); i++) {
//cout << "Now at (" << pos_x << ", " << pos_y << "), facing " << dir << endl;
if (!alive) break;
if (cmd[i] != 'F') {
if (cmd[i] == 'R') {
//cout << "Turning right" << endl;
switch (dir) {
case 'E':
dir = 'S';
break;
case 'S':
dir = 'W';
break;
case 'W':
dir = 'N';
break;
case 'N':
dir = 'E';
break;
default:
break;
}
} else if (cmd[i] == 'L') {
//cout << "Turning left" << endl;
switch (dir) {
case 'E':
dir = 'N';
break;
case 'S':
dir = 'E';
break;
case 'W':
dir = 'S';
break;
case 'N':
dir = 'W';
break;
default:
break;
}
}
} else {
//cout << "Moving forward" << endl;
switch (dir) {
case 'W':
if (!world->inBorder (pos_x-1, pos_y))
alive = false;
else
pos_x --;
break;
case 'S':
if (!world->inBorder (pos_x, pos_y-1))
alive = false;
else
pos_y --;
break;
case 'E':
if (!world->inBorder (pos_x+1, pos_y))
alive = false;
else
pos_x ++;
break;
case 'N':
if (!world->inBorder (pos_x, pos_y+1))
alive = false;
else
pos_y ++;
break;
default:
break;
}
if (!alive && world->isDangerous (pos_x, pos_y)) {
//cout << "Not going, cuz it's dangerous" << endl;
alive = true;
continue;
}
}
}
}
};
int main ()
{
// Variables
int border_x, border_y;
int robot_x, robot_y; string robot_dir;
// init world map
//cout << "init world map" << endl;
world = new Map ();
// init androids and move it
while (cin >> robot_x >> robot_y >> robot_dir){
// init bot
Android* man = new Android (robot_x, robot_y, robot_dir[0]);
// bot receive cmd
man->Recv_cmd ();
// bot move on
man->venture ();
// output bot last location
cout << man->pos_x << ' ' << man->pos_y << ' ' << man->dir;
// output bot alive status
if (!man->alive) {
cout << " LOST";
world->writeDanger (man->pos_x, man->pos_y);
}
// end of line
cout << endl;
// clean up
delete (man);
}
}Editor is loading...