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

A —MAZE

题目

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

input

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

output

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

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

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

(1, 4)

(2, 4)

(3, 4)

(4, 4)

解题思路

利用队列结构结合广度优先的搜索方法,将从起点出发可以走的且为走过的点放到队列中,并记录每个点的父节点即从哪个点到达的当前节点,并用数组state标记每个点的状态即是否走过。当到达终点后,从终点开始依次寻找每个节点的父节点即可得到一条路径。

#include<iostream>
using namespace std;
struct node
{
 int x;//横坐标
 int y;//纵坐标
 int father;//存储父节点在队列中的位置,即是通过那个节点到达的此节点
 //构造函数
 node(int x,int y,int f):x(x),y(y),father(f){}
 node(){}
};
int main()
{
 node queue[25];  //创建队列
 int head = 0, tail = 1;//用来表示队列的头和尾
 int a[5][5];//存储迷宫
 for (int i = 0; i < 5; i++)
 {
  for (int j = 0; j < 5; j++)
   cin >> a[i][j];
 }
 int state[5][5];//标识该点是否已经走过
 queue[head] = node(0, 0, -1);//起始点的父节点标记为-1,添加到队列中
 state[0][0] = 1;//1表示已经经过该点
 while (head != tail)
 {
  if (queue[head].x == 4 && queue[head].y == 4)
  {
   int b = head, num=0,road[25];//路径长度和路径
   while (queue[b].father != -1)//从终点往起点
   {
    road[num] = queue[b].father;
    b = queue[b].father;//父节点在队列中的位置
    num++;
   }
   for (int j = num - 1; j >= 0; j++)
   {
    cout << "(" << queue[road[j]].x << ", " << queue[road[j]].y << ")" << endl;
   }
   cout << "(4, 4)";
   return 0;
  }
  if (a[queue[head].x - 1][queue[head].y] == 0 && queue[head].x - 1 >= 0 && !state[queue[head].x - 1][queue[head].y])
  {
   cout << 11 << endl;
   queue[tail] = node(queue[head].x - 1, queue[head].y, head);
   state[queue[head].x - 1][queue[head].y] = 1;
   head++;
  }//上移
  if (a[queue[head].x + 1][queue[head].y] == 0 && queue[head].x <=3 && !state[queue[head].x + 1][queue[head].y])
  {
   cout << 22 << endl;
   queue[tail] = node(queue[head].x + 1, queue[head].y, head);
   state[queue[head].x + 1][queue[head].y] = 1;
   head++;
  }//下移
  if (a[queue[head].x][queue[head].y-1] == 0 && queue[head].y-1 >= 0 && !state[queue[head].x][queue[head].y-1])
  {
   cout << 33 << endl;
   queue[tail] = node(queue[head].x , queue[head].y-1, head);
   state[queue[head].x][queue[head].y-1] = 1;
   head++;
  }//左移
  if (a[queue[head].x][queue[head].y+1] == 0 && queue[head].y <= 3 && !state[queue[head].x][queue[head].y+1])
  {
   cout << 44<< endl;
   queue[tail] = node(queue[head].x, queue[head].y+1, head);
   state[queue[head].x][queue[head].y+1] = 1;
   head++;
  }//右移
  head++;
 }
}

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

解题思路

可看成迷宫问题,起点(0,0),终点(C,b)或者(a,C),a代表A杯中的水,b代表B杯中的水。每次可走的点变成了六个,分别为(A,b)(fill A), (a,B)(fill B), (0,b)(empty A), (a,0)(empty B),
(a+b/A,0/a+b-A)(pour A B), (0/a+b-B,a+b/B)(pour B A)

用队列结构进行广度优先搜索,并用结构体node中s来记录到该点经过的操作

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
 int a;//记录A瓶中水的容量
 int b;//记录B瓶中谁的容量
 string s;//记录到达该种情况经历了哪些步骤
 //构造函数
 node(int theA, int theB, string theS)
 {
  a = theA;
  b = theB;
  s = theS;
 }
 node() {}
};
int main()
{
 int A, B, C;
 queue<node> q;
 while (cin >> A >> B >> C)
 {
  int state[400][400];
  memset(state, 0, sizeof(state));//将数组state初始化为0
  state[0][0] = 1;
  //cout<<state[99][99]<<endl;
  while (!q.empty())
   q.pop();//防止上一种情况队列中还有剩余
  q.push(node(0, 0, string()));
  while (!q.empty())
  {
   //cout << "yaya" << endl;
   int a = q.front().a;
   int b = q.front().b;
   string s = q.front().s;
   //cout<<a<<b<<endl;
   q.pop();
   if (a == C || b == C)
   {
    //cout << "haha" << endl;
    for (int i = 0; i < s.size(); i++)
    {
     if (s[i] == '1')
      cout << "fill A" << endl;
     else if (s[i] == '2')
      cout << "fill B" << endl;
     else if (s[i] == '3')
      cout << "empty A" << endl;
     else if (s[i] == '4')
      cout << "empty B" << endl;
     else if (s[i] == '5')
      cout << "pour A B" << endl;
     else if (s[i] == '6')
      cout << "pour B A" << endl;
    }
    cout << "success" << endl;
    break;
   }
   if (state[A][b] == 0)
   {
    //cout << "11" << endl;
    q.push(node(A, b, s + '1'));
    state[A][b] = 1;
   }//fill A
   if (state[a][B] == 0)
   {
    //cout << "22" << endl;
    q.push(node(a, B, s + '2'));
    state[a][B] = 1;
   }//fill B
   if (state[0][b] == 0)
   {
    //cout << "33" << endl;
    q.push(node(0, b, s + '3'));
    state[0][b] = 1;
   }//empty A
   if (state[a][0] == 0)
   {
    //cout << "44" << endl;
    q.push(node(a, 0, s + '4'));
    state[a][0] = 1;
   }//empty B
   int c = a + b < B ? a + b : B;
   if (state[a + b - c][c] == 0)
   {
    //cout << "55" << endl;
    q.push(node(a + b - c, c, s + '5'));
    state[a + b - c][c] = 1;
   }//pour A B
   c = a + b < A ? a + b : A;
   if (state[c][a + b - c] == 0)
   {
    //cout << "66" << endl;
    q.push(node(c, a + b - c, s + '6'));
    state[c][a + b - c] = 1;
   }//pour B A
   
  }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值