您的位置:首页 > 大数据 > 人工智能

hdu 4308 Saving Princess claire BFS加优先队列

2012-07-20 17:27 309 查看


Problem I

Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 628    Accepted Submission(s): 173

Problem Description
Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.

Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.

The maze can be described as a matrix of r rows and c columns, with grids, such as 'Y', 'C', '*', '#' and 'P', in it. Every grid is connected with its up, down, left and right grids.

There is only one 'Y' which means the initial position when Prince ykwd breaks into the maze.

There is only one 'C' which means the position where Princess claire_ is jailed.

There may be many '*'s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.

The grid represented by '#' means that you can not pass it.

It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.

'P' means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another.

They say that there used to be some toll stations, but they exploded(surely they didn't exist any more) because of GDM teoy's savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his
mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.

Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn't want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he
needs to take his princess back.
 
Input
Multiple cases.(No more than fifty.)

The 1st line contains 3 integers, r, c and cost. 'r', 'c' and 'cost' is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )

Then an r * c character matrix with 'P' no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.

 
Output
One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)
 
SampleInput

 1 3 3
Y*C
1 3 2
Y#C
1 5 2
YP#PC
SampleOutput
3
Damn teoy!



 

题意: Y开始点  C 终止点 P 传送门  与所有传送门连同     # 墙   *能走的地方 但是要钱  只有*要钱

输入 行数  列数  以及钱  

问花的 最少的钱是多少

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<queue>

using namespace std;

int hang,lie,val,cnt;

char map[5100][5100];

struct haha

{

 int x;

 int y;

 int val;

 friend bool operator<(struct haha a,struct haha b)

 {

  return a.val>b.val;

 }

}q,temp;

struct p

{

 int x;

 int y;

}a[5000+10];

int step[4][2]={1,0,-1,0,0,1,0,-1},s_x,s_y,e_x,e_y;//把坐标存成了字符型 艾 一天全是低级错误  脑子不清醒

int BFS()

{

 int i,j,xx,yy,ans=999999999;

 priority_queue<haha>que;

 q.x=s_x;

 q.y=s_y;/////////  已开始这里写成了e_y  想打自己啊

 q.val=0;

 que.push(q);

 map[s_x][s_y]='#';

 while(!que.empty())

 {

  temp=que.top();

  que.pop();

  for(i=0;i<4;i++)

  {

               xx=temp.x+step[i][0];

      yy=temp.y+step[i][1];

      if(xx>=0&&xx<hang&&yy>=0&&yy<lie&&map[xx][yy]!='#')

      {

       if(xx==e_x&&yy==e_y)

       {

        if(ans>temp.val) ans=temp.val;continue;

       }

       if(map[xx][yy]=='*')

       {

        map[xx][yy]='#';

        q.x=xx;

           q.y=yy;

        q.val=temp.val+val;//////这里忘记加temp.val   MLE了

        que.push(q);

        continue;

       }

       if(map[xx][yy]=='P')

       {

                         for(j=0;j<cnt;j++)

       {

        if(map[a[j].x][a[j].y]=='P')

        {

        q.x=a[j].x;

        q.y=a[j].y;

        q.val=temp.val;

        que.push(q);

        map[q.x][q.y]='#';

        }

       }

       }

      }

  }

       

 }

 return ans;

}

int main()

{

 int i,j,ans;

 while(scanf("%d %d %d",&hang,&lie,&val)!=EOF)

 {

  cnt=0;

            for(i=0;i<hang;i++)

   {

    scanf("%s",map[i]);

    for(j=0;j<lie;j++)

     if(map[i][j]=='Y') {s_x=i;s_y=j;}

     else if(map[i][j]=='C') {e_x=i;e_y=j;}

     else if(map[i][j]=='P') {a[cnt].x=i;a[cnt++].y=j;}

   }

   ans=BFS();

   if(ans!=999999999)

   printf("%d\n",ans);

   else printf("Damn teoy!\n");

 }

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息