Skip to content

Instantly share code, notes, and snippets.

@kg86
Created September 17, 2014 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kg86/b841158352ac2e159881 to your computer and use it in GitHub Desktop.
Save kg86/b841158352ac2e159881 to your computer and use it in GitHub Desktop.
class RotatingBot {
public:
int minArea(vector <int> moves) {
int MAX_W = 55;
int MAX_H = 55;
int newmap[MAX_H*2][MAX_W*2];
vector< vector<int> > map(MAX_H*2);
int i,j;
for(i = 0; i < MAX_H*2; i++){
map[i] = vector<int>(MAX_H*2);
for(j = 0; j < MAX_W * 2; j++){
map[i][j] = 0;
newmap[i][j] = 0;
}
}
int init = 51;
int x = init, y = init;
map[x][y] = 1;
int dx = 1;
int dy = 0;
for(i = 0; i < moves.size(); i++){
int move = moves[i];
for(j = 0; j < move; j++){
if(map[y+dy][x+dx] == 1) return -1;
else map[y+dy][x+dx] = 1;
x += dx;
y += dy;
}
if (dx == 1 && dy == 0){ dx = 0; dy = -1;}
else if (dx == 0 && dy == -1){ dx = -1; dy = 0;}
else if (dx == -1 && dy == 0){ dx = 0; dy = 1;}
else if (dx == 0 && dy == 1){ dx = 1; dy = 0;}
}
int min_x, min_y, max_x, max_y;
min_x = min_y = max_x = max_y = init;
for(i = 0; i < map.size(); i++){
for(j = 0; j < map[i].size(); j++){
if(map[i][j] == 1){
min_x = min(min_x, j);
min_y = min(min_y, i);
max_x = max(max_x, j);
max_y = max(max_y, i);
}
}
}
cout << "min_x=" << min_x << " max_x=" << max_x
<< " min_y=" << min_y << " max_y=" << max_y
<< " value = " << (max_x - min_x + 1)* (max_y - min_y + 1) << endl;
// check
x = init, y = init;
dx = 1;
dy = 0;
newmap[y][x] = 1;
for(i = 0; i < moves.size()-1; i++){
int move = moves[i];
for(j = 0; j < move; j++){
x += dx;
y += dy;
newmap[y][x] = 1;
}
// if(!(x+dx > max_x || x+dx < min_x
// || y+dy > max_y || y+dy < min_y || map[y+dy][x+dx] == 1)){
// cout << "x=" << x << " y=" << y << " dx=" << dx
// << " dy=" << dy << " map[y+dy][x+dx]=" << map[y+dy][x+dx]
// << endl;
// return -1;
// }
cout << "x=" << x << " y=" << y << " dx=" << dx
<< " dy=" << dy << " map[y+dy][x+dx]=" << map[y+dy][x+dx]
<< endl;
if (dx == 1 && dy == 0){
if(!(x == max_x || newmap[y+dy][x+dx] == 1)) return -1;
dx = 0; dy = -1;
}
else if (dx == 0 && dy == -1){
if(!(y == min_y || newmap[y+dy][x+dx] == 1)) return -1;
dx = -1; dy = 0;
}
else if (dx == -1 && dy == 0){
if(!(x == min_x || newmap[y+dy][x+dx] == 1)) return -1;
dx = 0; dy = 1;
}
else if (dx == 0 && dy == 1){
if(!(y == max_y || newmap[y+dy][x+dx] == 1)) return -1;
dx = 1; dy = 0;
}
}
return (max_x - min_x + 1)* (max_y - min_y + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment