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