码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4771 Stealing Harry Potter's Precious(BFS + DFS)

时间:2014-07-08 15:58:25      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   2014   

HDU 4771 Stealing Harry Potter‘s Precious

题目链接

题意:给定人的起始位置和k个宝物,求人拿完全部宝物最小的步数

思路:先bfs打出两两之间路径,然后dfs暴力求答案,因为宝物才4个,所以暴力是没问题的

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int N = 105;
int n, m, k, g[10][10], vis[10];
char str[N][N];

struct Point {
    int x, y, num;
    Point(int x = 0, int y = 0) {
	this->x = x;
	this->y = y;
    }
} s, p[10];

int bfs(Point a, Point b) {
    queue<Point> Q;
    a.num = 0;
    Q.push(a);
    int vis[N][N];
    memset(vis, 0, sizeof(vis));
    vis[a.x][a.y] = 1;
    while (!Q.empty()) {
	Point now = Q.front();
	if (now.x == b.x && now.y == b.y) return now.num;
	Q.pop();
	for (int i = 0; i < 4; i++) {
	    int xx = now.x + d[i][0];
	    int yy = now.y + d[i][1];
	    if (xx <= 0 || xx > n || yy <= 0 || yy > m || str[xx][yy] == '#'|| vis[xx][yy]) continue;
	    Point next = Point(xx, yy);
	    next.num = now.num + 1;
	    vis[next.x][next.y] = 1;
	    Q.push(next);
	}
    }
    return -1;
}

bool build() {
    memset(g, -1, sizeof(g));
    for (int i = 0; i < k; i++) {
	g[0][i + 1] = bfs(s, p[i]);
	for (int j = i + 1; j < k; j++)
	    g[i + 1][j + 1] = g[j + 1][i + 1] = bfs(p[i], p[j]);
    }
    return true;
}

int dfs(int now, int num) {
    if (num == k) return 0;
    int ans = INF;
    for (int i = 1; i <= k; i++) {
	if (g[now][i] == -1 || vis[i]) continue;
	vis[i] = 1;
	ans = min(ans, dfs(i, num + 1) + g[now][i]);
	vis[i] = 0;
    }
    return ans;
}

int main() {
    while (~scanf("%d%d", &n, &m) && n || m) {
	memset(vis, 0, sizeof(vis));
	for (int i = 1; i <= n; i++) {
	    scanf("%s", str[i] + 1);
	    for (int j = 1; j <= m; j++) {
		if (str[i][j] == '@')
		    s = Point(i, j);
	    }
	}
	scanf("%d", &k);
	for (int i = 0; i < k; i++)
	    scanf("%d%d", &p[i].x, &p[i].y);
	if (!build()) printf("-1\n");
	else printf("%d\n", dfs(0, 0));
    }
    return 0;
}


HDU 4771 Stealing Harry Potter's Precious(BFS + DFS),布布扣,bubuko.com

HDU 4771 Stealing Harry Potter's Precious(BFS + DFS)

标签:style   blog   http   color   os   2014   

原文地址:http://blog.csdn.net/accelerator_/article/details/37372291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!