Oh, my goddess(bfs)

来源:互联网 发布:div js 显示隐藏 编辑:程序博客网 时间:2024/05/16 09:52

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
 /*分类:来源:思路:We are giants.create by Lee_SD on 2017/4/*/#include<queue>#include<algorithm>#include<string.h>#include<stdio.h>using namespace std;int n,m;char map[55][55];int st,ed;int vis[55][55];int dir[4][2]={-1,0,1,0,0,-1,0,1};struct Node{int x,y;int step;//由小到大 friend bool operator <(Node n1,Node n2){return n1.step>n2.step;} };int sum;int  bfs(int x,int y){Node q,tem;priority_queue<Node>que;q.x=x;q.y=y;q.step=0;memset(vis,0,sizeof(vis));que.push(q);vis[x][y]=1;while(!que.empty()){q=que.top();que.pop();for(int i=0;i<4;i++){tem.x=q.x+dir[i][0];tem.y=q.y+dir[i][1];if(tem.x<=0||tem.x>n||tem.y>m||tem.y<=0||vis[tem.x][tem.y])continue;if(map[tem.x][tem.y]=='O'){tem.step=q.step+1;vis[tem.x][tem.y]=1;}else if(map[tem.x][tem.y]=='#'){tem.step=q.step+4;vis[tem.x][tem.y]=1;}if(tem.x==st&&tem.y==ed)return tem.step;que.push(tem);vis[tem.x][tem.y]=1;}}return 0;}int main(){while(~scanf("%d%d",&n,&m)){memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf(" %c",&map[i][j]);scanf("%d%d",&st,&ed);printf("%d\n",bfs(1,1));}}        


0 0
原创粉丝点击