thought works 筆試 (生成迷宮)

thought works 筆試作業 

用計算機生成迷宮是一個很有趣的任務。我們可以用 ​道路網格(Road Grid)​ ​來表示迷宮的道路,那麼 3 x 3的 ​道路網格​(​圖-1 左​)可以對應一個 7 x 7 的 ​渲染網格(Render Grid)​ 

如果我們將迷宮 ​道路網格​ 兩個相鄰的 ​cell​ 連通,則可以打通道路

連通​道路網格​有如下的約束條件:
● 每一個 ​cell​ 只能夠直接與相鄰正南、正北、正東、正西的 ​cell​ 連通。不能夠和其他的 ​cell​ 連通。
● 兩個 ​cell​ 之間的連通一定是雙向的。即 ​cell(0,0) ​和 ​cell(1,0)​ 連通等價於 ​cell(1,0)​ 和cell(0,0)​ 的連通


要求1:將迷宮渲染爲字符串
現在我們希望你書寫程序,將給定迷宮的 ​道路網格​,渲染爲字符串輸出。例如,其使用方式如下(僞代碼,僅做演示,實際實現時請應用實際語言的編程風格)
Maze maze = MazeFactory.Create(command);
String mazeText = maze.Render();
其中 command 是一個字符串。它的定義如下:
● 第一行是迷宮 ​道路網格​ 的尺寸。例如 3 x 3 的迷宮爲 ​3 3​,而 5 x 4 的迷宮爲 ​5 4​(5 行 4 列)​。
● 第二行是迷宮 ​道路網格​ 的連通性定義。如果 ​cell(0,1) ​和​ cell(0,2) ​是連通的,則表示爲:0,1 0,2​,多個連通以分號 ​; ​隔開。
例如,如果給定輸入:
3 3
0,1 0,2;0,0 1,0;0,1 1,1;0,2 1,2;1,0 1,1;1,1 1,2;1,1 2,1;1,2 2,2;2,0 2,1
則輸出字符串爲(如果當前 渲染網格 爲牆壁,則輸出 [W] 如果爲道路則輸出 [R]):
[W] [W] [W] [W] [W] [W] [W]
[W] [R] [W] [R] [R] [R] [W]
[W] [R] [W] [R] [W] [R] [W]
[W] [R] [R] [R] [R] [R] [W]
[W] [W] [W] [R] [W] [R] [W]
[W] [R] [R] [R] [W] [R] [W]
[W] [W] [W] [W] [W] [W] [W]
要求2:檢查輸入的有效性
在處理輸入的時候需要檢查輸入的有效性。需要檢查的有效性包括如下的幾個方面:
● 無效的數字:輸入的字符串無法正確的轉換爲數字。此時,該函數的輸出爲字符串 ​”Invalid
number format​.​”
● 數字超出預定範圍:數字超出了允許的範圍,例如爲負數等。此時,該函數的輸出爲字符串
”Number out of range​.​”
● 格式錯誤:輸入命令的格式不符合約定。此時,該函數的輸出爲字符串​ ”Incorrect command
format​.​”
● 連通性錯誤:如果兩個網格無法連通,則屬於這種錯誤。此時,該函數的輸出爲字符串​ ”Maze
format error.”
當多個問題同時出現時,報告其中一個錯誤即可。
--------------------- 
作者:attitude_yu 
來源:CSDN 
原文:https://blog.csdn.net/attitude_yu/article/details/81327869 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

C++

#include <iostream>
#include<vector>
#include<string>

using namespace std;


int main(){
	vector<int> Info1(2);
	//輸入行和列
	cout << "input rows and cols:" << endl;
	for (int i = 0; i <= 1; i++){
		cin >> Info1[i];
		if (Info1[i] <= 0){
			cout << "Number out of range" << endl;
			return 0;
		}
	}
	if (Info1.size() != 2){
		cout << "Incorrect commad format" << endl;
		return 0;
	}
	int rows = Info1[0];
	int cols = Info1[1];
	vector<vector<int>> map(50, vector<int>(50));
	//int map[1000][1000];
	for (int i = 0; i < (rows * 2 + 1); i++){
		for (int j = 0; j < (cols * 2 + 1); j++){
			map[2 * i + 1][2 * j + 1] = 1;
		}
	}
	//輸入字符串
	cout << "input cells:" << endl;
	string string0;
	vector<string> str(50);
	int index = 0;
	cin.ignore();
	getline(cin, string0);
	//cout << string0 << endl;
	for (int i = 0; i < string0.length(); i++){
		if (string0[i] != ';'){
			str[index].push_back(string0[i]);
		}
		else{
			index++;
			continue;
		}
	}

	for (int i = 0; i <= index; i++){
		vector<string> xy(5);
		int num = 0;
		for (int j = 0; j < str[i].size(); j++){
			if (str[i][j] != ',' && str[i][j] != ' '){
				if (str[i][j] >= '0'&&str[i][j] <= '9'){
					xy[num].push_back(str[i][j]);
				}
				else{
					cout << "Invalid number format" << endl;
					return 0;
				}
			}
			else{
				num++;
				continue;
			}
		}
		int x0 = atoi(xy[0].c_str());
		int y0 = atoi(xy[1].c_str());
		int x1 = atoi(xy[2].c_str());
		int y1 = atoi(xy[3].c_str());
		if ((abs(x0 - x1) == 0 && abs(y0 - y1) == 1) || (abs(x0 - x1) == 1 && abs(y0 - y1) == 0)){
			if (x0 == x1)
				map[2 * x0 + 1][y0 + y1 + 1] = 1;
			else if (y0 == y1)
				map[x0 + x1 + 1][2 * y0 + 1] = 1;

		}
		else{
			cout << "Maze format error" << endl;
			return 0;
		}
	}

	//顯示結果;
	string mapDisplay;
	for (int i = 0; i < 2 * rows + 1; i++){
		//string mapDisplay;
		for (int j = 0; j < 2 * cols + 1; j++){
			if (map[i][j] == 1)
				mapDisplay.append("[R] ");
			else
				mapDisplay.append("[W] ");
		}
		cout << mapDisplay << endl;
		mapDisplay.clear();
	}

	return 0;
}

 本人輸入寫慣了用cin,自己寫的時候最好使用測試用例的方法,進入面試現場碼代碼的時候用cin難度會增加。

具體代碼和說明文檔下載鏈接:https://download.csdn.net/download/m0_38090566/10895181

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章