TopCoder

User's AC Ratio

100.0% (1/1)

Submission's AC Ratio

16.7% (1/6)

Tags

Description

這是一道 enum 跟 class 的練習題。

你正試圖開發一個簡單的迷宮導航系統。這個迷宮是一個 5x5 的固定地圖,其中包含牆壁(#)、起點(S)、終點(E),以及可以行走的空格(.)。地圖裡面有一個角色,最一開始會站在 $(1,1)$ 的位置。終點在 $(3,1)$ 的位置。地圖的樣子請見下方的程式碼。

現在已經定義好了一個 class 叫做 Map,以及一個表示方向的 enum Direction。現在,請你完成兩個函式,讓角色從起點出發,依照方向移動,最後抵達終點。

你要完成的兩個 function 分別為:

  1. void Map::move(Direction dir)
    • 功能:根據傳入的方向 dir 移動角色在地圖上的位置。
    • 規則:如果 dir 是 Direction::North,則 $x$ 減 1(向上)。如果是 Direction::South,則 $x$ 加 1(向下)。如果是 Direction::East,則 $y$ 加 1(向右)。如果是 Direction::West,則 $y$ 減 1(向左)。
  2. vector<Direction> getDirections()
    • 功能:回傳一組移動路徑(方向的序列),讓角色可以從起點 S 成功走到終點 E。
    • 規則:這個函式回傳的 vector 裡面每個方向都會被依序執行。每次移動後,系統會檢查該位置是否「合法」(過程中沒有撞牆),你需要回傳一組序列使得角色可以在不撞牆的情況下走到終點 $(3,1)$。

你需要在你的程式碼中的第一行寫 #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;
    }
}

Input Format

Output Format

請完成指定的兩個函式

Hints

以下是一個不會 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;
}

Problem Source

Subtasks

No. Testdata Range Score
1 0 100

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 65536 1