NYOJ - 635 - Oh, my goddess(BFS,优先队列)
来源:互联网 发布:蜜桃诱导充值视频源码 编辑:程序博客网 时间:2024/05/16 06:20
Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.
One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.
Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3
seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.
- 输入
- The input consists of blocks of lines. There is a blank line between two blocks.
The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.
O represents empty squares. # means a wall.
At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.
(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.) - 输出
- The least amount of time Shining Knight takes to save hisgoddess in one line.
- 样例输入
3 5O##########O#O#3 4
- 样例输出
14
思路:看了题目后,题目要求是从A点到B点,求出最短路。由于到某个点所需要走的步数不一样,走到#需要4步,O需要一步,所以求最短路要用优先队列。
#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<queue>using namespace std;int x1,y1,x2,y2,ans,n,m;int vis[55][55];int dir[4][2] = {1,0,-1,0,0,1,0,-1};char map[55][55];struct Point{int x,y,steps;friend bool operator<(Point a,Point b){return a.steps>b.steps;}};void bfs(){if(x1==x2&&y1==y2){ans = 0;return ;}priority_queue<Point>q;Point v1;v1.x = x1;v1.y = y1;v1.steps = 0;vis[x1][y1] = 1;q.push(v1);while(!q.empty()){Point v2;for(int i=0 ;i<4 ;i++){v2.x = q.top().x + dir[i][0];v2.y = q.top().y + dir[i][1];if(v2.x>=0&&v2.y>=0&&v2.x<n&&v2.y<m&&!vis[v2.x][v2.y]){if(v2.x==x2&&v2.y==y2){ans = q.top().steps+1;break;}if(map[v2.x][v2.y]=='#'){v2.steps = q.top().steps+4;}else{v2.steps = q.top().steps+1;}vis[v2.x][v2.y] = 1;q.push(v2);}}if(v2.x==x2&&v2.y==y2){break;}q.pop();}}int main(){while(scanf("%d%d",&n,&m)!=EOF&&n&&m){memset(vis,0,sizeof(vis));x1=0; y1=0; ans=-1;for(int i=0 ;i<n ;i++){scanf("%s",map[i]);}scanf("%d%d",&x2,&y2);x2 --;y2 --;bfs();printf("%d\n",ans);}return 0;}
- NYOJ 635 Oh, my goddess (BFS + 优先队列)
- NYOJ 635-Oh, my goddess【bfs+优先队列】
- NYOJ - 635 - Oh, my goddess(BFS,优先队列)
- nyoj 635 Oh, my goddess 优先队列+BFS
- 635 Oh, my goddess【优先队列+bfs】
- NYOJ 635 Oh, my goddess
- NYOJ 635 Oh, my goddess (简单BFS)
- nyoj 635 Oh, my goddess 【bfs(简单题)】
- Oh, my goddess(优先队列)
- nyoj--635--Oh, my goddess(dfs)
- NYOJ-635 Oh, my goddess C语言
- Oh, my goddess(bfs)
- 题目635:Oh, my goddess
- Oh, my goddess
- Oh, my goddess
- oh,my goddess
- oh,my goddess-OJ
- 【NYOJ 284 BFS+优先队列】
- SQL 数据库 学习 018 查询-01 计算列 的用法
- 浅析Webservice
- strongswan 搭建ikev1 for Android and iOS
- 带字数限制的文本域制作
- kibaba 选择字段
- NYOJ - 635 - Oh, my goddess(BFS,优先队列)
- 对象的哈希值
- html5 canvas 画笔透明的实现方法
- 【自考】—信系开管这次我们走心了
- 班级 学号 姓名 map集合映射
- Django自定义访问日志模块
- C语言,从字符串中提取一个字符串,int substr(char dst[], char src[],int start,int len)
- Matplotlib:plotting
- 看菜鸟如何更改Windows下 MySQL5.7 版本的服务名