解题报告 之 ZOJ3877 Earthstone Keeper

解题报告 之 ZOJ3877 Earthstone Keeper

Description

Earthstone Keeper is a famous roguelike game created byLizard Entertainment. In this game, an adventurer will explore in a single layer dungeon ofN×Msize.

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:

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 integerTindicating the number of test cases. For each test case:

The first line contains two integersNandM(1 <=N,M<= 500). The second line contains 4 integersSR,SC,TR,TC(1 <=SR,TR<=Nand 1 <=SC,TC<=M).

For the nextNlines, each line containsMcharacters indicating the map of dungeon. Each type of room is marked as:

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

13 51 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;}

会让你的心态更平和更坦然,也会让你心无旁骛,更会让你的心灵得到解脱和抚慰。

解题报告 之 ZOJ3877 Earthstone Keeper

相关文章:

你感兴趣的文章:

标签云: