-
-
Save moratorium08/2aa569af96f2b50156893bb09a45a0d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <random> | |
using namespace std; | |
void fail() { | |
cerr << "Hmm. Please contact the administrator." << endl; | |
exit(-1); | |
} | |
typedef enum { | |
Beam, | |
Target, | |
} RobotType; | |
typedef struct { | |
RobotType t; | |
int id; | |
// pos | |
int x; | |
int y; | |
} Robot; | |
typedef enum { | |
CEmpty, | |
CRobot, | |
CBlock, | |
CBeam, | |
} CellType; | |
typedef struct { | |
CellType t; | |
Robot *r; // CellTypeがCRobot以外のときは無効 | |
} Cell; | |
Robot beams[1003]; | |
Robot targets[1003]; | |
int beams_count; | |
int targets_count; | |
Cell board[1001][1001]; // board[y][x] でアクセスする。注意 | |
int width, height; | |
char turn; // 'A': Attack, 'D': Defence | |
// 入力をパースして上のグローバルに置かれている変数に格納する | |
void input() { | |
cin >> width >> height >> turn; | |
beams_count = targets_count = 0; | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
string s; | |
cin >> s; | |
switch (s[0]) { | |
case '.': | |
board[i][j].t = CEmpty; | |
break; | |
case '*': | |
board[i][j].t = CBlock; | |
break; | |
case 'x': | |
board[i][j].t = CBeam; | |
break; | |
case 'b': | |
board[i][j].t = CRobot; | |
board[i][j].r = &beams[beams_count]; | |
beams[beams_count].id = stoi(s.substr(1)); | |
beams[beams_count].t = Beam; | |
beams[beams_count].x = j; | |
beams[beams_count].y = i; | |
beams_count++; | |
break; | |
case 't': | |
board[i][j].t = CRobot; | |
board[i][j].r = &targets[targets_count]; | |
targets[targets_count].id = stoi(s.substr(1)); | |
targets[targets_count].t = Target; | |
targets[targets_count].x = j; | |
targets[targets_count].y = i; | |
targets_count++; | |
break; | |
default: | |
fail(); | |
} | |
} | |
} | |
} | |
// ロボットをstderrに表示する | |
void print_robot(Robot *r) { | |
switch (r->t) { | |
case Beam: | |
cerr << "b"; | |
break; | |
case Target: | |
cerr << "t"; | |
break; | |
default: | |
fail(); | |
} | |
cerr << r->id; | |
} | |
// 盤面の様子を出力する | |
void print_board() { | |
for (int y = 0; y < height; y++) { | |
for (int x = 0; x < width; x++) { | |
Cell *c = &board[y][x]; | |
switch (c->t) { | |
case CBeam: | |
cerr << "x"; | |
break; | |
case CEmpty: | |
cerr << "."; | |
break; | |
case CBlock: | |
cerr << "*"; | |
break; | |
case CRobot: | |
print_robot(c->r); | |
break; | |
} | |
} | |
cerr << endl; | |
} | |
} | |
void output(int id, char direction) { | |
cout << id << " " << direction << endl; | |
} | |
int main(void) { | |
random_device gen; | |
mt19937 rand(gen()); | |
while (1) { | |
input(); | |
print_board(); | |
unsigned d = rand() % 4; | |
char c; | |
if (d == 0) c = 'u'; | |
else if (d == 1) c = 'l'; | |
else if (d == 2) c = 'd'; | |
else if (d == 3) c = 'r'; | |
if (turn == 'A') { | |
unsigned x = rand() % beams_count; | |
output(beams[x].id, c); | |
} else { | |
unsigned x = rand() % targets_count; | |
output(targets[x].id, c); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment