p985.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <queue>
  5. #include <utility>
  6. using namespace std;
  7. // Serves to keep track of any map, each bit is mapped to something
  8. uint8_t the_matrix[500][500];
  9. /*
  10. V3 V2 V1 V0 E3 E2 E1 E0
  11. Vx: Visited in this phase, V0 is the first phase
  12. Ex: Edge in this direction, E0 is to the north during phase 0, E1 to the east during phase 0 etc
  13. E0 is to the east during phase 1, E1 to the south during phase 1 etc
  14. */
  15. struct Agent
  16. {
  17. int x;
  18. int y;
  19. int steps;
  20. };
  21. enum Direction
  22. {
  23. NORTH,
  24. EAST,
  25. SOUTH,
  26. WEST
  27. };
  28. bool check_dir(Agent smith, Direction dir)
  29. {
  30. return the_matrix[smith.x][smith.y] & (0x01 << ((dir+smith.steps)%4));
  31. }
  32. int main()
  33. {
  34. int width, height, steps;
  35. string str_in;
  36. queue<Agent> search_queue;
  37. while(cin >> width >> height)
  38. {
  39. steps = 0;
  40. cin.ignore(1000, '\n');
  41. cout << "Read width and height [" << width << "][" << height << "]\n";
  42. for(int x = 0; x < width; x++)
  43. {
  44. for(int y = 0; y < height; y++)
  45. {
  46. if(x == (width-1) && y == (height-1))
  47. continue;
  48. getline(cin, str_in);
  49. cout << "Read line: " << str_in << endl;
  50. the_matrix[x][y] = (str_in.find('N') != string::npos) ? 1 : 0;
  51. the_matrix[x][y] |= (str_in.find('E') != string::npos) ? 2 : 0;
  52. the_matrix[x][y] |= (str_in.find('S') != string::npos) ? 4 : 0;
  53. the_matrix[x][y] |= (str_in.find('W') != string::npos) ? 8 : 0;
  54. }
  55. }
  56. cout << "populated the graph" << endl;
  57. for(int x = 0; x < width; x++)
  58. {
  59. for(int y = 0; y < height; y++)
  60. {
  61. cout << x << ", " << y << ": 0x" << std::hex << (int)the_matrix[x][y] << endl;
  62. }
  63. }
  64. // Start in upper right corner
  65. search_queue.push(Agent{0,0,0});
  66. // Bredden först
  67. while(!search_queue.empty())
  68. {
  69. Agent smith = search_queue.front();
  70. search_queue.pop();
  71. if(smith.x == width-1 && smith.y == height-1)
  72. {
  73. steps = smith.steps+1;
  74. break;
  75. }
  76. if(the_matrix[smith.x][smith.y] & (0x10 << ((smith.steps)%4)))
  77. {
  78. // Already visited
  79. continue;
  80. }
  81. if(smith.y != 0 && check_dir(smith, NORTH))
  82. {
  83. search_queue.push(Agent{smith.x,smith.y-1,smith.steps+1});
  84. }
  85. if(smith.x != width-1 && check_dir(smith, EAST))
  86. {
  87. search_queue.push(Agent{smith.x,smith.y+1,smith.steps+1});
  88. }
  89. if(smith.y != height-1 && check_dir(smith, SOUTH))
  90. {
  91. search_queue.push(Agent{smith.x,smith.y+1,smith.steps+1});
  92. }
  93. if(smith.x != 0 && check_dir(smith, WEST))
  94. {
  95. search_queue.push(Agent{smith.x-1,smith.y,smith.steps+1});
  96. }
  97. }
  98. cout << steps;
  99. }
  100. return 0;
  101. }