Dailycode_6.14(年终奖、迷宫)

在这里插入图片描述

class Bonus {
public:
    int getMost(vector<vector<int> > board) {
        // write code here
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                if (i == 0 && j == 0)
                    continue;
                else if (i == 0)
                    board[i][j] += board[i][j-1];
                else if (j == 0)
                    board[i][j] += board[i-1][j];
                else
                {
                    int A = board[i-1][j];
                    int B = board[i][j-1];
                    
                    if (A > B)
                        board[i][j] += A;
                    else
                        board[i][j] += B;
                }
            }
        }
        return board[5][5];
    }
};

在这里插入图片描述

#include <iostream>
#include <stack>
using namespace std;
typedef struct point
{
	int x;
	int y;
}step;

bool should_continue(stack<step> sta, step end)
{
	bool flag1 = (sta.top().x == end.x);
	bool flag2 = (sta.top().y == end.y);
	if (flag1 && flag2)
		return true;
	return false;
}

int main()
{
	int N, M;
	int maze[10][10];

	while (cin >> N >> M)
	{
		for (int i = 0; i != N; i++)
			for (int j = 0; j != M; j++)
				cin >> maze[i][j];

		int flag[10][10] = { 0 };

		stack<step> sta;
		step begin, end, now;
		begin.x = 0; begin.y = 0;
		end.x = N - 1; end.y = M - 1;
		now = begin;
		sta.push(begin);
		flag[0][0] = 1;
		while (!should_continue(sta, end))
		{
			if (now.x + 1 <= N - 1 && now.y <= M - 1 && maze[now.x + 1][now.y] == 0 && flag[now.x + 1][now.y] == 0)//是否越界或已经到达过,向下走
			{
				flag[now.x + 1][now.y] = 1;
				now.x += 1;
				sta.push(now);
				continue;
			}
			else if (now.x <= N - 1 && now.y + 1 <= M - 1 && maze[now.x][now.y + 1] == 0 && flag[now.x][now.y + 1] == 0)//向右走
			{
				flag[now.x][now.y + 1] = 1;
				now.y += 1;
				sta.push(now);
				continue;
			}
			else//后退
			{
				sta.pop();
				now.x = sta.top().x;
				now.y = sta.top().y;
			}
		}
		stack<step> stb;
		while (!sta.empty())
		{
			stb.push(sta.top());
			sta.pop();
		}
		while (!stb.empty())
		{
			cout << "(" << stb.top().x << "," << stb.top().y << ")" << endl;
            stb.pop();
		}
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值