紫书UVa1600

在这里插入图片描述

#include<iostream>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
struct point
{
	int x;
	int y;
	point(const int x=-1,const int y=-1):x(x),y(y){}
	
};
struct status
{
	point pos;
	int turbo;
	status(const point&p,const int o=0):pos(p),turbo(o){}
};
point operator+(const point& A,const point&B)
{
	return point(A.x + B.x, A.y + B.y);
}
bool operator==(const point& A, const point& B)
{
	return A.x == B.x && A.y == B.y;
}
const int maxn = 20 + 2;
int m, n;
int k;
point dir[4] = { {0,1},{0,-1},{1,0},{-1,0} };
int vis[maxn][maxn];
int ob[maxn][maxn];
const int inf = 10000;
int grid[maxn][maxn];
bool isvalid(point start, point direct)
{
	if (start.x + direct.x >= m || start.x + direct.x < 0
		|| start.y + direct.y>=n || start.y + direct.y < 0)
		return false;
	return true;
}
int solve(const point &start,const point&final)
{
	memset(vis, inf, sizeof(vis));
	memset(ob, inf, sizeof(ob));
	queue<status>q;
	q.push(status(start,0));
	vis[0][0] = 0;
	ob[0][0] = 0;
	while (!q.empty())
	{
		status cur = q.front(); 
 		q.pop();
		if (cur.pos == final)
		{
			return vis[final.x][final.y];
		}
		for (int i = 0; i < 4; ++i)
		{
			point mdir = dir[i];
			if (isvalid(cur.pos, mdir))
			{
				point jump = cur.pos + mdir;
				int curobs = cur.turbo;
				if (grid[jump.x][jump.y] == 1)
					++curobs;
				else
					curobs = 0;
				if (curobs > k)
					continue;
				if (vis[jump.x][jump.y] > vis[cur.pos.x][cur.pos.y] + 1||curobs<ob[jump.x][jump.y])
				{
					vis[jump.x][jump.y] = vis[cur.pos.x][cur.pos.y] + 1;
					ob[jump.x][jump.y] = curobs;
					q.push(status(jump,curobs));
				}
			}
		}

	}
	return -1;
}
int main()
{
	int t;
	cin >> t;
	while (t-- > 0)
	{
		memset(grid, 0, sizeof(grid));
		cin >> m >> n;
		cin >> k;
		for (int i = 0; i < m; ++i)
		{
			for (int j = 0; j < n; ++j)
				cin >> grid[i][j];
		}
		cout << solve(point(0, 0), point(m - 1, n - 1)) << endl;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值