kuangbing题单(1)简单搜索HDU 2612 Find a way

先附上题目链接
一道普通的搜索题.也是这个题单里的最后一题.虽然在code里面我取得名叫bfs.但其实就是dfs.毕竟把所有的情况都枚举了一遍.在题目的规模下是可以过的.
思路就是先枚举其中一个人的移动情况.然后用一个二维数组记录一下此人到各个KFC所耗费的时间.然后枚举另外一个人移动.两者和的最小值就是答案了.代码部分还有不少可以优化的部分.有时间再来吧.
附上code

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <string.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int mn = 210;
typedef long long ll;
struct node {
	int r,c, cost;
};
int ans[mn][mn];
char maze[mn][mn];
int n, m;
node sy,sm;
bool inside(int r, int c) {
	if (r >= 0 && r < n && c >= 0 && c < m) return true;
	return false;
}
int minans = INF;
bool visy[mn][mn];
bool vism[mn][mn];
int dr[4] = { 0,0,1,-1 };
int dc[4] = { 1,-1,0,0 };
void bfsy() {
	queue<node> q;
	q.push(sy);
	while(!q.empty()){
		node cur = q.front(); q.pop();
		if (maze[cur.r][cur.c] == '@') {
			ans[cur.r][cur.c] = cur.cost;
		}
		for (int i = 0; i < 4; ++i) {
			int nr = dr[i] + cur.r, nc = dc[i] + cur.c;
			if (!inside(nr, nc)) continue;
			if (visy[nr][nc]) continue;
			if (maze[nr][nc] == '#') continue;
			visy[nr][nc] = 1;
			node nn = { nr,nc,cur.cost + 11 };
			q.push(nn);
		}
	}
}
void bfsm() {
	queue<node> q;
	q.push(sm);
	minans = INF;
	while (!q.empty()) {
		node cur = q.front(); q.pop();
		if (maze[cur.r][cur.c] == '@') {
			int tans = ans[cur.r][cur.c] + cur.cost;
			if (tans < minans) minans = tans;
		}
		for (int i = 0; i < 4; ++i) {
			int nr = dr[i] + cur.r, nc = dc[i] + cur.c;
			if (!inside(nr, nc)) continue;
			if (vism[nr][nc]) continue;
			if (maze[nr][nc] == '#') continue;
			vism[nr][nc] = 1;
			node nn = { nr,nc,cur.cost + 11 };
			q.push(nn);
		}
	}
}
int main() {
	while (cin >> n >> m) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				cin >> maze[i][j];
				if (maze[i][j] == 'Y') {
					sy = { i,j,0 };
				}
				if (maze[i][j] == 'M') {
					sm = { i,j,0 };
				}
			}
		}
		memset(visy, 0, sizeof(visy));
		memset(vism, 0, sizeof(vism));
		visy[sy.r][sy.c] = 1;
		vism[sm.r][sm.c] = 1;
		bfsy();
		bfsm();
		cout << minans<<endl;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值