存档

文章标签 ‘vector’

二维vector容器模拟迷宫,用回溯法走出迷宫

2010年4月9日 Yarkee 2 条评论

依然是老师布置的作业。题目为:

Write a backtracking program to find a way through a rectangle maze.
即用回溯法找出一条走出矩形迷宫的路径。
程序写得很粗糙,相当粗糙。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<vector>
#include"time.h"
using namespace std;
 
int flag=1;//由于迷宫可能有多个解,flag的作用就是用来控制,使输出的解只有一个
 
/*模拟的迷宫是有h*l个点的矩形,若矩形的某位置可以通行,则用'-'表示,某位置有障碍
,不可通行,用'*'表示,已走过的点用'0'表示
*/
 
void find_way(vector<vector<char>> &maze,int maze_i,int maze_j)//回溯法
{
        maze[maze_i][maze_j]='0';
        //若已达到出口,则输出已经被解出来的迷宫,及线路图
        if(maze_i==(maze.size()-1)&&maze_j==(maze[0].size()-1)){
            flag=0;
            cout<<"One solution of this maze is as follow: "<<endl;
            for(vector<vector<char>>::size_type i=0;i<maze.size();i++){
                for(vector<char>::size_type j=0;j<maze[0].size();j++){
                    cout<<maze[i][j]<<" ";
                }
                cout<<endl;
            }
            cout<<endl;
        }else{
        //如果下一个位置可以前进,则前进一步,然后递归调用fing_way函数
            if(maze_i>0&&maze[maze_i-1][maze_j]=='-'&&flag==1){
                find_way(maze,maze_i-1,maze_j);
            }
            if(maze_i<maze.size()-1&&maze[maze_i+1][maze_j]=='-'&&flag==1){
                find_way(maze,maze_i+1,maze_j);
            }
            if(maze_j>0&&maze[maze_i][maze_j-1]=='-'&&flag==1){
                find_way(maze,maze_i,maze_j-1);
            }
            if(maze_j<maze[0].size()-1&&maze[maze_i][maze_j+1]=='-'&&flag==1){
                find_way(maze,maze_i,maze_j+1);
            }
        }
        maze[maze_i][maze_j]='-';
}
 
int main()
{
    vector<vector<char>> maze;
    vector<char> temp;
    int h,l,t=1;
    srand((int)time(NULL));
    while(t!=0){
        cout<<"Input the height of the maze: ";
        cin>>h;
        cout<<"Input the length of the maze: ";
        cin>>l;
        //用二维的vector容器模拟一个迷宫,'-'表示可通行,'#'表示有障碍
        for(int i=0;i<h;i++){
            for(int j=0;j<l;j++){
                if((i==0&&j==0)||(i==h-1&&j==l-1)) temp.push_back('-');//保证入口和出口是可通行的
                else if(rand()%3){
                    temp.push_back('-');
                }
                else {
                    temp.push_back('#');
                }
            }
            maze.push_back(temp);
            temp.clear();
        }
        cout<<"The maze is as follow:"<<endl;
        for(int i=0;i<h;i++){
            for(int j=0;j<l;j++){
                cout<<maze[i][j]<<" ";
            }
            cout<<endl;
        }
        /*迷宫默认的入口位置为左上角,出口位置为右下角
          可以通过修改find_way的第二个和第三个参数,来修改入口位置
          通过修改find_way的第一个if语句来修改出口位置
        */
        cout<<endl;
        find_way(maze,0,0);
        //此时flag还等于0,证明迷宫无解
        if(flag==1) cout<<"Oh my god! No solution in this terrible maze!"<<endl<<endl;
        cout<<"Input 0 to quit this program,else to continue simulating."<<endl;
        //模拟完一次后,把二维的vector容器清空,flag置1,为下一次模拟做准备
        maze.clear();
        flag=1;
        cin>>t;
    }
    return 0;
}
分类: 编程 标签: , ,

vector容器的用法——摘自《C++Primer》

2010年3月17日 Yarkee 4 条评论

vector is a collection of objects of a single type, each of which has an associated integer index. As with strings, the library takes care of managing the memory associated with storing the elements. We speak of a vector as a container because it contains other objects. All of the objects in a container must have the same type. We’ll have much more to say about containers in Chapter 9.

vector 是同一种类型的对象的集合,每个对象都有一个对应的整数索引值。和 string 对象一样,标准库将负责管理与存储元素相关的内存。我们把 vector称为容器,是因为它可以包含其他对象。一个容器中的所有对象都必须是同一种类型的。我们将在第九章更详细地介绍容器。

To use a vector, we must include the appropriate header. In our examples, we also assume an appropriate using declaration is made:

使用 vector 之前,必须包含相应的头文件。本书给出的例子,都是假设已作了相应的 using 声明:

     #include <vector>
     using std::vector;


vector is a class template. Templates let us write a single class or function definition that can be used on a variety of types. Thus, we can define a vector that holds strings, or a vector to hold ints, or one to hold objects of our own class types, such as Sales_items. We’ll see how to define our own class templates in Chapter 16. Fortunately, we need to know very little about how templates are defined in order to use them.

vector 是一个类模板(class template)。使用模板可以编写一个类定义或函数定义,而用于多个不同的数据类型。因此,我们可以定义保存 string对象的 vector,或保存 int 值的 vector,又或是保存自定义的类类型对象(如 Sales_items 对象)的 vector。将在第十六章介绍如何定义程序员自己的类模板。幸运的是,使用类模板时只需要简单了解类模板是如何定义的就可以了。

To declare objects of a type generated from a class template, we must supply additional information. The nature of this information depends on the template. In the case of vector, we must say what type of objects the vector will contain. We specify the type by putting it between a pair of angle brackets following the template’s name:

声明从类模板产生的某种类型的对象,需要提供附加信息,信息的种类取决于模板。以 vector 为例,必须说明 vector 保存何种对象的类型,通过将类型放在类型放在类模板名称后面的尖括号中来指定类型:

     vector<int> ivec;               // ivec holds objects of type int
     vector<Sales_item> Sales_vec;   // holds Sales_items


As in any variable definition, we specify a type and a list of one or more variables. In the first of these definitions, the type is vector<int>, which is a vector that holds objects of type int. The name of the variable is ivec. In the second, we define Sales_vec to hold Sales_item objects.

和其他变量定义一样,定义 vector 对象要指定类型和一个变量的列表。上面的第一个定义,类型是 vector<int>,该类型即是含有若干 int 类型对象的vector,变量名为 ivec。第二个定义的变量名是 Sales_vec,它所保存的元素是 Sales_item 类型的对象。

vector is not a type; it is a template that we can use to define any number of types. Each of vector type specifies an element type. Hence, vector<int> and vector<string> are types.

vector 不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型。vector 类型的每一种都指定了其保存元素的类型。因此,vector<int> 和 vector<string> 都是数据类型。

阅读全文…

分类: 编程 标签: ,

WP SlimStat