NYOJ 635 Oh, my goddess (BFS + 优先队列)
2016-06-17 16:55
375 查看
题目635
题目信息
运行结果
本题排行
讨论区
时间限制: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.
样例输入
样例输出
来源
郑大第六届校赛
题目信息
运行结果
本题排行
讨论区
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 5 O#### ##### #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; }
相关文章推荐
- googleplay经验总结
- Goaccess 日志分析工具
- Visualizing Concurrency in Go · divan's blog
- XiaoMi/themis: Themis provides cross-row/cross-table transaction on HBase based on google's percolator.
- 论文笔记:Inception-V4, Inception-ResNet
- 论文笔记:Going deeper with convolution
- 我们一起成长(勿忘初心)
- Django进阶2
- Google背后的开源力量
- lingo解题报告内容解释
- ccah-500 第46题 Which scenario will go undetected
- Google Chrome Source Code 浏览器源码(下载)
- Google员工自述哈佛教书和Google工作的差别
- 我的第一篇博客--感谢google的StudyJams活动
- 制作.bat文件快速启动Mongo服务
- 制作一个简单的webCrawler - 以Goodreads上的作家quotes为例+爬取Finance Yahoo上的公司股价信息
- Django Customer User and Authentication Backend
- pgoneproxy 通过vip实现24小时不间断服务
- django中tinymce添加图片上传功能
- MIT Introduction to Algorithms Record-8