ch 2907宽搜

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define IMAX 2147483646
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define c unsigned long long
#define uint unsigned int

using namespace std;

int x, y, mx, my, d[111][111];
char s[111][111];
bool v[111][111];
int dx[8] = { -1,-1,-1,1,1,1,0,0 };
int dy[8] = { -1,0,1,-1,0,1,-1,1 };
queue<pair<int, int> > q;

bool pd() {
	for (int i = 1; i <= y; i++)
		for (int j = 1; j <= x; j++)
			if (!v[i][j]) return 0;
	return 1;
}

int main() {
	scanf("%d%d%d%d", &x, &y, &mx, &my);
	q.push(make_pair(y + 1 - my, mx));
	for (int i = 1; i <= y; i++)
		scanf("%s", s[i] + 1);
	memset(v, 0, sizeof(v));
	for (int i = 1; i <= y; i++)
		for (int j = 1; j <= x; j++)
			if (s[i][j] == '*') v[i][j] = 1;
	v[y + 1 - my][mx] = 1;
	memset(d, 0, sizeof(d));
	while (q.size()) {
		pair<int, int> now = q.front();
		q.pop();
		for (int i = 0; i < 8; i++) {
			pair<int, int> nxt;
			nxt.first = now.first + dx[i];
			nxt.second = now.second + dy[i];
			if (nxt.first < 1 || nxt.first > y || nxt.second < 1 || nxt.second > x)
				continue;
			if (v[nxt.first][nxt.second]) 
				continue;
			v[nxt.first][nxt.second] = 1;
			d[nxt.first][nxt.second] = d[now.first][now.second] + 1;
			q.push(nxt);
		}
		if (pd()) {
			printf("%d\n", d[now.first][now.second] + 1);
			return 0;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值