程序设计思维 week2 作业题

程序设计思维作业 week2

本次作业的两个题都是BFS的典型应用。

Problem A :Maze

1.题目概述

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

2.Input

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

3.Output

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

4.Sample

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)

5.整体思路

思路比较简单,利用栈来模拟BFS过程,将成功首先到达终点的一支保存在栈中,然后利用另一个栈来进行倒序输出。

6.代码

#include<iostream>
#include<stack>
using namespace std;
//记录点的坐标
class point
{
public:
    point(int x,int y)
    {
        _x=x;
        _y=y;
    }
    void output()
    {
        cout<<'('<<_x-1<<','<<' '<<_y-1<<')'<<endl;
    }
public:
    int _x;
    int _y;

};

int _dx[]={1,0,0,-1};
int _dy[]={0,-1,1,0};
stack<point> track;
int a[7][7];
int main()
{
    //初始化
    for(int i=0;i<7;i++)
    {
        for(int j=0;j<7;j++)
        {
            if((i*j==0)||i==6||j==6)
                a[i][j]=1;
            else cin>>a[i][j];
        }
    }
    //输入起点
    point start(1,1);
    point terminate(5,5);
    track.push(start);
    //判断终点条件
    while(!(track.top()._x==5&&track.top()._y==5))
    {
        point now=track.top();
        a[now._x][now._y]=1;
        int i;
        //BFS过程
        for(i=0;i<4;i++)
        {
            int new_x=now._x+_dx[i];
            int new_y=now._y+_dy[i];
            if(a[new_x][new_y]!=1)
            {
                a[new_x][new_y]=1;
                track.push({new_x, new_y});
                break;
            }
        }
        if(i==4) track.pop();
    }
    int size=track.size();
    stack<point> newtrack;
    for(int k=0;k<size;k++)
    {
        point n(track.top()._x,track.top()._y);
        track.pop();
        newtrack.push(n);
    }
    for(int i=0;i<size;i++)
    {
        newtrack.top().output();
        newtrack.pop();
    }
    return 0;
}

Problem B :Pour Water

1.题目概述

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

2. Sample inout and output

Input

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

Sample Input
2 7 5
2 7 4

Output

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

Sample Output
fill B
pour B A
success 
fill A
pour A B
fill A
pour A B
success

3.整体思路及代码

本题还是BFS的应用,将problem A 中的上下左右移动外扩操作替换为这里的倒满,倒空,A(B)倒给B(A)。通过map的使用可以记录一种状态的前一种状态的情况。然后根据两种状态之间的差别来确定这一步具体是做了怎么的操作。

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

struct Status
{
    int a, b;
    bool operator<(const Status &s) const
    {
        return a!=s.a ? a<s.a : b<s.b;
    }
};

/* 递归输出方案 */
void print(Status &p,map<Status, Status> from)
{
    if ( from.find(p) == from.end())
    {
        //cout<<"success"<<endl;
        return ;
    }
    if(p.a == 0&&p.b==0)
    {
        return ;
    }
    print(from[p],from); // 递归
    int _a=from[p].a;
    int _b=from[p].b;
    if(p.a!=_a)
    {
        if(p.a>_a)
        {
            if(p.b<_b)
                cout<<"pour B A"<<endl;
            else
                cout<<"fill A"<<endl;
        } else{
            if(p.b>_b)
                cout<<"pour A B"<<endl;
            else
                cout<<"empty A"<<endl;
        }
    } else{
        //p.a==_a
        if(p.b>_b)
            cout<<"fill B"<<endl;
        else
            cout<<"empty B"<<endl;
    }
    //printf("-><%d,%d>",  p.a, p.b);
}

void refresh(Status &s, Status &t,map<Status, Status> &from,queue<Status> &Q)
{
    if ( from.find(t) == from.end() )
    { // 特判合法,加入队列
        from[t] = s;
        Q.push(t);
    }
}

void bfs(int A, int B, int C,map<Status, Status> from,queue<Status> &Q)
{
    // 起点, 两杯水都空
    Status s,t;
    s.a=0; s.b=0;
    Q.push(s);

    while (!Q.empty())
    {
        // 取队首
        s = Q.front();
        Q.pop();
        // 特判到达终点
        if (s.a == C || s.b == C) {
            print(s,from); // 输出方案
            return;
        }

        // 倒空 a 杯的水
        if (s.a > 0) {
            t.a = 0;  // 倒空
            t.b = s.b;// b 杯不变
            refresh(s, t,from,Q);
        }

        // 同理,倒空 b 杯的水
        if (s.b > 0) {
            t.b = 0;  // 倒空
            t.a = s.a;// a 杯不变
            refresh(s, t,from,Q);
        }

        // a 杯未满,续满 a 杯
        if (s.a < A)
        {
            // 续满 a 杯
            t.a = A;
            t.b = s.b;
            refresh(s, t,from,Q);

            // 考虑倒入
            if (s.b != 0)
            {
                if (s.a + s.b <= A)
                {
                    t.a = s.a + s.b;
                    t.b = 0;
                    refresh(s, t,from,Q);
                } else
                {
                    t.a = A;
                    t.b = s.a + s.b - A;
                    refresh(s, t,from,Q);
                }
            }
        }

        // 同理,b 杯未满,续满 b 杯
        if (s.b < B)
        {
            t.a = s.a;
            t.b = B;
            refresh(s, t,from,Q);
            if (s.a != 0)
            {
                if (s.a + s.b <= B)
                {
                    t.a = 0;
                    t.b = s.a + s.b;
                    refresh(s, t,from,Q);
                } else
                {
                    t.a = s.a + s.b - B;
                    t.b = B;
                    refresh(s, t,from,Q);
                }
            }
        }
    }
    printf("-1\n");
}
int main()
{
    int a;
    while (cin>>a) {
        queue<Status> Q;
        map<Status, Status> from;
        int b,c;
        cin>>b;
        cin>>c;
        bfs(a, b, c,from,Q);
        cout<<"success"<<endl;
    }
    return 0;
}
发布了8 篇原创文章 · 获赞 2 · 访问量 252

猜你喜欢

转载自blog.csdn.net/lawrenceY/article/details/104688670