4115:鸣人和佐助

4115:鸣人和佐助

题目链接http://bailian.openjudge.cn/practice/4115/
思路:我之前用的是二维数组,忽略了到达同一点所用的查克拉可能不同,所以要用三维数组,来表示状态。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef struct Node {
	Node(int xx, int yy,int b,int t) : x(xx), y(yy),bs(b),tt(t){}
	int x, y,bs,tt;
}node;
queue<node> p;
char a[210][210];
bool visited[210][210][15];
int n, m, t;
int fx[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int min_bs =-1;
bool pd(int x, int y) {
	return x >= 1 && x <= m && y >= 1 && y <= n;
}
void bfs() {
	while (!p.empty()) {
		node temp = p.front();
		p.pop();
		for (int i = 0; i < 4; i++) {
			int x = temp.x+fx[i][0], y = temp.y+fx[i][1];
			if (pd(x, y)) {
				
				if (a[x][y] == '#'&& temp.tt>0 && !visited[x][y][temp.tt-1]) {
					p.push(node(x, y, temp.bs + 1,temp.tt-1));
					visited[x][y][temp.tt-1] = 1;
				}
				else if (a[x][y] == '+')
				{
					min_bs = temp.bs+1;
					return;
				}
				else if (a[x][y] == '*' && !visited[x][y][temp.tt]) {
					p.push(node(x, y, temp.bs+1,temp.tt));
					visited[x][y][temp.tt] = 1;
				}
			}
		}
	}
}
int main() {
	cin >> m >> n>>t;
	int x, y;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
			if (a[i][j] == '@') {
				x = i, y = j;
			}
		}
	}
	memset(visited, 0, sizeof(visited));
	visited[x][y][t] = 1;
	p.push(node(x, y,0,t));
	bfs();
	cout << min_bs;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值