当前位置: 首页 > 工具软件 > Goddess > 使用案例 >

Oh, my goddess

仇阳州
2023-12-01

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
来源
郑大第六届校赛
上传者

ACM_赵铭浩

思路:优先队列+bfs(),还有就是我提交了几次。之前那个边界范围弄错了。我是下标从1开始而不是从0开始的。

#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
using namespace std;
char a[500][500];
int c[4]={0,1,0,-1};
int d[4]={1,0,-1,0};
int visited[500][500],b[500][500];
int M,N;
struct point
{
	int xx;
	int yy;
	int cost;
 friend bool operator <(point n1,point n2)  
    {  
        return n1.cost>n2.cost;  
    }  
};
void bfs(int x,int y)
{
	int i;
	priority_queue<point>q;
	point now,next;
	now.xx=1;
	now.yy=1;
	now.cost=0;
	visited[1][1]=1;
	q.push(now);
	while(!q.empty())
	{
		next=q.top();
		q.pop();
		for(i=0;i<4;i++)
		{
		  now.xx=next.xx+c[i];
		  now.yy=next.yy+d[i];
		  if(now.xx==x &&now.yy==y)
          {
			  cout<<next.cost+1<<endl;
			  return ;
		  }
          if(!visited[now.xx][now.yy]&&(now.xx<=M && now.yy<=N &&now.xx>=1 &&now.yy>=1))
		  {
			  visited[now.xx][now.yy]=1;
              now.cost=next.cost+b[now.xx][now.yy];
              q.push(now);
		  }
		}
	}
}
int main()
{
	int i,j,x,y;
	while(cin>>M>>N)
	{
		memset(visited,0,sizeof(visited));
      for(i=1;i<=M;i++) //3 5
	  {
		  for(j=1;j<=N;j++)
		  {
			 cin>>a[i][j];
		    if(a[i][j]=='O')
		     b[i][j]=1;
            else if(a[i][j]=='#')
		     b[i][j]=4;
		  }
	  }
	  cin>>x>>y;
	  bfs(x,y);
	}
	return 0;
}


 类似资料:

相关阅读

相关文章

相关问答