Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Created October 24, 2015 18:07
Show Gist options
  • Save jdiez17/178ce9de96b1b8748b9d to your computer and use it in GitHub Desktop.
Save jdiez17/178ce9de96b1b8748b9d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Block {
public:
int s, x, y, val;
char orientation;
Block(int _s, int _r, int _c, int _v, char _o): s(_s), y(_r), x(_c), val(_v), orientation(_o) {}
bool operator==(const Block& other) {
return other.s == s && other.x == x && other.y == y && other.orientation == orientation && other.val == val;
}
Block& operator=(const Block& from) {
s = from.s;
x = from.x;
y = from.y;
val = from.val;
orientation = from.orientation;
return *this;
}
};
class Board {
private:
void replace(Block& blck, int val) {
for(int ss = 0; ss < blck.s; ss++) {
if(blck.orientation == 'V') {
cells[blck.x + (blck.y + ss) * n] = val;
} else {
cells[blck.x + ss + blck.y * n] = val;
}
}
}
public:
int n;
std::vector<int> cells;
std::vector<Block> blocks;
Board(int _n): cells(_n * _n), n(_n) {}
void print() {
for(int y = 0; y < n; y++) {
for(int x = 0; x < n; x++) {
cout << cells[x + y * n];
}
cout << endl;
}
}
void apply(Block& blck) {
replace(blck, blck.val);
blocks.push_back(blck);
}
void remove(Block& blck) {
replace(blck, 0);
blocks.erase(find(blocks.begin(), blocks.end(), blck));
}
bool aabb(int x, int y) {
return x > 0 && x < n && y > 0 && y < n; // && cells[x + y * n] == 0;
}
};
typedef struct {
Block old;
Block newb;
} Move;
int main(void) {
int tests;
cin >> tests;
for(int i = 0; i < tests; i++) {
int y, n, b;
cin >> y >> n >> b;
Board brd(n);
Block yellow(2, 2, y, 1, 'H');
brd.apply(yellow);
for(int ii = 0; ii < b; ii++) { // read block spec
int s, r, c;
char orientation;
cin >> s >> orientation >> r >> c;
Block blck(s, r, c, 2, orientation);
brd.apply(blck);
}
brd.print();
int ii = 0;
std::queue<Move> bfsq;
do {
// find all valid moves
for(auto it = brd.blocks.begin(); it != brd.blocks.end(); it++) {
Block blck = *it;
////// ACHTUNG \\\\\\
////// This part is broken.
////// It doesn't check if there's a block in the cell where the block would move.
if(blck.orientation == 'V') {
int yup = blck.y - 1;
if(brd.aabb(blck.x, yup)) {
Block newb = blck;
newb.y -= 1;
bfsq.push(Move{blck, newb});
}
int ydown = blck.y + blck.s + 1;
if(brd.aabb(blck.x, ydown)) {
Block newb = blck;
newb.y += 1;
bfsq.push(Move{blck, newb});
}
}
if(blck.orientation == 'H') {
int xleft = blck.x - 1;
if(brd.aabb(xleft, blck.y)) {
Block newb = blck;
newb.x -= 1;
bfsq.push(Move{blck, newb});
}
int xright = blck.x + blck.s + 1;
if(brd.aabb(xright, blck.y)) {
Block newb = blck;
newb.x += 1;
bfsq.push(Move{blck, newb});
}
}
}
if(!bfsq.size())
break;
// get move from queue
Move& move = bfsq.front();
bfsq.pop();
// execute move
brd.remove(move.old);
brd.apply(move.newb);
cout << endl << endl << endl;
brd.print();
} while(ii++ < 10);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment