Oh, my goddess
-
描述
-
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; }
-
The input consists of blocks of lines. There is a blank line between two blocks.