程序设计思维与实践 Week2 作业

A Maze

1.题目大意

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

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

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

样例:

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
(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)

HINT:
1.坐标(x, y)表示第x行第y列,行、列的编号从0开始,且以左上角为原点。
2.另外注意,输出中分隔坐标的逗号后面应当有一个空格。

2.解题思路

  • 这道题主要考虑bfs找到路径,并且沿终点返回起点的顺序将路径倒序输出。
  • 使用队列进行bfs,寻找从起点开始可到达的点:
  1. 队列非空时,每次取出队首点处理,遍历其上下左右四个方向,将未到达过且未超过边界且未碰到障碍的点加入队列
  2. 每次加入需要更新该点与起点的距离和该点的前驱节点(便于倒序输出)
  3. 若到达终点,则返回转去输出路径
  • 使用栈进行倒序输出:
    从终点开始,每次将当前顶点的前驱顶点压栈,最后依次弹出顶点并输出即为起点到终点的顺序

3.代码

#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;

struct Point
{
    int x, y;
    Point(){}
    Point(int theX, int theY)
    {
        x = theX;
        y = theY;
    }
}pre[5][5];

int maze[5][5], dis[5][5], n=5;
const int sx=0, sy=0, tx=4, ty=4;
queue<Point> points;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

void bfs()
{
    while (!points.empty()) points.pop();
    memset(dis, -1, sizeof(dis));
    dis[sx][sy] = 0;
    Point start(sx, sy);
    points.push(start);
    while (!points.empty())
    {
        Point now = points.front();
        points.pop();
        for (int i = 0; i < 4; i++)
        {
            int x = now.x + dx[i];
            int y = now.y + dy[i];
            if (x >= 0 && x < n && y >= 0 && y < n && dis[x][y] == -1 && maze[x][y]==0)
            {
                dis[x][y] = dis[now.x][now.y] + 1;
                pre[x][y] = Point(now.x, now.y);
                Point next(x, y);
                points.push(next);
                if (x == tx && y == ty)
                    return;
            }
        }
    }
}

int main()
{
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            cin >> maze[i][j];
    
    bfs();
    stack<Point> out;
    int nowX = tx, nowY = ty;
    
    while (true)
    {
        Point now(nowX, nowY);
        out.push(now);
        if (nowX == 0 && nowY == 0)
            break;
        nowX = pre[now.x][now.y].x;
        nowY = pre[now.x][now.y].y;
    }
    while (!out.empty())
    {
        Point output = out.top();
        out.pop();
        cout<<"("<<output.x<<", "<<output.y<<")"<<endl;
    }
    
    return 0;
}

4.实现细节

  • 创建Point类表示每个点的横纵坐标及初始化方法,创建Point类型的二维数组表示每个点 (i,j) 的前驱节点为 pre[i][j]。
  • bfs函数为中心思想,实现并不太复杂,严格按照解题思路中的“取出处理、合法加入”不断进行即可。
    (经过优化的一处是把表示是否到达的数组合并入表示距离的数组dis[][],注意要对其进行初始化,否则默认填充的数很大,无法加入队列)
  • 考虑了比较久的是倒序输出,虽然思路很明确,需要标记前驱节点并利用栈来解决,但是如何记录前驱节点的细节实现遇到了一些问题。
  • 原来是考虑在Point类里加入preX和preY的变量,但是在bfs里加入队列时表现为无法加入
    (推测是Point now.preX = xxx的复制构造问题,现在觉得如果改为Point next(x, y, preX, preY)应该可以实现)

B Pour Water

1.题目大意

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

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

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

样例:

2 7 5
2 7 4
fill B
pour B A
success 
fill A
pour A B
fill A
pour A B
success

2.解题思路

  • 这道题主要考虑把倒水这个隐式图问题转化为“显式”的可以用bfs思想解决的问题。
  • 理解这个隐式图问题的类比方法:
  1. 当前A/B杯中水的容量 -> 图中当前点的坐标
  2. 杯子中水的转移方式(A倒入B、B倒入A、A倒空/满、B倒空/满) -> 图中点的移动方向(上下左右)
  • 使用队列进行bfs,寻找从起始状态开始可到达的状态:
  1. 队列非空时,每次取出队首状态处理,遍历六种倒水情况,将未到达过的状态加入队列
  2. 若到达目标状态,则返回转去输出路径
  • 使用栈进行倒序输出:
    从目标状态开始,每次将当前状态的前驱状态压栈,最后依次弹出元素并输出状态,即为到达目标状态的一系列指令

3.代码

#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;

struct Status
{
    int a, b;
    string operation;
    bool operator < (const Status &status) const
    {
        if (a != status.a)
            return a < status.a;
        return b < status.b;
    }
};

queue<Status> bottle;
map<Status, Status> pre;

void output(Status &status)
{
    if (pre.find(status) == pre.end() || (status.a == 0 && status.b == 0))
    {
        cout << status.operation;
        return;
    }
    output(pre[status]);
    cout << status.operation << endl;
}

void refresh(Status &s, Status &t)
{
    if (pre.find(t) == pre.end())//发现未曾出现t状态
    {
        pre[t] = s;//标记t状态从s状态而来
        bottle.push(t);
    }
}

void bfs(int A, int B, int C)
{
    Status s, t;
    s.a = 0;
    s.b = 0;
    bottle.push(s);
    while (!bottle.empty())
    {
        s = bottle.front();
        bottle.pop();
        if (s.a == C || s.b == C)//到达终点
        {
            output(s);
            cout << "success" << endl;
            return;
        }
        
        if (s.a > 0)//倒空a杯的水
        {
            t.a = 0;
            t.b = s.b;
            t.operation = "empty A";
            refresh(s, t);
        }
        if (s.b > 0)//倒空b杯的水
        {
            t.b = 0;
            t.a = s.a;
            t.operation = "empty B";
            refresh(s, t);
        }
        if (s.a < A)//当a杯未满时,蓄满a杯
        {
            t.a = A;
            t.b = s.b;//b杯不变
            t.operation = "fill A";
            refresh(s, t);
            //考虑ab之间倒水关系
            if (s.b != 0)
            {
                if (s.a + s.b <= A)//若a杯够装,把b杯水全倒入a杯
                {
                    t.a = s.a + s.b;
                    t.b = 0;
                    t.operation = "pour B A";
                    refresh(s, t);
                }
                else//若a杯不够装,把a杯倒满,剩下的留在b杯
                {
                    t.a = A;
                    t.b = s.a + s.b - A;
                    t.operation = "pour B A";
                    refresh(s, t);
                }
            }
        }
        if (s.b < B)//同理,当b杯未满时,蓄满b杯
        {
            t.a = s.a;
            t.b = B;
            t.operation = "fill B";
            refresh(s, t);
            if (s.a != 0)
            {
                if (s.a + s.b <= B)
                {
                    t.a = 0;
                    t.b = s.a + s.b;
                    t.operation = "pour A B";
                    refresh(s, t);
                }
                else
                {
                    t.a = s.a + s.b - B;
                    t.b = B;
                    t.operation = "pour A B";
                    refresh(s, t);
                }
            }
        }
    }
}
            
int main()
{
    int A, B, C;
    while (cin >> A >> B >> C)
    {
        pre.clear();
        while (!bottle.empty()) bottle.pop();
        bfs(A, B, C);
    }
    return 0;
}

4.实现细节

  • 创建Status类表示每个状态中A杯和B杯水的容量及前驱状态,创建map<Status, Status> pre表示每个状态status的前驱状态为pre.find(status)。
  • bfs函数为中心思想,其中的转换状态部分需要对A杯和B杯进行完全相同的讨论,因此代码比较长,但是逻辑思想不难懂。
    (refresh函数判断该状态是否出现,若未出现则加入队列,并记录前驱状态,该函数是从bfs中提取出来的,提高了代码整洁度)
  • 这里的倒序输出和作业A题相似,先用循环压栈放入,再循环弹栈输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值