zoukankan      html  css  js  c++  java
  • hdu 5040 Instrusive【BFS+优先队列】

    11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy

    先转一个优先队列的用法:

    http://www.cppblog.com/shyli/archive/2007/04/06/21366.html

    优先队列用法

    在优先队列中,优先级高的元素先出队列。 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 优先队列的第一种用法,也是最常用的用法:

    priority_queue<int> qi;

    通过<操作符可知在整数中元素大的优先级高。 故示例1中输出结果为:9 6 5 3 2
    第二种方法: 在示例1中,如果我们要把元素从小到大输出怎么办呢? 这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

    priority_queue<int, vector<int>, greater<int> >qi2;

    其中 第二个参数为容器类型。 第二个参数为比较函数。 故示例2中输出结果为:2 3 5 6 9
    第三种方法: 自定义优先级。

    struct node {     friend bool operator< (node n1, node n2)     {         return n1.priority < n2.priority;     }     int priority;     int value; };

    在该结构中,value为值,priority为优先级。 通过自定义operator<操作符来比较元素中的优先级。 在示例3中输出结果为: 优先级  值 9          5 8          2 6          1 2          3 1          4 但如果结构定义如下:

    struct node {     friend bool operator> (node n1, node n2)     {         return n1.priority > n2.priority;     }     int priority;     int value; };

    则会编译不过(G++编译器) 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。 而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
    //代码清单

    #include<iostream> #include<functional> #include<queue> using 

    Instrusive

    Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 138 Accepted Submission(s): 34

    Problem Description
    The legendary mercenary Solid Matt gets a classic mission: infiltrate a military base.
    The military base can be seen as an N * N grid. Matt's target is in one of the grids and Matt is now in another grid.
    In normal case, Matt can move from a grid to one of the four neighbor grids in a second. But this mission is not easy.
    Around the military base there are fences, Matt can't get out of the base.
    There are some grids filled with obstacles and Matt can't move into these grids.
    There are also some surveillance cameras in the grids. Every camera is facing one of the four direction at first, but for every second, they will rotate 90 degree clockwisely. Every camera's sight range is 2, which means that if Matt is in the same grid as the camera, or in the grid that the camera is facing, he will be seen immediately and the mission will fail.
    Matt has a special equipment to sneak: a cardbox. Matt can hide himself in the card box and move without being noticed. But In this situation, Matt will have to use 3 seconds to move 1 grid. Matt can also just hide in the cardbox without moving. The time to hide and the time to get out of the cardbox can be ignored.
    Matt can't take the risk of being noticed, so he can't move without cardbox into a grid which is now insight of cameras or from a grid which is now insight of cameras. What's more, Matt may be in the cardbox at the beginning.
    As a live legend, Matt wants to complete the mission in the shortest time.
     
    Input
    The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
    For each test cases, the first line contains one integer:N(1<=N<=500)
    In the following N lines, each line contains N characters, indicating the grids.
    There will be the following characters:
    ● '.' for empty ● '#' for obstacle ● 'N' for camera facing north ● 'W' for camera facing west ● 'S' for camera facing south ● 'E' for camera facing east ● 'T' for target ● 'M' for Matt
     
    Output
    For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.
    If Matt cannot complete the mission, output '-1'.
     
    Sample Input
    2 3 M.. .N. ..T 3 M.. ### ..T
     
    Sample Output
    Case #1: 5 Case #2: -1
     
    Source
     
    Recommend
    hujie
     
    题意:在一个NxN的方格图中,有些位置有摄像头,每一秒都会顺时针转90度,摄像头能看守的范围是2个格子(摄像头所在的和面对的一个格子),然后要从起点走到终点,你有一个箱子,当你藏在箱子里面移动时,摄像头照到你也米有事,但是这样走一格需要3秒,不藏在箱子里走需要1秒,最后要求完成任务最少的时间。
     
    思路:首先,题目说了,当一个格子被摄像头照着时,你又要进入这个格子,那么你就必须要用箱子。当你从一个正在被摄像头照着的格子出去时,也必须要用箱子。注意到摄像头的运动是会循环的,当你的当前时间d,根据d%4能直接得出摄像头正在照的方向。 所以我们用d[r][c][mod] 表示当前在(r,c),并且时间%4 为mod时,的最少时间。 那么我们转移的时候也可以选择不动,其他的转移根据摄像头的状态判断一下就好了。
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 #include<stack>
     10 #include<string>
     11 
     12 #define N 505
     13 #define M 10000002
     14 #define mod 10000007
     15 //#define p 10000007
     16 #define mod2 100000000
     17 #define inf 1000000000
     18 //#define ll long long
     19 //#define LL long long
     20 #define maxi(a,b) (a)>(b)? (a) : (b)
     21 #define mini(a,b) (a)<(b)? (a) : (b)
     22 
     23 using namespace std;
     24 
     25 int T;
     26 int n;
     27 int mi[N][N];
     28 char s[N][N];
     29 int dirx[]={-1,0,1,0};
     30 int diry[]={0,1,0,-1};
     31 
     32 struct PP
     33 {
     34     friend bool operator< (PP n1, PP n2)
     35     {
     36         return n1.time > n2.time;
     37     }
     38     int x;
     39     int y;
     40     int time;
     41 };
     42 
     43 PP start,end;
     44 
     45 void ini()
     46 {
     47     int i,j;
     48     scanf("%d",&n);
     49     //memset(s,-1,sizeof(s));
     50 
     51     for(i=0;i<=n+1;i++){
     52         for(j=0;j<=n+1;j++){
     53            s[i][j]='#';
     54         }
     55     }
     56 
     57     for(i=1;i<=n;i++){
     58         scanf("%s",s[i]+1);
     59     }
     60     for(i=1;i<=n;i++){
     61         for(j=1;j<=n;j++){
     62             mi[i][j]=inf;
     63             if(s[i][j]=='M'){
     64                 start.x=i;
     65                 start.y=j;
     66                 mi[i][j]=0;
     67             }
     68             else if(s[i][j]=='T'){
     69                 end.x=i;
     70                 end.y=j;
     71             }
     72 
     73             else if(s[i][j]=='N'){
     74                 s[i][j]=2;
     75             }
     76 
     77             else if(s[i][j]=='W' ){
     78                 s[i][j]=3;
     79             }
     80 
     81             else if(s[i][j]=='S'  ){
     82                 s[i][j]=0;
     83             }
     84 
     85             else if(s[i][j]=='E'  ){
     86                 s[i][j]=1;
     87             }
     88         }
     89     }
     90 }
     91 
     92 int ok1(int i,int j,int time)
     93 {
     94     if(i>=1 && i<=n && j>=1 && j<=n && s[i][j]!='#' && time<mi[i][j])
     95   // if(s[i][j]!='#' && time<mi[i][j])
     96         return 1;
     97     else
     98         return 0;
     99 }
    100 
    101 int ok2(int i,int j,int time)
    102 {
    103     int ni,nj;
    104     int o;
    105     //if(s[i][j]=='N' || s[i][j]=='W' || s[i][j]=='S' || s[i][j]=='E'  )
    106     //    return 0;
    107     for(o=0;o<4;o++){
    108         ni=i+dirx[o];
    109         nj=j+diry[o];
    110         if(ni>=1 && ni<=n && nj>=1 && nj<=n){
    111             if(s[ni][nj]>=0 && s[ni][nj]<=3){
    112                 if((time%4)==(o+s[ni][nj])%4 ){
    113                     return 0;
    114                 }
    115             }
    116             /*
    117             if(s[ni][nj]=='N' && (time%4)==(o+2)%4 ){
    118                 return 0;
    119             }
    120 
    121             if(s[ni][nj]=='W' && (time%4)==(o+3)%4 ){
    122                 return 0;
    123             }
    124 
    125             if(s[ni][nj]=='S' && (time%4)==(o)%4 ){
    126                 return 0;
    127             }
    128 
    129             if(s[ni][nj]=='E' && (time%4)==(o+1)%4 ){
    130                 return 0;
    131             }
    132             */
    133         }
    134     }
    135     return 1;
    136 }
    137 
    138 void bfs()
    139 {
    140     int i,j;
    141     start.time=0;
    142    // queue<PP> q;
    143     priority_queue<PP> q;
    144     PP now,nx;
    145     q.push(start);
    146     while(q.size()>=1)
    147     {
    148         now=q.top();
    149         if(now.x==end.x && now.y==end.y) break;
    150         q.pop();
    151         nx=now;
    152         nx.time++;
    153         for(i=0;i<4;i++){
    154             nx.x=now.x+dirx[i];
    155             nx.y=now.y+diry[i];
    156             if(ok1(nx.x,nx.y,nx.time)==0) continue;
    157 
    158             if(nx.time+2<mi[nx.x][nx.y]){
    159                 nx.time+=2;
    160                 mi[nx.x][nx.y]=nx.time;
    161                 q.push(nx);
    162                 nx.time-=2;
    163             }
    164 
    165             if(s[now.x][now.y]>=0 && s[now.x][now.y]<=3){
    166                 continue;
    167             }
    168             if(s[nx.x][nx.y]>=0 && s[nx.x][nx.y]<=3){
    169                 continue;
    170             }
    171            // if(s[now.x][now.y]=='N' || s[now.x][now.y]=='W' || s[now.x][now.y]=='S' || s[now.x][now.y]=='E'  )
    172                // continue;
    173            // if(s[nx.x][nx.y]=='N' || s[nx.x][nx.y]=='W' || s[nx.x][nx.y]=='S' || s[nx.x][nx.y]=='E'  )
    174              //   continue;
    175            // if(s[now.x][now.y]>3 && s[nx.x][nx.y]>3  ){
    176                 for(j=0;j<=1;j++){
    177                 if(nx.time+j>=mi[nx.x][nx.y]) continue;
    178                     if(ok2(now.x,now.y,now.time+j)==1 && ok2(nx.x,nx.y,now.time+j)==1) {
    179                         nx.time+=j;
    180                         mi[nx.x][nx.y]=nx.time;
    181                         q.push(nx);
    182                         nx.time-=j;
    183                         break;
    184                     }
    185                 }
    186            // }
    187 
    188 
    189 
    190 
    191         }
    192     }
    193 }
    194 
    195 void out()
    196 {
    197     if(mi[end.x][end.y]==inf){
    198         printf("-1
    ");
    199     }
    200     else{
    201         printf("%d
    ",mi[end.x][end.y]);
    202     }
    203 }
    204 
    205 int main()
    206 {
    207     //freopen("data.in","r",stdin);
    208     //freopen("data.out","w",stdout);
    209     scanf("%d",&T);
    210     for(int cnt=1;cnt<=T;cnt++)
    211    // while(T--)
    212    // while(scanf("%d%d",&n,&m)!=EOF)
    213     {
    214       //  if(n==0 && m==0) break;
    215         printf("Case #%d: ",cnt);
    216         ini();
    217         bfs();
    218         out();
    219     }
    220 
    221     return 0;
    222 }
  • 相关阅读:
    Java IO学习3:字节字符转换流
    Java IO学习8:System类对IO的支持
    设计模式(一)单例模式
    复制excel下拉框的数值
    iis7.5+win2008 出现 HTTP Error 503. The service is unavailable.
    php显示当前数据库名称
    解决secureCRT显示中文为乱码
    Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e23 ' [Microsoft][ODBC SQL
    jquery textSlider 文字滚动
    donetcms与Discuz整合的webconfig设置
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3994634.html
Copyright © 2011-2022 走看看