Kevin Shepherd's solution:
#include <iostream>
#include <map>
#include <set>
#include <fstream>
#include <string>
#include <list>
#define MAX_CELL_DIM 0xFF
#define MAX_TARGET_COUNT 1
#define MAX_X_DIM 1024
#if 1
#define MAX_Y_DIM 512
#define STATE_ARRAY_SIZE 0x2000000
#define TARGET_MASK 0x1FFFFFFF
#define TARGET_BITS 0x20000000
#define TARGET_ROLL 29
#define POS_X_MASK 0xE007FFFF
#define POS_X_BITS 0x3FF
#define POS_X_ROLL 19
#define POS_Y_MASK 0xFFF803FF
#define POS_Y_BITS 0x1FF
#define POS_Y_ROLL 10
#else
#define MAX_Y_DIM 1024
#define STATE_ARRAY_SIZE 0x4000000
#define TARGET_MASK 0x3FFFFFFF
#define TARGET_BITS 0x40000000
#define TARGET_ROLL 30
#define POS_X_MASK 0xC00FFFFF
#define POS_X_BITS 0x3FF
#define POS_X_ROLL 20
#define POS_Y_MASK 0xFFF003FF
#define POS_Y_BITS 0x3FF
#define POS_Y_ROLL 10
#endif
#define VEL_X_MASK 0xFFFFFC1F
#define VEL_X_BITS 0xF
#define VEL_X_SIGN 0x200
#define VEL_X_ROLL 5
#define VEL_Y_MASK 0xFFFFFFE0
#define VEL_Y_BITS 0xF
#define VEL_Y_SIGN 0x10
#define VEL_Y_ROLL 0
#define VEL_MASK 0x000001EF
unsigned long target_mask = 0;
struct CellCoord
{
unsigned char x, y;
bool operator<(const CellCoord & __other) const
{
return (x < __other.x?true:(x > __other.x?false:y < __other.y));
}
bool operator==(const CellCoord & __other) const
{
return (x == __other.x && y == __other.y);
}
};
class State
{
public:
State()
:packed(0)
{}
State(const State & __other)
:packed(__other.packed)
{}
State & operator=(const State & __other)
{
packed = __other.packed;
}
bool operator<(const State & __other) const
{
return packed<__other.packed;
}
bool operator==(const State & __other) const
{
return (packed == __other.packed);
}
void set_packed(unsigned long __packed)
{
packed = __packed;
}
void set_target(unsigned char )
{
packed |= TARGET_BITS;
}
unsigned long get_targets() const
{
return packed >> TARGET_ROLL;
}
void set_targets(unsigned long __targets)
{
packed &= TARGET_MASK;
packed |= (__targets << TARGET_ROLL);
}
bool all_targets() const
{
return ((packed & TARGET_BITS) != 0);
}
short get_x() const
{
return (short)((packed >> POS_X_ROLL) & POS_X_BITS);
}
void set_x(short __x)
{
packed &= POS_X_MASK;
packed |= ((unsigned long)__x << POS_X_ROLL);
}
short get_y() const
{
return (short)((packed >> POS_Y_ROLL) & POS_Y_BITS);
}
void set_y(short __y)
{
packed &= POS_Y_MASK;
packed |= ((unsigned long)__y << POS_Y_ROLL);
}
short get_vel_x() const
{
return (packed & VEL_X_SIGN)?-(short)((packed >> VEL_X_ROLL) & VEL_X_BITS):+(short)((packed >> VEL_X_ROLL) & VEL_X_BITS);
}
void set_vel_x(short __vx)
{
packed &= VEL_X_MASK;
if (__vx < 0)
{
packed |= VEL_X_SIGN;
__vx = - __vx;
}
if (__vx > 10)
__vx = 10;
packed |= ((unsigned long)__vx << VEL_X_ROLL);
}
short get_vel_y() const
{
return (packed & VEL_Y_SIGN)?-(short)((packed >> VEL_Y_ROLL) & VEL_Y_BITS):+(short)((packed >> VEL_Y_ROLL) & VEL_Y_BITS);
}
void set_vel_y(short __vy)
{
packed &= VEL_Y_MASK;
if (__vy < 0)
{
packed |= VEL_Y_SIGN;
__vy = - __vy;
}
if (__vy > 10)
__vy = 10;
packed |= ((unsigned long)__vy << VEL_Y_ROLL);
}
bool has_vel() const
{
return ((packed & VEL_MASK) != 0);
}
unsigned long packed;
};
struct Active
{
State st;
std::string path;
Active()
{}
Active(const Active & __other)
:st(__other.st), path(__other.path)
{}
Active & operator=(const Active & __other)
{
st = __other.st;
path = __other.path;
}
};
typedef std::list<Active> ActiveList;
unsigned long state_array[STATE_ARRAY_SIZE];
ActiveList current_states;
unsigned char cell_map[MAX_CELL_DIM+1][MAX_CELL_DIM+1];
unsigned long target_baseline;
unsigned long _cell_width = 0, _cell_height = 0;
unsigned long _pixel_width = 0, _pixel_height = 0;
unsigned long state_count = 0;
bool is_set_state(State _st)
{
return ( state_array[_st.packed >> 5] & (1 << (_st.packed & 0x1F) ) ) != 0;
}
void set_state(State _st)
{
state_array[_st.packed >> 5] |= (1 << (_st.packed & 0x1F) );
}
bool transition (unsigned char __impulse, const Active & __from);
int main(int _argc, char * _argv[])
{
if (_argc < 2 )
{
std::cerr << "Usage: " << _argv[0] << " <map>" << std::endl;
return 1;
}
std::ifstream _in(_argv[1]);
_in >> _cell_width;
_in >> _cell_height;
std::cout << "Cells: " << _cell_width << "x" << _cell_height << std::endl;
if (_cell_width == 0 || _cell_width > MAX_CELL_DIM ||
_cell_height == 0 || _cell_height > MAX_CELL_DIM)
{
std::cerr << "Wrong cell dimensions 0 < cell < " << MAX_CELL_DIM << std::endl;
return 1;
}
_pixel_width = _cell_width * 10;
_pixel_height = _cell_height * 10;
if (_pixel_width > MAX_X_DIM || _pixel_height > MAX_Y_DIM)
{
std::cerr << "Map too large " << _pixel_width << "x" << _pixel_height * 10 << " > " << MAX_X_DIM << "x" << MAX_Y_DIM << std::endl;
return 1;
}
_pixel_width -= 9;
_pixel_height -= 9;
std::string _line;
unsigned long _target_max = 0;
Active _initial_state;
CellCoord _cell_pos;
_cell_pos.y = 0;
memset(cell_map, 0, sizeof(unsigned char [MAX_CELL_DIM+1][MAX_CELL_DIM+1]));
memset(state_array, 0, sizeof(unsigned long [STATE_ARRAY_SIZE]));
unsigned long _open_cell_count = 0;
while (_in.good() && _cell_pos.y < _cell_height)
{
std::getline(_in, _line);
if (_line.size() >= _cell_width)
{
std::cout << _line << std::endl;
for (_cell_pos.x = 0; _cell_pos.x < _cell_width; ++ _cell_pos.x)
{
switch (_line[_cell_pos.x])
{
case '#':
cell_map[_cell_pos.x][_cell_pos.y] = 1;
break;
case 'O':
cell_map[_cell_pos.x][_cell_pos.y] = 2;
_initial_state.st.set_x(_cell_pos.x * 10);
_initial_state.st.set_y(_cell_pos.y * 10);
++ _open_cell_count;
break;
case '+':
++ _target_max;
cell_map[_cell_pos.x][_cell_pos.y] = (unsigned char)(_target_max + 2);
++ _open_cell_count;
break;
case ' ':
default:
cell_map[_cell_pos.x][_cell_pos.y] = 0;
++ _open_cell_count;
break;
}
}
++ _cell_pos.y;
}
}
if (_target_max > MAX_TARGET_COUNT)
{
std::cerr << "Too many targets " << _target_max << " > " << MAX_TARGET_COUNT << std::endl;
return 1;
}
else
{
std::cout << "Target count " << _target_max << " Open Cell Count: " << _open_cell_count << std::endl;
}
target_mask = 0xFFFFFFFF >> (32 - _target_max);
std::cout << "Target mask " << target_mask << std::endl;
set_state(_initial_state.st);
current_states.push_back(Active());
ActiveList::iterator _end_2 = current_states.end();
-- _end_2;
current_states.push_back(Active());
ActiveList::iterator _end_1 = current_states.end();
-- _end_1;
current_states.push_back(_initial_state);
bool _found = false;
unsigned long _iter = 0;
unsigned long _prev_target_baseline = 0;
while (!_found)
{
target_baseline = 0xFFFFFFFF;
ActiveList::iterator _ax = current_states.begin();
for (; _ax != _end_2; ++ _ax)
{
if (transition(3, * _ax))
{
_found = true;
break;
}
if (transition(6, * _ax))
{
_found = true;
break;
}
}
current_states.erase(current_states.begin(), ++ _end_2);
if (!_found)
{
_ax = current_states.begin();
for (; _ax != _end_1; ++ _ax)
{
if (transition(1, * _ax))
{
_found = true;
break;
}
if (transition(2, * _ax))
{
_found = true;
break;
}
if (transition(4, * _ax))
{
_found = true;
break;
}
}
}
if (!_found)
{
_ax = _end_1;
for (++ _ax; _ax != current_states.end(); ++ _ax)
{
if (transition(0, * _ax))
{
_found = true;
break;
}
}
}
_end_2 = _end_1;
current_states.push_back(Active());
_end_1 = current_states.end();
-- _end_1;
if (target_baseline != _prev_target_baseline)
{
State _t_from, _t_to;
_t_from.set_targets(_prev_target_baseline);
_t_to.set_packed(0xFFFFFFFF);
_t_to.set_targets(_prev_target_baseline);
std::cout << "Erasing Target base: " << _prev_target_baseline << std::endl;
// states_reached.erase(states_reached.lower_bound(_t_from), states_reached.upper_bound(_t_to));
_prev_target_baseline = target_baseline;
}
++ _iter;
std::cout << '.';
{ // report
unsigned char report_map[MAX_CELL_DIM+1][MAX_CELL_DIM+1];
memcpy(report_map, cell_map, sizeof(unsigned char [MAX_CELL_DIM+1][MAX_CELL_DIM+1]));
unsigned long _active_count = 0;
for (ActiveList::iterator _ax = current_states.begin(), _ay = current_states.end(); _ax != _ay; ++ _ax)
{
unsigned char _content = report_map[ (* _ax).st.get_x() / 10 ][ (* _ax).st.get_y() / 10 ];
if (_content != 0xFF)
{
if ((* _ax).st.all_targets())
report_map[ (* _ax).st.get_x() / 10 ][ (* _ax).st.get_y() / 10 ] = 0xFF;
else
report_map[ (* _ax).st.get_x() / 10 ][ (* _ax).st.get_y() / 10 ] = 0xFE;
}
++ _active_count;
}
std::cout << std::endl;
for (unsigned char y = 0; y < _cell_height; ++ y)
{
for (unsigned char x = 0; x < _cell_width; ++ x)
{
unsigned char _content = report_map[ x ][ y ];
switch (_content)
{
case 0:
std::cout << ' ';
break;
case 1:
std::cout << '#';
break;
case 2:
std::cout << 'O';
break;
case 0xFE:
std::cout << '*';
break;
case 0xFF:
std::cout << 'T';
break;
default:
std::cout << '+';
break;
}
}
std::cout << std::endl;
}
std::cout << "Iteration: " << _iter << " Count: " << _active_count << " State Count: " << state_count << std::endl;
}
}
return 0;
}
bool transition (unsigned char __impulse, const Active & __from)
{
Active _to(__from);
switch (__impulse)
{
case 0:
_to.st.set_vel_y(_to.st.get_vel_y() + 1);
break;
case 1:
_to.st.set_vel_x(_to.st.get_vel_x() - 2);
_to.st.set_vel_y(_to.st.get_vel_y() + 1);
break;
case 2:
_to.st.set_vel_y(_to.st.get_vel_y() - 1);
break;
case 3:
_to.st.set_vel_x(_to.st.get_vel_x() - 2);
_to.st.set_vel_y(_to.st.get_vel_y() - 1);
break;
case 4:
_to.st.set_vel_x(_to.st.get_vel_x() + 2);
_to.st.set_vel_y(_to.st.get_vel_y() + 1);
break;
case 6:
_to.st.set_vel_x(_to.st.get_vel_x() + 2);
_to.st.set_vel_y(_to.st.get_vel_y() - 1);
break;
}
short _new_x, _new_y;
bool _x_two_col, _y_two_col;
do
{
_new_x = _to.st.get_x() + _to.st.get_vel_x();
if (_new_x >= 0 && _new_x < _pixel_width)
{
_new_y = _to.st.get_y() + _to.st.get_vel_y();
if (_new_y >= 0 && _new_y < _pixel_height)
{
CellCoord _cell_pos;
_cell_pos.x = _new_x / 10;
if (_cell_pos.x < _cell_width)
{
_cell_pos.y = _new_y / 10;
if (_cell_pos.y < _cell_height)
{
unsigned char _cell_content = cell_map[_cell_pos.x][_cell_pos.y];
if (cell_map[_cell_pos.x][_cell_pos.y] != 1)
{
_x_two_col = ((_new_x % 10) != 0);
_y_two_col = ((_new_y % 10) != 0);
bool _collision = false;
if (_x_two_col)
{
++ _cell_pos.x;
if (cell_map[_cell_pos.x][_cell_pos.y] == 1)
_collision = true;
if (_y_two_col)
{
++ _cell_pos.y;
if (cell_map[_cell_pos.x][_cell_pos.y] == 1)
_collision = true;
-- _cell_pos.y;
}
-- _cell_pos.x;
}
if (_y_two_col)
{
++ _cell_pos.y;
if (cell_map[_cell_pos.x][_cell_pos.y] == 1)
_collision = true;
-- _cell_pos.y;
}
if (!_collision)
break;
}
}
}
}
}
_to.st.set_vel_x(_to.st.get_vel_x() / 2);
_to.st.set_vel_y(_to.st.get_vel_y() / 2);
}
while ( _to.st.has_vel() );
_new_x = _to.st.get_x() + _to.st.get_vel_x();
_new_y = _to.st.get_y() + _to.st.get_vel_y();
_x_two_col = ((_new_x % 10) != 0);
_y_two_col = ((_new_y % 10) != 0);
CellCoord _cell_pos;
_cell_pos.x = _new_x / 10;
_cell_pos.y = _new_y / 10;
_to.st.set_x(_new_x);
_to.st.set_y(_new_y);
bool _home = false;
unsigned char _cell_content = cell_map[_cell_pos.x][_cell_pos.y];
if (_cell_content == 2)
_home = true;
else if ( _cell_content > 2)
{
_to.st.set_target(_cell_content - 3);
}
if (_x_two_col)
{
++ _cell_pos.x;
_cell_content = cell_map[_cell_pos.x][_cell_pos.y];
if (_cell_content == 2)
_home = true;
else if ( _cell_content > 2)
{
_to.st.set_target(_cell_content - 3);
}
if (_y_two_col)
{
++ _cell_pos.y;
_cell_content = cell_map[_cell_pos.x][_cell_pos.y];
if (_cell_content == 2)
_home = true;
else if ( _cell_content > 2)
{
_to.st.set_target(_cell_content - 3);
}
-- _cell_pos.y;
}
-- _cell_pos.x;
}
if (_y_two_col)
{
++ _cell_pos.y;
_cell_content = cell_map[_cell_pos.x][_cell_pos.y];
if (_cell_content == 2)
_home = true;
else if ( _cell_content > 2)
{
_to.st.set_target(_cell_content - 3);
}
-- _cell_pos.y;
}
if (!is_set_state(_to.st))
{
set_state(_to.st);
++ state_count;
target_baseline &= _to.st.get_targets();
char _buf[2];
sprintf(_buf, "%d", (int)__impulse);
_to.path.append(_buf);
current_states.push_back(_to);
}
if (_home)
{
if (!_to.st.all_targets())
_home = false;
else
{
std::cout << "Solution: " << _to.path << std::endl;
}
}
return _home;
}
/*
Iteration: 146 Count: 809753 State Count: 62354880
Solution: 666662200200000002200220022002200220022002200220022000000000000000000000000011100000001000000000000333330020020002220002022200202020222222222024200000
0000000664000000400000000000006666600000022222220020000000020002222220000000000000022232222220202020202020213130202000000020000200220022002200220022002200220022
002000000020000
.
* TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT TT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT # #
###TTTTTTTTTTTTT##########TTTTTT### TT ##################
###TTTT############################# T ####### ####
##################################### ####### ##### ###
##################################### ##### #### ####
##################################### ##### #### ###
##################################### ##### ##### #
#####################################T##### ####
##################################TT ######## # ### #### #
###### ####TTTTTT ###################### ##
##### ##### TT ## ### ##
#### #### ####### ## #
##### + ########################## ###
############# ############### ** ###### ##
############## T T ** ######### ##
############## ############################# ####
############### ##################### #########
############### ################### #########
############### ################## ## ##
################ ####### ###
################ ###
################ ###################
################### #########################
###################### ##############################
######################## #########################
########################## ################## ##
###### ######## ##
######## ####
########## ######
############## ########
################### #######
###########################################################
###############################################################
Iteration: 147 Count: 786041 State Count: 62737960
Steps: 325, Fuel Usage: 146.
*/