首页 > 技术文章 > 程序设计思维与实践 Week2 作业 (1/2/智能班)

mopa 2020-03-01 18:45 原文

A - Maze


 

东东有一张地图,想通过地图找到妹纸。地图显示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹纸,这两个位置保证为0。既然已经知道了地图,那么东东找到妹纸就不难了,请你编一个程序,写出东东找到妹纸的最短路线。

Input

输入是一个5 × 5的二维数组,仅由0、1两数字组成,表示法阵地图。

Output

输出若干行,表示从左上角到右下角的最短路径依次经过的坐标,格式如样例所示。数据保证有唯一解。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4)

本题可利用BFS进行求解。从起点对不等于0的点进行扩张,扩张时可以向四个方向前进,并对已经访问过的点不再进行访问。

首先构建结构体用来存放输入的点的坐标。

typedef struct zb{
    int x;
    int y;
} zb;

 构建已访问数组,以及x,y扩张数组。

bool visited[6][6];
int dx[4] = {1,-1};
int dy[4] = {1,-1};

 

 在扩张的过程中,记录扩散的点是重点,可以在到达终点后对经过的点进行回溯

所以,构建二维数组u进行存储每一点的父节点

zb u[6][6];

在每次扩张时,都将u[i][j]更新为其父节点的坐标

这样只要记录最终点坐标即可

BFS过程如下:

while(1){
        n = q.front();
        if(n.x==5&&n.y==5) {
            out = n;
            break;
        }
        zb tmp;
        q.pop();
        visited[n.x][n.y] = true;
        tmp.y = n.y;
        for(int j=0;j<2;j++) {
            tmp.x = n.x + dx[j];
            if (visited[tmp.x][tmp.y]||a[tmp.x][tmp.y]==1) continue;
            if(tmp.x>=1&&tmp.x<=5&&tmp.y>=1&&tmp.y<=5){
                u[tmp.x][tmp.y] = n;
                q.push(tmp);
            }
        }
        tmp.x = n.x;
        for(int j=0;j<2;j++) {
            tmp.y = n.y + dy[j];
            if (visited[tmp.x][tmp.y]||a[tmp.x][tmp.y]==1) continue;;
            if(tmp.x>=1&&tmp.x<=5&&tmp.y>=1&&tmp.y<=5){
                u[tmp.x][tmp.y] = n;
                q.push(tmp);
            }
        }
    }
View Code

B - Pour Water


 

倒水问题 "fill A" 表示倒满A杯,"empty A"表示倒空A杯,"pour A B" 表示把A的水倒到B杯并且把B杯倒满或A倒空。

Input

输入包含多组数据。每组数据输入 A, B, C 数据范围 0 < A <= B 、C <= B <=1000 、A和B互质。

Output

你的程序的输出将由一系列的指令组成。这些输出行将导致任何一个罐子正好包含C单位的水。每组数据的最后一行输出应该是“success”。输出行从第1列开始,不应该有空行或任何尾随空格。

Sample Input

2 7 5
2 7 4

Sample Output

fill B
pour B A
success 
fill A
pour A B
fill A
pour A B
success

依旧是采用BFS方法,在A和B之间有六种操作,向A中灌满水,向B中灌满水,将A中水倒空,将B中水倒空,将B中水倒入A中(直到A满或B空),将A中水倒入B中(直到B满或A空)。

而扩张的即为对应的状态,例如初始状态A0B0,若A容量为x,B容量为y,则状态即为AmBn(0≤m≤x,0≤n≤y),以此作为一组进行扩张即可。

首先,构建结构体用来存储此状态,其中需要判断是否倒满来决定是否被倒水。

typedef struct p{
    int m;
    int n;
    int pa;
    p(){
        this->n = 0;
        this->m = 0;
    }
    p(int t){
        this->n = t;
        this->m = 0;
    }
    void f(p &B,int i){
        if(i==1){
            this->fill();

        }
        else if(i==2){
            this->fill(B);
        }
        else empty();
    }
    void setmn(p B){
        this->m = B.m;
        this->n = B.n;
    }
    void fill(p& B){
        if((this->m+B.m)<=(this->n)){
            (this->m) = (this->m) + B.m;
            B.m = 0;
        }
        else{
            B.m = B.m - (this->n)+(this->m);
            this->m = this->n;
        }
    }
    void fill(){
        this->m = n;
    }
    void empty(){
        this->m = 0;
    }
    void out(){
        cout<<this->m<<endl;
    }
}p;

BFS过程如下:

a = q.front().first;
            b = q.front().second;
            p ad=a,bd = b;
            if(a.m==z||b.m==z){
                out = q.front();
                break;
            }
            q.pop();
            p tmp;
            pair<int,int> cc = pair<int,int> (a.m,b.m);
            visited[cc] = true;
            for(int i = 1;i<=3;i++){
                b = bd;
                tmp.setmn(ad);
                tmp.f(b,i);
                pair<int,int> t = pair<int,int> (tmp.m,b.m);
                if(visited.find(t)==visited.end()){
                    myPair outmp = myPair(ad.m,bd.m,i);
                    parent[t] = outmp;
                    q.push(pair<p,p>(tmp,b));
                }
            }
            for(int i = 1;i<=3;i++){
                a = ad;
                tmp.setmn(bd);
                tmp.f(a,i);
                pair<int,int> t = pair<int,int> (a.m,tmp.m);
                if(visited.find(t)==visited.end()){
                    myPair outmp = myPair(ad.m,bd.m,-i);
                        parent[t] = outmp;
                    q.push(pair<p,p>(a,tmp));
                }
            }
View Code

在BFS过程中犯了一个错,在此记录:

在每次扩张时,必须将此状态初始化,或者用新变量代表当前状态,否则每次扩张都是在前一次扩张的基础上进行的。

将结果输出,通过在BFS过程中对每一次行动进行标号,采用-3,-2,-1,1,2,3对行为进行标号,将标号和当前状态存放入parent数组中,进行输出。

pair<int,int> opt;
        opt = pair<int,int> (out.first.m,out.second.m);
        int ip = parent[opt].op;
        stack<int> stack;
        while(!(opt.first==0&&opt.second==0)){
            stack.push(ip);
            opt = parent[opt].zb;
            ip = parent[opt].op;
        }
        while(!stack.empty()){
            int tn = stack.top();
            cout<<omp[tn]<<endl;
            stack.pop();
        }
        cout<<"success"<<endl;
View Code

 











推荐阅读