NYOJ 635 Oh, my goddess (BFS + 优先队列)
来源:互联网 发布:小米电视软件安装 编辑:程序博客网 时间:2024/05/16 08:24
Oh, my goddess
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
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
- 来源
郑大第六届校赛
#include <iostream>#include <queue>#include <cstring>using namespace std;const int maxn = 55;int n, m;char maze[maxn][maxn];int a, b;bool vis[maxn][maxn];int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};struct node{ int x, y, step; bool friend operator < (node a, node b){ return a.step > b.step; }};int bfs(){ node now, next; now.x = 0, now.y = 0, now.step = 0; priority_queue<node> q; q.push(now); vis[now.x][now.y] = 1; while (!q.empty()){ now = q.top(); q.pop(); if (now.x == a - 1 && now.y == b - 1) return now.step; for (int i = 0; i < 4; i++){ next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; if (next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[next.x][next.y]){ if (maze[next.x][next.y] == '#') next.step = now.step + 4; else next.step = now.step + 1; q.push(next); vis[next.x][next.y] = 1; } } } return 0;}int main(){ while (cin >> n >> m){ memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) cin >> maze[i]; cin >> a >> b; cout << bfs() << endl; } return 0;}
0 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 (简单BFS)
- Oh, my goddess(优先队列)
- nyoj--635--Oh, my goddess(dfs)
- NYOJ 635 Oh, my goddess
- Oh, my goddess(bfs)
- nyoj 635 Oh, my goddess 【bfs(简单题)】
- NYOJ-635 Oh, my goddess C语言
- 题目635:Oh, my goddess
- Oh, my goddess
- Oh, my goddess
- oh,my goddess
- oh,my goddess-OJ
- 【NYOJ 284 BFS+优先队列】
- Android 调用.so包时报错:No implementation found for native Lxxx, java.lang.UnsatisfiedLinkError: XXX时的解决办法
- Windows下Cmake和VS联合使用dll
- 使用新的Kafka消费者客户端
- MySQL 性能优化
- 每日一命令(11)ln - make links between files
- NYOJ 635 Oh, my goddess (BFS + 优先队列)
- Axure教程 axure新手入门基础
- 正则表达式30分钟入门教程
- leetcode same tree
- 如何实现JS_MD5加密
- oracle 新建表空间 新建库 新建用户
- 组合数模任意数
- 可以节约很多代码的几个正则表达式
- JSON语法解析