解题报告 之 ZOJ3877 Earthstone Keeper

解题报告 之 ZOJ3877 Earthstone Keeper


Description

Earthstone Keeper is a famous roguelike game created by Lizard Entertainment. In this game, an adventurer will explore in a single layer dungeon of N × M size.

The adventurer starts at the room (SR, SC) and he wants to go to the room located at (TR, TC) since there hides a big treasure box full of gold and diamonds! However, exploration is not always an easy job. There are many traps and monsters in the dungeon. To be specific, there are 4 types of rooms:

  1. Common. The room is safe.
  2. Obstacles. The room is full of obstacles that the adventurer cannot enter it.
  3. Trap. The room has been set up with a trap. The trap will be triggered when the adventurer enters the room. When the adventurer leaves, the trap will reset so it can be triggered next time.
  4. Monster. There is a monster standing in it. If the adventurer walks into a monster room or any room adjacent to a monster room, the monster will immediately rush up to the adventurer and fight with him. Once the monster is killed, the adventurer can continue his exploration. Of course, the monster will not revive.

Two rooms are adjacent if and only if they share an edge. The adventurer can take 1 minute to go from a room to an adjacent room. Traps or monsters are always dangerous. More precisely, there is a fatality value for each trap and monster. Although our adventurer is strong and swift, battling with a deadly monster or dodging a rolling stone trap are not wise options.

The dungeon has been sealed by its owner with a powerful magic so the adventurer can not escape to outside until he found the treasure. By the way, being afraid of monsters battling with each other, the dungeon owner will not set any two monster rooms adjacent to each other. The room (SR, SC) and (TR, TC) are always common rooms and will not be adjacent to any monster room.

The adventurer want choose a best path to the treasure. The total fatality value of all monsters he killed and all traps he triggered should be as low as possible (If a trap was triggered multiple times, the fatality should also count multiple times). Among all safest paths, he want to choose the shortest path lead to the treasure room.

Please write program to help the adventurer find out the best path to the treasure.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N and M (1 <= NM <= 500). The second line contains 4 integers SRSCTRTC (1 <= SRTR <= N and 1 <= SCTC <= M).

For the next N lines, each line contains M characters indicating the map of dungeon. Each type of room is marked as:

  1. Common: "."
  2. Obstacles: "#"
  3. Trap: from "a" to "z"
  4. Monster: from "A" to "Z"

The fatality value of trap "a" and monster "A" is 1. The fatality value of trap "b" and monster "B" is 2, and so on. Therefore, the most dangerous trap "z" or monster "Z" has its fatality value equal to 26.

Output

For each test case, output the total fatality value and the time cost (in minutes) of the best path, separated by a space. It is guaranteed that there always exists at least one path from (SR, SC) to (TR, TC).

Sample Input

1
3 5
1 1 3 5
..b..
.#C#.
..a..

Sample Output

4 6

题目大意:有一个地图,给出起点和终点坐标。有一些格子有陷阱,也有些格子有怪物。陷阱标志a~z,表示走到这个陷阱会造成对应伤害。而怪物标记为A~Z,表示走到怪物周围四个格子会造成对应伤害(怪物会来打你)。规定好了不会有挨着的两个怪物格,且起点终点也不是怪物格和陷阱格,问从起点到终点的遭受伤害最短的路,如果伤害一样,则找到路径最短的。

分析:乍看之下很像模拟题,但是细想发现明显要超时。然后看了看发现是双关键字最短路,具体思路是更新时如果第一关键字小于,或者第一关键字等于&&第二关键字小于即可更新。这道题还有个难点在于建图,首先需要看看周围的周围有没有怪物,这点比较容易理解。最难的是跨越怪物的建图。对于每个怪物格子,查看周围的格子,如果有两个可以的格子则连接这两个格子(长度为2),且要看看联通的两个格子的周围是否有怪物(注意甄别是否重复计算两次)。

上代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<functional>
#include<utility>
#include<cmath>

using namespace std;
const int MAXN = 502;	//最大节点
const int MAXM = 1100000; //边的最大值
const int INF = 0x3f3f3f3f;

char map[MAXN][MAXN];	//保存地图	
pair<int, int> dis[MAXN][MAXN];	//用于最短路的长度(pair->first是伤害,second是距离)
int n, m;
int sx, sy, dx, dy;
int dir[4][2] = { -1, 0, 1, 0, 0, -1, 0, 1 };


struct Edge
{
	int vx, vy;
	int bl, dis;
	int next;
}; //边结构


struct node
{
	int x, y, b, d;

	node( int x, int y, int b, int d )
	{
		this->x = x;
		this->y = y;
		this->b = b;
		this->d = d;
	}
}; //节点状态结构


class cmp
{
public:
	bool operator() ( const node& lhs, const node& rhs )
	{
		if(lhs.b != rhs.b) return lhs.b > rhs.b;
		return lhs.d > rhs.d;
	}
};

int head[MAXN][MAXN];
Edge edge[MAXM];
int cnt = 0;

void addedge( int ux, int uy, int vx, int vy, int bl, int dis )
{
	edge[cnt].vx = vx;
	edge[cnt].vy = vy;
	edge[cnt].bl = bl;
	edge[cnt].dis = dis;
	edge[cnt].next = head[ux][uy];
	head[ux][uy] = cnt++;
}//邻接表的数组实现

void ini( )
{
	memset( map, '#', sizeof map );
	memset( head, -1, sizeof head );
	cnt = 0;

	scanf( "%d%d", &n, &m );
	scanf( "%d%d%d%d", &sx, &sy, &dx, &dy );
	for(int i = 1; i <= n; i++)
		scanf( "%s", map[i] + 1 );
}//初始化函数	

bool check( int x, int y )
{
	if(x <= 0 || y <= 0 || x > n || y > m || map[x][y] == '#')return false;
	return true;
}//看看是否可以走到这个格子

void SPFA( )	//最短路算法,Dijkstra要超时
{
	priority_queue<node, vector<node>, cmp> q;
	q.push( node( sx, sy, 0, 0 ) );
	memset( dis, 0x7f, sizeof dis );
	dis[sx][sy] = make_pair( 0, 0 );

	while(!q.empty( ))
	{
		
		int x = q.top( ).x;
		int y = q.top( ).y;
		if((x == dx) && (y == dy)) break;
		q.pop( );

		for(int i = head[x][y]; i != -1; i = edge[i].next)
		{
			int vx = edge[i].vx;
			int vy = edge[i].vy;
			int temb = edge[i].bl;
			int temd = edge[i].dis;

			if(islower( map[vx][vy] )) temb += map[vx][vy] - 'a' + 1;
			if((dis[vx][vy].first > dis[x][y].first + temb) || (dis[vx][vy].first == dis[x][y].first + temb&&dis[vx][vy].second > dis[x][y].second + temd))
			{
				dis[vx][vy].first = dis[x][y].first + temb;
				dis[vx][vy].second = dis[x][y].second + temd;
				q.push( node( vx, vy, dis[vx][vy].first, dis[vx][vy].second ) );
			}
		}
	}

}


void create( ) //建图过程。比较复杂
{
	for(int i = 1; i <= n; i++)
	for(int j = 1; j <= m; j++)
	if(map[i][j] != '#' && !isupper( map[i][j] ))	//处理非怪物节点
	{
		for(int k1 = 0; k1 <= 3; k1++)
		{
			int x1 = i + dir[k1][0];
			int y1 = j + dir[k1][1];

			if(!check( x1, y1 ) || isupper( map[x1][y1] )) continue;
			int blood = 0;
			for(int k2 = 0; k2 <= 3; k2++)
			{
				int x2 = x1 + dir[k2][0];
				int y2 = y1 + dir[k2][1];
				if(!check( x2, y2 )) continue;

				if(isupper( map[x2][y2] ))
				{
					blood += map[x2][y2] - 'A' + 1;
				}
			}
			addedge( i, j, x1, y1, blood, 1 );

		}

	}

	//怪物节点特殊处理
	for(int i = 1; i <= n; i++)
	for(int j = 1; j <= m; j++)
	if(isupper( map[i][j] ))
	{
		for(int k1 = 0; k1 <= 3; k1++)
		{
			int x1 = i + dir[k1][0];
			int y1 = j + dir[k1][1];
			if(!check( x1, y1 ))continue;

			for(int k2 = 0; k2 <= 3; k2++) if(k2 != k1)
			{
				int x2 = i + dir[k2][0];
				int y2 = j + dir[k2][1];
				int blood = 0;
				if(!check( x2, y2 ))continue;
				for(int k3 = 0; k3 <= 3; k3++)
				{
					int x3 = x2 + dir[k3][0];
					int y3 = y2 + dir[k3][1];
					if(!check( x3, y3 ) || abs( x3 - x1 ) + abs( y3 - y1 ) <= 1) continue;
					if(isupper( map[x3][y3] ))	blood += map[x3][y3] - 'A' + 1;
				}
				addedge( x1, y1, x2, y2, blood, 2 );
			}
		}
	}
}

int main( )
{

	int kase;
	scanf( "%d", &kase );
	while(kase--)
	{
		ini( );
		create( );
		SPFA( );
		printf( "%d %d\n", dis[dx][dy].first, dis[dx][dy].second );

	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值