POJ - 2688 Cleaning Robot

题意:求回收所有垃圾的最短路

思路:先BFS处理两个垃圾的距离,然后DFS记忆化搜索

dp[i][state]表示处理到第i个后状态是state的最短路

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int MAXN = 30;
const int INF = 0x3f3f3f3f;

struct Point {
	int x,y;
}point[20];
struct node {
	int x,y,step,f;
	node(int _x, int _y, int _step, int _f) {
		x = _x, y = _y, step = _step, f = _f;
	}
	bool operator< (const node a)const {
		return f > a.f;
	}
};
char map[MAXN][MAXN];
int n,m;
int dx[4] = {1,-1,0,0};
int	dy[4] = {0,0,1,-1};
int dis[MAXN][MAXN], vis[MAXN][MAXN];
int dp[12][1<<12],sum;

int check(int x, int y) {
	if (x > 0 && x <= n && y > 0 && y <= m && map[x][y] != 'x')
		return 1;
	return 0;
}

int getdiff(const Point &a, const Point &b) {
	return abs(a.x-b.x) + abs(a.y-b.y);
}

int bfs(const Point &a, const Point &b) {
	priority_queue<node > q;
	q.push(node(a.x, a.y, 0, getdiff(a, b)));
	memset(vis, 0, sizeof(vis));
	vis[a.x][a.y] = 1;
	while (!q.empty()) {
		node cur = q.top();
		q.pop();
		if (cur.x == b.x && cur.y == b.y)
			return cur.step;
		for (int i = 0; i < 4; i++) {
			Point tmp;
			tmp.x = cur.x + dx[i];
			tmp.y = cur.y + dy[i];
			if (check(tmp.x, tmp.y) && !vis[tmp.x][tmp.y]) {
				vis[tmp.x][tmp.y] = 1;
				int f = cur.step+1+getdiff(tmp, b);
				q.push(node(tmp.x, tmp.y, cur.step+1, f));
			}
		}
	}
	return -1;
}

int getFlag(int i) {
	return 1<<i;
}

int dfs(int st, int u) {
	if (dp[u][st] != INF)
		return dp[u][st];
	int s = st^getFlag(u-1);
	if (s == 0)
		return dis[0][u];
	for (int i = 1; i <= sum; i++)
		if (s & getFlag(i-1))
			dp[u][st] = min(dp[u][st], dfs(s, i)+dis[i][u]);
	return dp[u][st];
}

int main() {
	while (scanf("%d%d", &m, &n) != EOF && n+m) {
		memset(map, 0, sizeof(map));
		sum = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%s", &map[i][1]);
			for (int j = 1; j <= m; j++)
				if (map[i][j] == '*') {
					++sum;
					point[sum].x = i;
					point[sum].y = j;
				}
				else if (map[i][j] == 'o') {
					point[0].x = i;
					point[0].y = j;
				}
		}
		int flag = 0;
		for (int i = 0; i <= sum; i++) {
			for (int j = i; j <= sum; j++) {
				if (i == j)
					dis[i][j] = 0;
				else {
					dis[i][j] = dis[j][i] = bfs(point[i], point[j]);
					if (dis[i][j] == -1) {
						flag = 1;
						break;
					}
				}
			}
		}
		if (flag) {
			printf("-1\n");
			continue;
		}
		int ends = getFlag(sum) - 1;
		for (int i = 0; i <= sum; i++)
			for (int j = 0; j <= ends; j++)
				dp[i][j] = INF;
		dp[0][0] = 0;
		int ans = INF;
		for (int i = 1; i <= sum; i++)
			ans = min(ans, dfs(ends, i));
		printf("%d\n", ans);
	}
	return 0;
}

POJ - 2688 Cleaning Robot,布布扣,bubuko.com

时间: 2024-10-05 18:19:31

POJ - 2688 Cleaning Robot的相关文章

poj 2688 Cleaning Robot (tsp问题)

Cleaning Robot Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4073   Accepted: 1659 Description Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture. Consider the room floor paved with

poj 2688 cleaning robot(bfs+dfs)

Description Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture. Consider the room floor paved with square tiles whose size fits the cleaning robot (1 * 1). There are 'clean tiles' and 'dirty tiles

poj 2688 状态压缩dp解tsp

题意: 裸的tsp. 分析: 用bfs求出任意两点之间的距离后可以暴搜也可以用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发访问过s集合中的点,目前在点u走过的最短路程. 代码: //poj 2688 //sep9 #include <iostream> #include <queue> using namespace std; const int maxW=32; const int maxN=12; int dx[4]={-1,1,0,

POJ 1573:Robot Motion

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10267   Accepted: 5001 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

Cleaning Robot POJ - 2688

题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过障碍物),如果能,需要得到机器人移动最少步数. 思路:可以把扫地机器人和地砖编号,然后得出编号之间的相互距离,那么就变成了一个图问题. 用bfs来得出编号之间的距离. 再用dfs来填边,得到最少移动步数,注意要剪支,不然就T了... 1 #include <iostream> 2 #include

POJ 2376 Cleaning Shifts(轮班打扫)

Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided t

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

[POJ 2376] Cleaning Shifts

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12874   Accepted: 3331 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000