ACdream 1191(广搜)
来源:互联网 发布:淘宝开店需要多少钱啊 编辑:程序博客网 时间:2024/05/01 02:18
题目链接:http://acdream.info/problem?pid=1191
Dragon Maze
Problem Description
You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find power to save your kingdom and its people. An old legend states that power comes from a place known as Dragon Maze. Dragon Maze appears randomly out of nowhere without notice and suddenly disappears without warning. You know where Dragon Maze is now, so it is important you retrieve some power before it disappears.
Dragon Maze is a rectangular maze, an N×M grid of cells. The top left corner cell of the maze is (0, 0) and the bottom right corner is(N-1, M-1). Each cell making up the maze can be either a dangerous place which you never escape after entering, or a safe place that contains a certain amount of power. The power in a safe cell is automatically gathered once you enter that cell, and can only be gathered once. Starting from a cell, you can walk up/down/left/right to adjacent cells with a single step.
Now you know where the entrance and exit cells are, that they are different, and that they are both safe cells. In order to get out of Dragon Maze before it disappears, you must walk from the entrance cell to the exit cell taking as few steps as possible.
If there are multiple choices for the path you could take, you must choose the one on which you collect as much power as possible in order to save your kingdom.
Input
The first line of the input gives the number of test cases, T(1 ≤ T ≤ 30). T test cases follow.
Each test case starts with a line containing two integers N and M(1 ≤ N, M ≤ 100), which give the size of Dragon Maze as described above.
The second line of each test case contains four integers enx, eny, exx, exy(0 ≤ enx, exx < N, 0 ≤ eny, exy < M), describing the position of entrance cell (enx, eny) and exit cell (exx, exy).
Then N lines follow and each line has M numbers, separated by spaces, describing the N×M cells of Dragon Maze from top to bottom.
Each number for a cell is either -1, which indicates a cell is dangerous, or a positive integer, which indicates a safe cell containing a certain amount of power.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1).
If it's possible for you to walk from the entrance to the exit, y should be the maximum total amount of power you can collect by taking the fewest steps possible.
If you cannot walk from the entrance to the exit, y should be the string "Mission Impossible." (quotes for clarity).
Sample Input
22 30 2 1 02 -1 53 -1 64 40 2 3 2-1 1 1 21 1 1 12 -1 -1 11 1 1 1
Sample Output
Case #1: Mission Impossible.Case #2: 7
思路:直接广搜,用两个数组分别存储 step 和 energy ,然后不断去维护即可,这道题卡了我好久。
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cstdio>#include <cmath>#include <queue>const int INF=1e9;using namespace std;int map[110][110],vis[110][110];int n,m,sx,sy,ex,ey,min_step;int steps[110][110],energy[110][110];int xx[]={0,0,-1,1};int yy[]={-1,1,0,0};struct node{ int x,y;};bool judge(int x,int y){ return x>=0&&x<n&&y>=0&&y<m;}void bfs(){ memset(vis,0,sizeof(vis)); memset(steps,0,sizeof(steps)); memset(energy,0,sizeof(energy)); queue<node>que; node q,p; q.x=sx,q.y=sy; que.push(q); vis[sx][sy]=1; steps[sx][sy]=0; energy[sx][sy]=map[sx][sy]; while(!que.empty()) { q=que.front(); que.pop(); if(steps[q.x][q.y]>min_step)continue; if(q.x==ex&&q.y==ey) { min_step=steps[q.x][q.y]; } for(int i=0;i<4;i++) { p.x=q.x+xx[i]; p.y=q.y+yy[i]; if(!judge(p.x,p.y))continue; if(map[p.x][p.y]==-1)continue; if(vis[p.x][p.y]&&steps[p.x][p.y]==steps[q.x][q.y]+1) { energy[p.x][p.y]=max(energy[p.x][p.y],energy[q.x][q.y]+map[p.x][p.y]); continue; } energy[p.x][p.y]=energy[q.x][q.y]+map[p.x][p.y]; steps[p.x][p.y]=steps[q.x][q.y]+1; vis[p.x][p.y]=1; que.push(p); /* if(!vis[p.x][p.y]||steps[p.x][p.y]==steps[q.x][q.y]+1) { energy[p.x][p.y]=max(energy[p.x][p.y],energy[q.x][q.y]+map[p.x][p.y]); } if(vis[p.x][p.y]||map[p.x][p.y]==-1|| !judge(p.x,p.y))continue; vis[p.x][p.y]=1; steps[p.x][p.y]=steps[q.x][q.y]+1; que.push(p); */ } }}int main(){ int T,test=1; cin>>T; while(T--) { scanf("%d%d",&n,&m); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&map[i][j]); min_step=INF; bfs(); if(min_step==INF) { printf("Case #%d: Mission Impossible.\n",test++); } else printf("Case #%d: %d\n",test++,energy[ex][ey]); } return 0;}
- ACdream 1191(广搜)
- ACdream之使用vector和queue进行邻接表的广搜
- 广搜
- 广搜
- 广搜
- 广搜
- 广搜
- 广搜
- ACDream 1191 - Dragon Maze
- ACdream
- Acdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- ACdream
- quick-cocos2d-x游戏开发【14】——StateMachine状态机
- C++双链表
- Oracle数据库操作常用语句
- 安装SQL Server 2012 时出现 ERROR WHILE ENABLING WINDOWS FEATURE : NetFx3, ERROR CODE : -2146498298 的解决办法
- 【2014最新】iOS App 提交上架store 详细流程
- ACdream 1191(广搜)
- Dynamics CRM2013 任务列表添加自定义按钮
- 重新建站记:上
- asp.net开发细节
- ubuntu Nodejs和npm的安装
- 函数局部变量造成的野指针问题(编译器VS2010)
- 第十五章 15.6节练习 & 15.7.1节练习
- Linux 下 Redis 安装详解
- 基于cocos2d-x 的雷电源码分析