zoukankan      html  css  js  c++  java
  • zoj3652Maze(广搜+优先队列+移位+与,或运算)

    Maze
    Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 45 Accepted Submission(s) : 14
    Problem DescriptionCelica is a brave person and believer of a God in the bright side. He always fights against the monsters that endanger humans. One day, he is asked to go through a maze to do a important task.
    The maze is a rectangle of n*m, and Celica is at (x1, y1) at the beginning while he needs to go to (x2, y2). And Celica has a Mobility of l. When he moves a step the movility will decreased by 1. If his mobility equals to 0, he can't move anymore in this turn. And no matter how much mobility Celica uses in one turn, his movility will become l again in the next turn. And a step means move from one lattice to another lattice that has an adjacent edge.
    However, due to the world's rule and the power of magic, there is something called Dominance in the maze. Some lattices may be dominated by some monsters and if Celica goes into these lattices, his mobility will be reduced to 0 at once by the monster's magic power. And monsters have strong "Domain awareness" so one lattice won't be dominated by more than one monster.
    But luckily, Celica gets a strong power from his God so that he can kill this monsters easily. If Celica goes into a lattice which a monster stands on, he can kill the monster without anytime. If a monsters is killed, the lattices it dominates will no longer be dominated by anyone(or we can say they are dominated by Celica) and these lattices will obey the rule of mobility that normal lattices obey.
    As for the task is so important that Celica wants to uses the least turn to go to (x2, y2). Please find out the answer.

    PS1: It doesn't matter if Celica doesn't kill all the monsters in the maze because he can do it after the task and a monster may appear at a lattice that is not dominated by it, even a lattice that is not dominated by any monsters.

    PS2: We define (1,1) as the top left corner. And monsters won't move.

    PS3: No matter which lattice Celia gets in, the change of mobility happens first.

    PS4: We promise that there is no two monsters have same position and no monster will appear at the start point of Celica.

    Input

    The first contains three integers, n, m, l.(1≤n, m≤50, 1≤l≤10)

    Then there follows n lines and each line contains m integers. The j-th integer p in the line i describe the lattice in the i line and j row. If p euqals to -1, it means you can't get into it. If p euqals to 0, it means the lattice is not dominated by any monster. If p is larger than 0, it means it is dominated by the p-th monster.

    And then in the n+2 line, there is an integer k(0≤k≤5) which means the number of monster.

    Then there follows k lines. The i-th line has two integers mean the position of the i-th monster.

    At last, in the n+k+3 lines, there is four integers x1, y1, x2, y2.

    Output
    If Celica can't get to the (x2, y2), output "We need God's help!", or output the least turn Celica needs.
    Sample Input
    5 5 4
    2 2 2 1 0
    -1 2 2 -1 1
    2 2 2 1 1
    1 1 1 1 0
    1 2 2 -1 0
    2
    4 2
    1 1
    5 1 1 5
    5 5 4
    1 1 1 1 1
    1 2 2 -1 -1
    2 2 -1 2 2
    -1 -1 2 2 2
    2 2 2 2 2
    2
    2 2
    1 2
    1 1 5 5
    Sample Output
    4
    We need God's help!
    Hit
    In the first case, Celica goes to (4,1) in turn 1. Then he goes to (4,2) in turn 2. After he gets (4,2), kill the monster 1. He goes through (4,3)->(4,4)>(3,4)->(3,5) in turn 3. At last he goes (2,5)->(1,5) in turn 4.

    题意:

    题目大意:打怪兽。给一个n*m的地图,其中1~5表示该位置被第i个怪兽管理着,0表示空地可以走,-1表示这个位置不可以走。

    再输入一个k,接下去k行怪兽所在的坐标。

    最后一行就是Celica的起点和终点,问最少需要多少回合才能走到终点。

    特别注意:

    1、每一个回合开始,Celica的初始体力值为L,每移动一次,体力值减1,当体力值==0时,开始下一回合。

    2、只要进入怪兽所管理的位置,体力值就瞬间变为0。

    3、如果进入到怪兽所在的位置,就要将怪兽打死,体力值变为0,。与此同时,地图上怪兽的位置和怪兽所控制的位置全部变为0(无障碍空地)。

    4、起点没有怪,但可能被某一个怪兽控制。

    5、如果起点和终点重合,直接输出0。即回合数为0.

    6、怪兽不一定在他控制的位置,被一号怪兽控制的区域也可以是2号怪兽。

    7、复活过得地方不会在死一次了,并且起始点就算是复活地。

    8、在到达终点的时候,体力值刚好变为0的话,不会再另外算一个回合,需要特殊判断。

    9、如果不能到达终点的话,记得输出We need God's help!

      1 #include<iostream>
      2 #include <cstdio>
      3 #include<cstdlib>
      4 #include<cstring>
      5 //#include <string>//定义string类型的变量时必须加上这个头文件,当然这道题没用到
      6 #include<queue>
      7 using namespace std;
      8 int n,m,l,k;
      9 int map[55][55];
     10 int dir[4][2]={0,1,0,-1,1,0,-1,0};//方向数组
     11 int visit[55][55][100];//标记走过的点
     12 struct node
     13 {
     14     int x;
     15     int y;
     16     int step;   //体力步数
     17     int t;  //回合数
     18     int mark;//标记第几只怪兽是否被打死
     19 };
     20 bool operator < (node n1,node n2) //优先队列,回合少的优先,回合相等,剩余体力多的优先
     21     {
     22         if(n1.t!=n2.t)
     23             return n1.t>n2.t;
     24         return n1.step<n2.step;
     25     }
     26 node kk[10],st,ed,node1,node2;
     27 void bfs()
     28 {
     29     priority_queue<node> q;//优先队列
     30     while(!q.empty())
     31     q.pop();
     32     node1.x=st.x;
     33     node1.y=st.y;
     34     node1.step=l;//初始体力为l
     35     node1.t=1;//初始为第一个回合
     36     node1.mark=0;//初始怪兽都没打死
     37     memset(visit,0,sizeof(visit));
     38     visit[node1.x][node1.y][node1.mark]=1;
     39     q.push(node1);
     40     while(!q.empty())
     41     {
     42         node1=q.top();//得到队首的值
     43         q.pop();
     44         if(node1.x==ed.x&&node1.y==ed.y)//走到终点
     45         {
     46             if(node1.step==l)  //终点是的体力为满值就可以少一个回合
     47             printf("%d
    ",node1.t-1);
     48             else
     49             printf("%d
    ",node1.t);
     50             return;
     51         }
     52         for(int i=0;i<4;i++)
     53         {
     54             node2.x=node1.x+dir[i][0];
     55             node2.y=node1.y+dir[i][1];
     56             node2.t=node1.t;
     57             node2.mark=node1.mark;
     58             if(node2.x<=0||node2.x>n||node2.y<=0||node2.y>m)//越界返回
     59             continue;
     60             if(map[node2.x][node2.y]==-1)//墙返回
     61             continue;
     62             if(visit[node2.x][node2.y][node2.mark])//走过返回
     63             continue;
     64             if(map[node2.x][node2.y]==0||(node2.mark&(1<<(map[node2.x][node2.y])))!=0)//没有被怪兽控制或者是怪兽被打败了
     65             node2.step=node1.step-1;
     66             else  //否者被怪兽控制着
     67             {
     68                 node2.step=0;
     69             }
     70             for(int i=1;i<=k;i++)//有k只怪兽
     71             {
     72                 if(node2.x==kk[i].x&&node2.y==kk[i].y)  //遇到怪兽就把它打死
     73                 {
     74                     node2.mark=node1.mark|(1<<i);//标记怪兽被打死
     75                 }
     76             }
     77             if(node2.step==0)//如果步数被用完
     78             {
     79                 node2.step=l;//就加一回合
     80                 node2.t=node1.t+1;//恢复最大体力
     81             }
     82             visit[node2.x][node2.y][node2.mark]=1;//标记走过
     83             q.push(node2);
     84         }
     85     }
     86     printf("We need God's help!
    ");
     87     return;
     88 }
     89 int main()
     90 {
     91     while(~scanf("%d%d%d",&n,&m,&l))
     92     {
     93         for(int i=1;i<=n;i++)//必须是1--n,第74句需要
     94         {
     95             for(int j=1;j<=m;j++)//必须是1--m,第74句需要
     96             {
     97                 scanf("%d",&map[i][j]);
     98             }
     99         }
    100         scanf("%d",&k);
    101         for(int i=1;i<=k;i++)//必须是1--k,第74句需要
    102         {
    103             scanf("%d%d",&kk[i].x,&kk[i].y);
    104         }
    105         scanf("%d%d%d%d",&st.x,&st.y,&ed.x,&ed.y);
    106         if(st.x==ed.x&&st.y==ed.y)//起点就是终点返回0
    107         {
    108             printf("0
    ");
    109             continue;
    110         }
    111         bfs();
    112     }
    113 }
  • 相关阅读:
    Jquery+ajax+bootstrap
    Js+Jquery
    css(2)+JS
    css
    mysql 高级
    Git
    Redis
    Nginx
    python爬虫 | 一条高效的学习路径
    拉勾网爬取全国python职位并数据分析薪资,工作经验,学历等信息
  • 原文地址:https://www.cnblogs.com/dshn/p/4948330.html
Copyright © 2011-2022 走看看