UESTC_Just a Maze CDOJ 1162
Just a Maze
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others)
Here is a maze with N × M room.
You start from the room (SR,SC) and want to go to the room located at (TR,TC). However, there are many traps and monsters in this maze.
There are 4 types of rooms:
1. Blank->('.'). which has nothing.
2. Rock->('#'). which you can not enter it.
3. Trap->('a'~'z'), which once you enter it, you will suffer (Trap-'a'+1) damage(s). After you leave,the trap will reset so it can be triggered next time.
4. Monster->('A'~'Z'). If you go into a monster room or any room adjacent to a monster room, the monster will immediately rush up to you and fight with you. You will kill it, but you will get hurt too, suffering (Monster-'A'+1) damage(s). And the monster will not revive.
Two rooms are adjacent if and only if they share an edge. You can take 1 step to go from a room to another adjacent room.
The safest path is a lowest total damage path. Among all safest path,find the path with lowest steps.
Input
The first line contains two integers N and M (1≤N,M≤500).
The second line contains 4 integers SR,SC,TR,TC (1≤SR,TR≤N and 1≤SC,TC≤M).
For the next N lines, each line contains M characters indicating the map of maze. Each type of room is marked as:
1. Blank->('.')
2. Rock->('#')
3. Trap: from 'a'~'z'
4. Monster: from 'A'~'Z'
The damage you suffer from the trap 'a' is 1,'b' is 2..and so on.
The damage you suffer from the monster 'A' is 1... and 'Z' is 26.
The room (SR,SC) and (TR,TC) are always blank rooms and will not be adjacent to any monster room.
Output
Output the lowest total damage and the lowest steps in all safest path.
Sample input and output
Sample Input | Sample Output |
---|---|
3 5 |
4 6 |
Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#define pb push_back
#define show(x) cout << "x is " << x << endl
#define printarray(x,n) for(int i = 0 ; i < n ; ++ i) x == 0 ? cout << x[i] : cout << x[i] << " ";cout << endl
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define loop(x,st,ed) for(int x = st ; x < ed ; ++ x)
#define blackkey
typedef long long ll;
using namespace std;
const int maxn = + ; typedef struct status
{
int x , y ;
status(int x, int y )
{
this->x = x, this->y = y ;
}
}; typedef struct Edge
{
int x , y , co;
Edge(int x, int y , int co)
{
this->x = x , this->y = y , this->co = co;
}
}; typedef struct qst
{
int x , y , d1 , d2;
friend bool operator < (const qst & a , const qst & b)
{
if (a.d1 < b.d1)
return false;
else if(a.d1 == b.d1 && a.d2 < b.d2)
return false;
return true;
}
qst(int x,int y,int d1,int d2)
{
this-> x = x , this->y = y , this->d1 = d1 , this->d2 = d2;
}
}; typedef pair<int,int>pos;
typedef pair<pos,pos>spj;
int n , m , sr , sc , tr , tc , mincost[maxn][maxn][] , dir[][] = {-,,,,,,,-} , damage[maxn][maxn] , ban[maxn][maxn] , damage2[maxn][maxn] ,used[maxn][maxn] ;
char s[maxn][maxn];
vector<Edge>E[maxn][maxn];
set<spj>sb;
priority_queue<qst>q; inline int inmap(int x,int y)
{
return x <= n && x >= && y <= m && y >= ;
} int GetSameDamage(int x1,int y1,int x2,int y2)
{
int ans = ;
loop(i , , )
{
int newx = x1 + dir[i][];
int newy = y1 + dir[i][];
if (s[newx][newy] <= 'Z' && s[newx][newy] >= 'A')
{
loop(j , , )
if (x2 + dir[j][] == newx && y2 + dir[j][] == newy)
{
ans += s[newx][newy] - 'A' + ;
break;
}
}
}
return ans;
} void dijkstra()
{
q.push(qst(sr,sc,,));
memset(mincost,-,sizeof(mincost));
mincost[sr][sc][] = mincost[sr][sc][] = ;
while(!q.empty())
{
int x = q.top().x , y = q.top().y , d1 = q.top().d1 , d2 = q.top().d2 ; q.pop();
if (used[x][y])
continue;
used[x][y] = ;
loop(i , , )
{
int newx = x + dir[i][] , newy = y + dir[i][] ;
if (!inmap(newx,newy) || ban[newx][newy]) continue;
int newhurt = d1 + damage[newx][newy] + damage2[newx][newy] , newcost = d2 + ;
if ( newhurt < mincost[newx][newy][] || !(mincost[newx][newy][] ^ -) )
{
mincost[newx][newy][] = newhurt;
mincost[newx][newy][] = newcost;
q.push(qst(newx,newy,newhurt,newcost));
}
else if( !(newhurt ^ mincost[newx][newy][]) && newcost < mincost[newx][newy][])
{
mincost[newx][newy][] = newcost;
q.push(qst(newx,newy,newhurt,newcost));
}
}
loop(i , , E[x][y].size() )
{
int newx = E[x][y][i].x , newy = E[x][y][i].y , add = E[x][y][i].co;
int newhurt = d1 + damage[newx][newy] + damage2[newx][newy] - add, newcost = d2 + ;
if ( newhurt < mincost[newx][newy][] || !(mincost[newx][newy][] ^ -) )
{
mincost[newx][newy][] = newhurt;
mincost[newx][newy][] = newcost;
q.push(qst(newx,newy,newhurt,newcost));
}
else if( !(newhurt ^ mincost[newx][newy][]) && newcost < mincost[newx][newy][])
{
mincost[newx][newy][] = newcost;
q.push(qst(newx,newy,newhurt,newcost));
}
}
}
} int main(int argc,char *argv[])
{
//local;
memset(damage , , sizeof(damage));
memset(damage2 , , sizeof(damage2));
memset(used,,sizeof(used));
scanf("%d%d%d%d%d%d",&n,&m,&sr,&sc,&tr,&tc);
loop(i , , n + )
scanf("%s",s[i] + );
loop(i , , n + )
loop(j , , m + )
{
if (s[i][j] == '#')
ban[i][j] = ;
else if (s[i][j] <= 'z' && s[i][j] >= 'a')
damage[i][j] = s[i][j] - 'a' + ;
else if(s[i][j] <= 'Z' && s[i][j] >= 'A')
{
ban[i][j] = ;
loop(k , , )
damage2[i + dir[k][]][j + dir[k][]] += s[i][j] - 'A' + ;
loop(kx , , )
{
int newx = i + dir[kx][] ;
int newy = j + dir[kx][] ;
if (!inmap(newx,newy) || s[newx][newy] == '#') continue;
pos p1(newx,newy);
loop(k2 , , )
{
int tx = i + dir[k2][];
int ty = j + dir[k2][];
if ( !inmap(tx,ty) || s[tx][ty] == '#' || (tx ==newx && ty == newy)) continue;
pos p2(tx,ty);
spj ju = make_pair(p1,p2);
if (!sb.count(ju))
{
int co = GetSameDamage(newx,newy,tx,ty);
E[newx][newy].pb(Edge(tx,ty,co));
sb.insert(ju);
} }
}
}
}
dijkstra();
printf("%d %d\n",mincost[tr][tc][],mincost[tr][tc][]);
return ;
}
UESTC_Just a Maze CDOJ 1162的更多相关文章
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- NYOJ题目1162数字
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr8AAAJ/CAIAAAD+kywNAAAgAElEQVR4nO3dO1LjzN4H4G8T5CyE2A ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
随机推荐
- [HEOI 2013 day2] SAO (树形动态规划)
题目大意 给一棵N个节点的有向树(N <= 1000),求其拓扑序列个数. 思路 我们将任意一个点作为根,用dp[i][j]表示以节点i为根的子树满足节点i在第j个位置上的拓扑序列的个数.在求节 ...
- 打开首页老是加载themes.googleusercontent.com_Wopus问答
打开首页老是加载themes.googleusercontent.com_Wopus问答 打开首页老是加载themes.googleusercontent.com
- 浅谈c语言程序为什么需要内存 栈又是什么?
.关于内存(程序的执行需要内存的支持) ()内存本身在物理上是硬件器件,由操作系统提供 ()内存的管理最终由操作系统统一管理.为了能过便捷的管理内存(酒店管理房间 是不是分很多不同的类型和待遇呢),同 ...
- 马士兵 Servlet & JSP(1) Servlet (源代码)
1.HTTP协议基础测试(获取页面源代码) import java.io.BufferedReader; import java.io.IOException; import java.io.Inpu ...
- CodeManage 源代码管理器v2.0发布
下载地址 欢迎大家提出宝贵的意见和bug
- viewstate加密(转)
ViewState在客户端展开的时候,默认是Auto,不加密的,如果页面有限制性的表单控件才加密,所以,可以查看,代码如下: byte[] bytes = Convert.FromBase64Stri ...
- RDLC报表上下标实现
例:m的6次方 ="M"&ChrW(8310) Character Name Character Num Entity Hex Entity Superscript Cha ...
- exc_bad_access(code=1, address=0x789870)野指针错误
原因: exc_bad_access(code=1, address=0x789870)野指针错误,主要的原因是,当某个对象被完全释放,也就是retainCount,引用计数为0后.再去通过该对象去调 ...
- hdu4006 优先队列
A - 签到 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65768KB 64bit I ...
- web多语言的一种处理方式
在开发一种国际化系统时,多语言是必须的. 总的来说处理方式有两种,一种是后端处理,另一种是前端处理.呵呵,有点废话~~ 后端处理没用过,猜猜是在标记需要处理语言的地方进行替换. 前端处理是要先把语言文 ...