AOJ 0558 bfs

题意

传送门 AOJ 0558

老鼠初始体力为 1,每吃 1 个奶酪体力增 1,且老鼠所吃奶酪硬度值不能超过自身体力值,这意味着老鼠只能按照 1, 2, 3… 的顺序吃奶酪。每一次从当前位置 bfs 找到对应奶酪工厂的最短路径即可。(可以经过某个工厂而不吃这个工厂的奶酪)

#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define abs(x)    ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-3
#define PI 3.14159265358979323846
#define MAX_N 1005
using namespace std;
typedef pair<int, int> P;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int H, W, N;
char mp[MAX_N][MAX_N];
int d[MAX_N][MAX_N];

bool judge(int x, int y){
	return x >= 0 && x < H && y >= 0 && y < W && d[x][y] == 0 && mp[x][y] != 'X';
}

int bfs(int &sx, int &sy, char c){
	int fx, fy;
	queue<P> que;
	memset(d, 0, sizeof(d));
	que.push(P(sx, sy));
	while(!que.empty()){
		P p = que.front(); que.pop();
		int x = p.first, y = p.second;
		if(mp[x][y] == c){
			fx = x, fy = y;
			break;
		}
		for(int i = 0; i < 4; i++){
			int nx = x + dx[i], ny = y + dy[i];
			if(judge(nx, ny)){
				d[nx][ny] = d[x][y] + 1;
				que.push(P(nx, ny));
			}
		}
	}
	sx = fx, sy = fy;
	return d[fx][fy];
}

int main(){
	while(~scanf("%d%d%d", &H, &W, &N)){
		int sx, sy;
		for(int i = 0; i < H; i++){
			scanf("%s", mp + i);
			for(int j = 0; j < W; j++){
				if(mp[i][j] == 'S'){
					sx = i, sy = j;
				}
			}
		}
		int res = 0;
		for(int i = 1; i <= N; i++){
			res += bfs(sx, sy, i + '0');
		}
		printf("%d\n", res);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值