這是一道 enum 跟 class 的練習題。
你正試圖開發一個簡單的迷宮導航系統。這個迷宮是一個 5x5 的固定地圖,其中包含牆壁(#)、起點(S)、終點(E),以及可以行走的空格(.)。地圖裡面有一個角色,最一開始會站在 $(1,1)$ 的位置。終點在 $(3,1)$ 的位置。地圖的樣子請見下方的程式碼。
現在已經定義好了一個 class 叫做 Map
,以及一個表示方向的 enum Direction
。現在,請你完成兩個函式,讓角色從起點出發,依照方向移動,最後抵達終點。
你要完成的兩個 function 分別為:
void Map::move(Direction dir)
Direction::North
,則 $x$ 減 1(向上)。如果是 Direction::South
,則 $x$ 加 1(向下)。如果是 Direction::East
,則 $y$ 加 1(向右)。如果是 Direction::West
,則 $y$ 減 1(向左)。vector<Direction> getDirections()
你需要在你的程式碼中的第一行寫 #include "lib1047.h"
。並且,你的程式碼當中只需要實作指定的兩個函式。
我們會把你的程式碼放在以下程式碼當中指定的位置:
#include<iostream> #include<vector> using namespace std; enum Direction { North, South, East, West }; class Map { public: void move(Direction dir); bool checkValid() { if (x < 0 || x >= 5 || y < 0 || y >= 5) return false; if (map[x][y] == '#') return false; return true; } bool isExit() { return x == finalX && y == finalY; } Map() { x = 1; y = 1; map[0] = "#####"; map[1] = "#S..#"; map[2] = "###.#"; map[3] = "#E..#"; map[4] = "#####"; } private: int x, y; int finalX = 3, finalY = 1; string map[5]; }; vector<Direction> getDirections(); /* TODO: YOUR CODE WILL GO HERE */ int main() { Map map; vector<Direction> directions = getDirections(); bool bad = false; for (Direction dir : directions) { map.move(dir); if (!map.checkValid()) { cout << "Invalid move!" << endl; return 0; } } if (!bad && map.isExit()) { cout << "Redacted" << endl; } else { cout << "Did not find exit!" << endl; } }
無
請完成指定的兩個函式
以下是一個不會 AC 的模版:
#include "lib1047.h"
void Map::move(Direction dir) {
// TODO: Write your code here
}
vector<Direction> getDirections() {
// TODO: Fill in results vector correctly
vector<Direction> results = { ... };
return results;
}
No. | Testdata Range | Score |
---|---|---|
1 | 0 | 100 |