|
|
@@ -3,6 +3,7 @@
|
|
|
#include <string>
|
|
|
#include <queue>
|
|
|
#include <utility>
|
|
|
+#include <sstream>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
@@ -15,8 +16,8 @@ uint8_t the_matrix[500][500];
|
|
|
V3 V2 V1 V0 E3 E2 E1 E0
|
|
|
|
|
|
Vx: Visited in this phase, V0 is the first phase
|
|
|
-Ex: Edge in this direction, E0 is to the north during phase 0, E1 to the east during phase 0 etc
|
|
|
- E0 is to the east during phase 1, E1 to the south during phase 1 etc
|
|
|
+Ex: Edge in this direction, E0 is to the north during phase 0, E1 to the west during phase 0 etc
|
|
|
+ E0 is to the east during phase 1, E1 to the north during phase 1 etc
|
|
|
*/
|
|
|
|
|
|
struct Agent
|
|
|
@@ -29,9 +30,9 @@ struct Agent
|
|
|
enum Direction
|
|
|
{
|
|
|
NORTH,
|
|
|
- EAST,
|
|
|
+ WEST,
|
|
|
SOUTH,
|
|
|
- WEST
|
|
|
+ EAST,
|
|
|
};
|
|
|
|
|
|
bool check_dir(Agent smith, Direction dir)
|
|
|
@@ -45,44 +46,74 @@ int main()
|
|
|
int width, height, steps;
|
|
|
string str_in;
|
|
|
|
|
|
+ ostringstream debug_stream;
|
|
|
+
|
|
|
queue<Agent> search_queue;
|
|
|
|
|
|
- while(cin >> width >> height)
|
|
|
+ while(cin >> height >> width)
|
|
|
{
|
|
|
steps = 0;
|
|
|
|
|
|
cin.ignore(1000, '\n');
|
|
|
|
|
|
- cout << "Read width and height [" << width << "][" << height << "]\n";
|
|
|
+ debug_stream << "Read width and height [" << width << "][" << height << "]\n";
|
|
|
|
|
|
- for(int x = 0; x < width; x++)
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
{
|
|
|
- for(int y = 0; y < height; y++)
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
{
|
|
|
if(x == (width-1) && y == (height-1))
|
|
|
+ {
|
|
|
+ the_matrix[x][y] = 0;
|
|
|
continue;
|
|
|
+ }
|
|
|
|
|
|
getline(cin, str_in);
|
|
|
|
|
|
- cout << "Read line: " << str_in << endl;
|
|
|
+ debug_stream << "Read line: " << str_in << endl;
|
|
|
|
|
|
- the_matrix[x][y] = (str_in.find('N') != string::npos) ? 1 : 0;
|
|
|
- the_matrix[x][y] |= (str_in.find('E') != string::npos) ? 2 : 0;
|
|
|
- the_matrix[x][y] |= (str_in.find('S') != string::npos) ? 4 : 0;
|
|
|
- the_matrix[x][y] |= (str_in.find('W') != string::npos) ? 8 : 0;
|
|
|
+ the_matrix[x][y] = (str_in.find('N') != string::npos) ? (1<<NORTH) : 0;
|
|
|
+ the_matrix[x][y] |= (str_in.find('E') != string::npos) ? (1<<EAST) : 0;
|
|
|
+ the_matrix[x][y] |= (str_in.find('S') != string::npos) ? (1<<SOUTH) : 0;
|
|
|
+ the_matrix[x][y] |= (str_in.find('W') != string::npos) ? (1<<WEST) : 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- cout << "populated the graph" << endl;
|
|
|
+ debug_stream << "populated the graph" << endl;
|
|
|
for(int x = 0; x < width; x++)
|
|
|
{
|
|
|
for(int y = 0; y < height; y++)
|
|
|
{
|
|
|
- cout << x << ", " << y << ": 0x" << std::hex << (int)the_matrix[x][y] << endl;
|
|
|
+ debug_stream << x << ", " << y << ": 0x" << std::hex << (int)the_matrix[x][y] << endl;
|
|
|
+
|
|
|
+ int phase = 4;
|
|
|
+ if(check_dir(Agent{x,y,phase}, NORTH))
|
|
|
+ {
|
|
|
+ debug_stream << "Arrow to NORTH" << endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(check_dir(Agent{x,y,phase}, EAST))
|
|
|
+ {
|
|
|
+ debug_stream << "Arrow to EAST" << endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(check_dir(Agent{x,y,phase}, SOUTH))
|
|
|
+ {
|
|
|
+ debug_stream << "Arrow to SOUTH" << endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(check_dir(Agent{x,y,phase}, WEST))
|
|
|
+ {
|
|
|
+ debug_stream << "Arrow to WEST" << endl;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Start in upper right corner
|
|
|
+ // Clear queue
|
|
|
+ while(!search_queue.empty())
|
|
|
+ search_queue.pop();
|
|
|
+
|
|
|
+ // Start in upper right corner
|
|
|
search_queue.push(Agent{0,0,0});
|
|
|
|
|
|
// Bredden först
|
|
|
@@ -91,41 +122,56 @@ int main()
|
|
|
Agent smith = search_queue.front();
|
|
|
search_queue.pop();
|
|
|
|
|
|
+ debug_stream << "Searching [" << smith.x << "][" << smith.y << "] during phase " << smith.steps%4 << " (step " << smith.steps << ")\n";
|
|
|
+
|
|
|
if(smith.x == width-1 && smith.y == height-1)
|
|
|
{
|
|
|
- steps = smith.steps+1;
|
|
|
+ debug_stream << "Found exit!" << endl;
|
|
|
+ steps = smith.steps;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if(the_matrix[smith.x][smith.y] & (0x10 << ((smith.steps)%4)))
|
|
|
{
|
|
|
// Already visited
|
|
|
+ debug_stream << "Already visited" << endl;
|
|
|
continue;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ the_matrix[smith.x][smith.y] |= (0x10 << ((smith.steps)%4));
|
|
|
+ }
|
|
|
|
|
|
if(smith.y != 0 && check_dir(smith, NORTH))
|
|
|
{
|
|
|
+ debug_stream << "Possible to go NORTH" << endl;
|
|
|
search_queue.push(Agent{smith.x,smith.y-1,smith.steps+1});
|
|
|
}
|
|
|
|
|
|
if(smith.x != width-1 && check_dir(smith, EAST))
|
|
|
{
|
|
|
- search_queue.push(Agent{smith.x,smith.y+1,smith.steps+1});
|
|
|
+ debug_stream << "Possible to go EAST" << endl;
|
|
|
+ search_queue.push(Agent{smith.x+1,smith.y,smith.steps+1});
|
|
|
}
|
|
|
|
|
|
if(smith.y != height-1 && check_dir(smith, SOUTH))
|
|
|
{
|
|
|
+ debug_stream << "Possible to go SOUTH" << endl;
|
|
|
search_queue.push(Agent{smith.x,smith.y+1,smith.steps+1});
|
|
|
}
|
|
|
|
|
|
if(smith.x != 0 && check_dir(smith, WEST))
|
|
|
{
|
|
|
+ debug_stream << "Possible to go WEST" << endl;
|
|
|
search_queue.push(Agent{smith.x-1,smith.y,smith.steps+1});
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- cout << steps;
|
|
|
+ if(steps)
|
|
|
+ cout << steps << endl;
|
|
|
+ else
|
|
|
+ cout << "no path to exit" << endl;
|
|
|
|
|
|
}
|
|
|
|