Oh, my goddess nyoj

一名路過的小碼農啊發表於2017-03-30

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<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
char map[60][60];
int dx,dy;
struct node
{
	int x;int y;int step;
	node(int a,int b,int c):x(a),y(b),step(c){
	}
	node(){
	}
	friend bool operator<(node a,node b)
	{
		return a.step>b.step;
	}
};
int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
bool vis[60][60];
int bfs()
{
	priority_queue<node>q;
	q.push(node(1,1,0));
	vis[1][1]=true;
	while(!q.empty()){
		node cur=q.top();
		q.pop();
		if(cur.x==dx&&cur.y==dy)return cur.step;
		for(int i=0;i<4;i++)
		{
			int xx=cur.x+dir[i][0];
			int yy=cur.y+dir[i][1];
			if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy])continue; 
			vis[xx][yy]=true;
			if(map[xx][yy]=='#')
			{
				q.push(node(xx,yy,cur.step+4));
			}
			if(map[xx][yy]=='O')
			{
			    q.push(node(xx,yy,cur.step+1));
			}
		}
    }
	return 0;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>map[i][j];
			}
		}
		scanf("%d%d",&dx,&dy);
		memset(vis,false,sizeof(vis));
		int res=bfs();
		printf("%d\n",res);
		printf("\n");
	}
	return 0;
}  
很尷尬哎,O看成了o

相關文章