Week2 作业

A-Maze

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

Input

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

Output

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

Example 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

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

Hint

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

题意:
在一个五乘五的地图中,找出从左上角到右下角的最短路径,注意只有标记为0的点才可以通过。

分析:
这是一道走迷宫问题,通常使用队列,将要扩展的点加入队列,扩展后移出队列,当队列为空时就遍历完全。首先从起点开始,检查是否能够到达上下左右四个点(即没有到达边界,没有障碍物,没有被标记过),对每次走过的点进行标记,如此不断循环直到到达终点。结构体point代表地图中的某一个点,map<point,point> pass用来记录通过的路径,然后用递归法输出。

代码如下:

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

struct point
{
	int x, y;
	bool operator < (const point& a) const
	{//重载比较方法
		return x == a.x ? y < a.y : x < a.x;
	}
};

map<point, point> pass;

void output(point& a)
{//递归输出路径
	if (pass.find(a) == pass.end())
	{
		cout << "(" << a.x << ", " << a.y << ")" << endl;
		return;
	}
	output(pass[a]);
	cout << "(" << a.x << ", " << a.y << ")" << endl;
	return;
}

int main()
{
	int m[5][5];
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			cin >> m[i][j];
		}
	}
	point start; 
	start.x = 0; 
	start.y = 0;
	queue<point> q;
	q.push(start);
	while (!q.empty())
	{
		point pre = q.front();
		q.pop();
		//判断终点
		if (pre.x == 4 && pre.y == 4)
		{
			output(pre);
		}
		for (int i = 0; i < 4; i++)
		{
			int move_x, move_y;
			switch (i)
			{
			case 0:
				move_x = 1;
				move_y = 0;
				break;
			case 1:
				move_x = -1;
				move_y = 0;
				break;
			case 2:
				move_x = 0;
				move_y = 1;
				break;
			case 3:
				move_x = 0;
				move_y = -1;
				break;
			}
			int pre_x = pre.x + move_x;
			int pre_y = pre.y + move_y;
			point next;
			next.x = pre_x;
			next.y = pre_y;
			if (pre_x == 0 && pre_y == 0)
				break;
			if (pre_x >= 0 && pre_x <= 4 && pre_y >= 0 && pre_y <= 4 &&
				m[pre_x][pre_y] == 0 && pass.find(next) == pass.end())
			{
				pass[next] = pre;
				q.push(next);
			}
		}
	}
	return 0;
}

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列开始,不应该有空行或任何尾随空格。

Example Input

2 7 5
2 7 4

Example Output

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

Notes

如果你的输出与Sample Output不同,那没关系。对于某个"A B C"本题的答案是多解的,不能通过标准的文本对比来判定你程序的正确与否。 所以本题由 SPJ(Special Judge)程序来判定你写的代码是否正确。

题意:
有两个固定容积的杯子,通过给出的6种操作,在任意一个杯子中剩余所给量的水。

分析:
这道题目中,一共有6种操作,假如两个杯子的最大容量分别为A和B,杯子中目前的水量为a和b,则每个操作过后a和b的变化如下表:

操作A杯中水量B杯中水量
fill Aa=Ab=b
fill Ba=ab=B
empty Aa=0b=b
empty Ba=ab=0
pour A Bif(a+b<B) a=0;else a=a+b-Bif(a+b<B) b=a+b;else b=B
pour B Aif(a+b<A) a=a+b;else a=Aif(a+b<A) b=0;else b=a+b-A

所以只要不断重复倒空A,倒空B,倒满A和倒满B这些操作,只要有一个桶中的水量变为C则停止循环,而在倒满A和倒满B操作中,需要考虑桶中能否装得下,进行分类讨论,使用map保存每次变化前后的两种状态,使用队列让状态不断进队出队,bfs解决问题,最后用递归输出所有的操作。

代码如下:

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

struct status
{
	int a, b;//两个水杯中剩余的水量
	string operate;//倒水操作
	bool operator<(const status& m) const
	{//重载操作符 
		return a != m.a ? a < m.a : b < m.b;
	}
};

queue<status> q;
map<status, status> mp;

void check(status before, status after)
{
    if (mp.find(after) == mp.end())
    {
        mp[after] = before;
        q.push(after);
    }
}

void output(status m)
{//输出操作 
    if (m.operate == "start")
    {
        return;
    }
    output(mp[m]);
    cout << m.operate << endl;
}

int main()
{
	int A, B, C;
	while (cin >> A >> B >> C)
	{
        mp.clear();//清空map
        while (!q.empty())//清空queue
            q.pop();
        status before, after;//操作前后的两种状态
        before.a = 0;
        before.b = 0;
        before.operate = "start";
        q.push(before);
        while (!q.empty())
        {
            before = q.front();
            q.pop();
            if (before.a > 0)
            {//倒空A
                after.a = 0;
                after.b = before.b;
                after.operate = "empty A";
                check(before, after);
            }
            if (before.b > 0)
            {//倒空B 
                after.b = 0;
                after.a = before.a;
                after.operate = "empty B";
                check(before, after);
            }
            if (before.a < A)
            {//倒满A 
                after.a = A;
                after.b = before.b;
                after.operate = "fill A";
                check(before, after);
                if (before.b != 0)
                {
                    if (before.a + before.b <= A)
                    {//A能装下
                        after.a = before.a + before.b;
                        after.b = 0;
                        after.operate = "pour B A";
                        check(before, after);
                    }
                    else
                    {//A装不下
                        after.a = A;
                        after.b = before.a + before.b - A;
                        after.operate = "pour B A";
                        check(before, after);
                    }
                }
            }
            if (before.b < B)
            {//倒满B
                after.b = B;
                after.a = before.a;
                after.operate = "fill B";
                check(before, after);
                if (before.a != 0)
                {
                    if (before.a + before.b <= B)
                    {//B能装下
                        after.b = before.a + before.b;
                        after.a = 0;
                        after.operate = "pour A B";
                        check(before, after);
                    }
                    else
                    {//B装不下
                        after.b = B;
                        after.a = before.a + before.b - B;
                        after.operate = "pour A B";
                        check(before, after);
                    }
                }
            }
            if (before.a == C || before.b == C)
            {
                output(before);
                cout << "success" << endl;
                break;
            }
        }
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值