Skip to content

Instantly share code, notes, and snippets.

@ouoam
Last active January 30, 2021 14:51
Show Gist options
  • Select an option

  • Save ouoam/c3cd726878f2b9eca0db822858450e46 to your computer and use it in GitHub Desktop.

Select an option

Save ouoam/c3cd726878f2b9eca0db822858450e46 to your computer and use it in GitHub Desktop.
solve the parking lot game but only lv.1
#include <iostream>
#include <stdint.h>
#include <queue>
#include <set>
uint32_t encode(uint8_t map[]) {
uint8_t tempMap[30];
for (int i = 0; i < 30; i ++) {
tempMap[i] = map[i];
}
uint32_t out = 0;
for (int j = 0; j < 30; j++) {
switch (tempMap[j]) {
case ' ':
out = (out << 1);
break;
case '-':
tempMap[j+1] = '=';
out = (out << 1) | 1;
out = (out << 1);
break;
case '|':
tempMap[j+6] = 'l';
out = (out << 1) | 1;
out = (out << 1) | 1;
break;
}
}
return out;
}
uint8_t * decode(uint32_t hash) {
uint8_t * map = new uint8_t[30];
for (int i = 0; i < 30; i++) {
map[i] = '*';
}
int i = 0;
for (int j = 29; j >= 0; j--) {
while (map[i] != '*') i++;
if ((hash >> j) & 1) { // 1
j--;
if ((hash >> j) & 1) { // 11
map[i+6] = 'l';
map[i++] = '|';
} else { // 10
map[i++] = '-';
map[i++] = '=';
}
} else { // 0
map[i++] = ' ';
}
}
return map;
}
struct path {
uint32_t hash;
std::queue<uint32_t> path;
} tempPath;
void bfs(uint32_t hash) {
std::set<uint32_t> visited;
std::queue<path> queue;
tempPath.hash = hash;
queue.push(tempPath);
visited.insert(hash); // start
while (!queue.empty()) {
tempPath = queue.front();
queue.pop();
uint8_t * myMap = decode(tempPath.hash);
if (myMap[6*2+4] == '-' && myMap[6*2+5] == '=') {
break;
}
tempPath.path.push(tempPath.hash);
for (int loc = 0; loc < 30; loc++) {
uint8_t st = 0, en = 0;
auto findPath = [&](int mul, char find1, char find2) {
for (int k = -st; k <= en; k++) {
if (k == 0) continue;
myMap[loc] = ' ';
myMap[loc+mul] = ' ';
myMap[loc+(k*mul)] = find1;
myMap[loc+(k*mul)+mul] = find2;
uint32_t newMap = encode(myMap);
if (visited.find(newMap) == visited.end()) {
tempPath.hash = newMap;
queue.push(tempPath);
visited.insert(newMap);
}
myMap[loc+(k*mul)] = ' ';
myMap[loc+(k*mul)+mul] = ' ';
myMap[loc] = find1;
myMap[loc+mul] = find2;
}
};
if (myMap[loc] == '-') {
st = 0, en = 0;
while (((loc-st-1)%6) != 5 && myMap[loc-st-1] == ' ') {
st++;
}
while (((loc+en+2)%6) != 0 && myMap[loc+en+2] == ' ') {
en++;
}
findPath(1, '-', '=');
} else if (myMap[loc] == '|') {
st = 0, en = 0;
while (((loc-(st*6)-6)/6) != -1 && myMap[loc-(st*6)-6] == ' ') {
st++;
}
while (((loc+(en*6)+12)/6) != 5 && myMap[loc+(en*6)+12] == ' ') {
en++;
}
findPath(6, '|', 'l');
}
}
delete [] myMap;
}
tempPath.path.push(tempPath.hash);
while (!tempPath.path.empty()) {
uint32_t hash = tempPath.path.front();
tempPath.path.pop();
printf("==========\n");
uint8_t * myMap = decode(hash);
for (int i = 0; i < 5; i++) {
printf("%.6s\n", &myMap[i*6]);
}
delete [] myMap;
}
}
int main() {
// 5 * 6
uint8_t map[] =
" --|--"
" | | |"
" |-- |"
" |"
" |";
bfs(encode(map));
printf("end\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment