Niflheimr is playing a game call "Maze", he can control the robot.
Available controls are as below:
key up: move forward
key left: turn left
key right: turn right
But the right key of his keyboard is broken, he has to turn left 3 times to turn right. (-270º is equal to +90º)
Maze will be a rectangle surrounded by walls, with 'start' and 'end' points.
Note that the robot is initially at 'start', and the initial facing direction is not determined (this will affect the answer to our question)!
Figure out the minimum times he should type his left key to reach the 'end'.
HINT:
here's some code for you to understand this problem. you can use it and implement the solve or rewrite all the code by yourself.
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
/// TypeDef
typedef pair<int, int> cor;
typedef int Direction;
/// ClassDef
class CorData {
public:
int x, y, dir;
CorData(int _x, int _y, int _dir) {
x = _x;
y = _y;
dir = _dir;
}
};
/// ConstDef
const int mazeMaxSize = 100;
const int dirSize = 4;
/// GlobalDef
int Maze[mazeMaxSize][mazeMaxSize];
int MazeBeen[mazeMaxSize][mazeMaxSize][dirSize]; /// 儲存走到該位置 , 面向
/// 的最小轉彎次數 , INF =1e9
queue<CorData> que, nextQue; /// the queue for BFS .
int movex[4] = {-1, 0, 1, 0}; /// 記錄DIR對應到的X移動方向 {上,左,下,右}
int movey[4] = {0, -1, 0, 1}; /// 記錄DIR對應到的Y移動方向 {上,左,下,右}
int x, y;
/// FunctionDef
inline int Turn(int i);
inline void printMazeBeen();
inline void printMaze();
inline void initMaze(); /// x ,y is global variable.
inline void loadMaze();
int solve();
/// MAIN Function
int main() {
int T;
int ans;
cin >> T;
while (T--) {
loadMaze();
// printMaze();
ans = solve();
cout << ans << "\n";
}
}
/// Inline function
inline int Turn(int i) { return (i + 1) % 4; }
/// for debugging
inline void printMazeBeen() {
for (int i = 0; i < 4; i++) {
cout << "============printMazeBeen==========\n";
for (int j = 0; j < x; j++) {
for (int k = 0; k < y; k++) {
if (MazeBeen[j][k][i] == int(1e9)) cout << "N ";
else cout << MazeBeen[j][k][i] << " ";
}
cout << "\n";
}
}
}
/// for debugging
inline void printMaze() {
cout << "=========printMaze===========\n";
for (int j = 0; j < x; j++) {
for (int k = 0; k < y; k++) cout << Maze[j][k] << " ";
cout << "\n";
}
}
inline void initMaze() { /// x ,y is global variable.
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
MazeBeen[i][j][0] = MazeBeen[i][j][1] = MazeBeen[i][j][2] = MazeBeen[i][j][3] = int(1e9); // let never been equal to 1e9(INF)
}
}
while (!que.empty()) que.pop();
while (!nextQue.empty()) nextQue.pop();
return;
}
inline void loadMaze() {
string str;
cin >> x >> y; /// input Maze x, y .
initMaze();
getline(cin, str);
for (int i = 0; i < x; i++) {
getline(cin, str);
for (int j = 0; j < y; j++) {
if (str[j] == '*') {
Maze[i][j] = -1;
} else if (str[j] == '.') {
Maze[i][j] = 0;
} else if (str[j] == 'S') {
Maze[i][j] = 1;
for (int d = 0; d < 4; d++) que.push(CorData(i, j, d));
} else if (str[j] == 'E') {
Maze[i][j] = 2;
}
}
}
}
int solve() {
/// TODO: implement this function so that the BFS will work.
}
first line will be an integer T means T cases below.
each testcase will have two integer X Y shows the size of Maze
there will be X lines , Y character each line.
for all testcases : 0 < X , Y < 31
for '*' means wall , barrier.
for '.' means path.
for 'S' means start.
for 'E' means end.
output should be one integer to show the minimal left button to finish the game.