UVA 10798 - Be wary of Roses(记忆化BFS)

UVA 10798 - Be wary of Roses

题目链接

题意:给定一个地图,人一开始在中心,问选择一种走法走出去,使得面朝任何一个方向走,踩到的花的最大值最小

思路:用优先队列进行BFS,每次取出踩到最少的情况,广搜记录状态为当前位置,和4个方向分别踩到的花数

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 21;
const int d[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};

int n, vis[N][N][11][11][11][11];

char g[N][N];

struct State {
	int x, y, val;
	int up, left, down, right;
	State() {x = y = up = left = down = right = 0;}
	State(int x, int y, int up, int left, int down, int right) {
		this->x = x;
		this->y = y;
		this->up = up;
		this->left = left;
		this->down = down;
		this->right = right;
		val = max(max(max(up,left), down), right);
	}
	bool operator < (const State& c) const {
		return val > c.val;
	}
} s;

void init() {
	for (int i = 0; i < n; i++) {
		scanf("%s", g[i]);
		for (int j = 0; j < n; j++)
			if (g[i][j] == 'P')
				s.x = i, s.y = j;
	}
}

int bfs() {
	memset(vis, 0, sizeof(vis));
	priority_queue<State> Q;
	Q.push(s);
	vis[s.x][s.y][0][0][0][0] = 1;
	while (!Q.empty()) {
		State u = Q.top();
		Q.pop();
		if (u.x == 0 || u.x == n - 1 || u.y == 0 || u.y == n - 1) return u.val;
		for (int i = 0; i < 4; i++) {
			int xx = u.x + d[i][0];
			int yy = u.y + d[i][1];
			int up = u.up;
			int left = u.left;
			int down = u.down;
			int right = u.right;
			if (g[xx][yy] == 'R') up++;
			if (g[n - 1 - yy][xx] == 'R') left++;
			if (g[n - 1 - xx][n - 1 - yy] == 'R') down++;
			if (g[yy][n - 1 - xx] == 'R') right++;
			if (!vis[xx][yy][up][left][down][right]) {
				vis[xx][yy][up][left][down][right] = 1;
				Q.push(State(xx, yy, up, left, down, right));
			}
		}
	}
}

int main() {
	while (~scanf("%d", &n) && n) {
		init();
		printf("At most %d rose(s) trampled.\n", bfs());
	}
	return 0;
}
时间: 2024-08-04 23:59:03

UVA 10798 - Be wary of Roses(记忆化BFS)的相关文章

uva 1390 - Interconnect(期望+哈希+记忆化)

题目连接:uva 1390 - Interconnect 题目大意:给出n表示有n个点,m表示有m条边,现在任选两点建立一条边,直到整个图联通,问说还需建立边数的期望,建过边的两点仍可以建边. 解题思路:哈希的方法很是巧妙,将各个联通分量中节点的个数c[i]转换成一个30进制的数(因为节点个数最多为30),因为结果很大,所以对1e5+7取模.获得的哈希值作为插入和搜索的起点. #include <cstdio> #include <cstring> #include <alg

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

uva 10626 Buying Coke (DP记忆化搜索)

uva 10626 Buying Coke I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes at once, since my working mates also likes coke. A coke in the vending machine costs 8 Swedish crowns, and the machine accept crowns with the va

UVA 10003 Cutting Sticks 区间DP+记忆化搜索

UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的长度L,第二行是切割点的个数n,接下来的n行是切割点在木棍上的坐标. 输出切割木棍的最小费用 前话-区间dp简单入门 区间dp的入门下面博客写的非常好,我就是看的他们博客学会的,入门简单,以后的应用就得靠自己了. https://blog.csdn.net/qq_41661809/article/d

UVa 10651 Pebble Solitaire(DP 记忆化搜索)

Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as possible

UVA 11762 Race to 1(记忆化+期望)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操作次数,则有: f[x]=1+f[x]*(1-g[x]/p[x])+sigma(f[x][y])/p[x] 进一步为: f[x]=(sigma(f[x/y])+p[x])/g[x] 其中p[x]表示1..x的素数个数,p[x]表示素数中可以整除x的个数. 保留vis可以节约时间. [代码] 1 #i

Uva 10118 Free Candies (DP+记忆化搜索)

The Problem Little Bob is playing a game. He wants to win some candies in it - as many as possible. There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one

UVA 11008--Antimatter Ray Clearcutting+状态压缩记忆化搜索

题目链接:点击进入 最多只有16个点,如果不用状态压缩的话,最优子结构没法找到.所以我们进行状态压缩,用一个数表示当前的状态,对应二进制位为1表示该位置的树还未被砍掉,为0表示已被砍掉,初始状态为(1< #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define maxn 20 #define INF 0x3f3f3f3f typedef struct { i

HDU 1072(记忆化BFS)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时间. 解题思路: vis的第三维标记一下到这个格子的时间. 尽管可以格子可以重复走,但在相同时间到这个格子是没有意义的. 小心一下时间不能为0的问题就行了. #include "cstdio" #include "queue" #include "cstrin