NYOJ 635 Oh, my goddess (简单BFS)

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


一个0和O的闹剧,把O看成了0,我也是醉了(衰

ac代码:

 
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
int ex,ey;
char map[55][55];
int hp[55][55];
int v[55][55];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s
{
	int x;
	int y;
	int step;
	friend bool operator<(s n1,s n2)
    {    
        return n2.step<n1.step;    
    } 
};
int check(int xx,int yy)
{
	if(xx<=0||yy<0||xx>n||yy>=m)
	return 1;
	if(v[xx][yy])
	return 1;
	return 0;
}
int bfs()
{
	int i;
	priority_queue<s>q; 
	s a,b;
	a.x=1;
	a.y=0;
	a.step=0;
	v[a.x][a.y]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		if(a.x==ex&&a.y==ey-1)
		{
			return a.step;	
		}
		for(i=0;i<4;i++)
		{
			int nx=a.x+dir[i][0];
			int ny=a.y+dir[i][1];
			if(check(nx,ny)==0)
			{
				b.step=a.step+hp[nx][ny]+1;
				b.x=nx;b.y=ny;
				v[b.x][b.y]=1;
				q.push(b);
		    }
		}
	}
	return 0;
}
int main()
{
	int i,j;
	int sum;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(hp,0,sizeof(hp));
		memset(v,0,sizeof(v));
		for(i=1;i<=n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='O')
				hp[i][j]=0;
				else if(map[i][j]=='#')
				hp[i][j]=3;
			}
		}
		scanf("%d%d",&ex,&ey);
		sum=bfs();
		printf("%d\n",sum);
	}
	return 0;
}                                



你可能感兴趣的:(NYOJ 635 Oh, my goddess (简单BFS))