zoukankan      html  css  js  c++  java
  • Circling Round Treasures CodeForces

    C. Circling Round Treasures
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.

    You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals v, and besides, you've made k single moves (from one cell to another) while you were going through the path, then such path brings you the profit of v - k rubles.

    Your task is to build a closed path that doesn't contain any bombs and brings maximum profit.

    Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm:

    1. Assume that the table cells are points on the plane (the table cell on the intersection of the i-th column and the j-th row is point(i, j)). And the given path is a closed polyline that goes through these points.
    2. You need to find out if the point p of the table that is not crossed by the polyline lies inside the polyline.
    3. Let's draw a ray that starts from point p and does not intersect other points of the table (such ray must exist).
    4. Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point p (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside.
    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 20) — the sizes of the table. Next n lines each contains m characters — the description of the table. The description means the following:

    • character "B" is a cell with a bomb;
    • character "S" is the starting cell, you can assume that it's empty;
    • digit c (1-8) is treasure with index c;
    • character "." is an empty cell;
    • character "#" is an obstacle.

    Assume that the map has t treasures. Next t lines contain the prices of the treasures. The i-th line contains the price of the treasure with index ivi ( - 200 ≤ vi ≤ 200). It is guaranteed that the treasures are numbered from 1 to t. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character "S".

    Output

    Print a single integer — the maximum possible profit you can get.

    Examples
    input
    4 4
    ....
    .S1.
    ....
    ....
    10
    output
    2
    input
    7 7
    .......
    .1###2.
    .#...#.
    .#.B.#.
    .3...4.
    ..##...
    ......S
    100
    100
    100
    100
    output
    364
    input
    7 8
    ........
    ........
    ....1B..
    .S......
    ....2...
    3.......
    ........
    100
    -100
    100
    output
    0
    input
    1 1
    S
    output
    0
    Note

    In the first example the answer will look as follows.

    In the second example the answer will look as follows.

    In the third example you cannot get profit.

    In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell.

    题意:

      要求在一张网格图上走出一条闭合路径,不得将炸弹包围进去,使围出的总价值减去路径长度最大。

    分析:

      大致同poj3182,再加上状态压缩。w[x]表示该状态下的sum{val},dp[x][y][k]表示围圈,在k状态下的最短dis。|状态:选择了1-8中哪几个

      转移的时候 xor一下就好

      ps:bomb怎么搞?直接把他的val=-oo,扔到bfs中。正确性自行脑补

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int N=25;
    const int M=1<<10;
    const int dx[4]={0,0,1,-1};
    const int dy[4]={1,-1,0,0};
    int dp[N][N][M],w[M],val[N];char mp[N][N];
    int n,m,cnt,ans,sx,sy,gx[N],gy[N],px,py,pk,nx,ny,nk;
    struct node{
        int x,y,k;
        node(int x=0,int y=0,int k=0):x(x),y(y),k(k){}
    };
    bool ok(int j){
        if(nx==gx[j]&&ny<gy[j]){
            if(px<nx) return 1;
        }
        if(px==gx[j]&&py<gy[j]){
            if(px>nx) return 1;
        }
        return 0;
    }
    void bfs(){
        memset(dp,-1,sizeof dp);
        queue<node>q;
        q.push(node(sx,sy,0));
        dp[sx][sy][0]=0;
        while(!q.empty()){
            node t=q.front();q.pop();
            px=t.x;py=t.y;pk=t.k;
            if(px==sx&&py==sy) ans=max(ans,w[pk]-dp[px][py][pk]);
            for(int i=0;i<4;i++){
                nx=px+dx[i];ny=py+dy[i];nk=pk;
                if(nx<1||ny<1||nx>n||ny>m||(mp[nx][ny]!='.'&&mp[nx][ny]!='S')) continue;
                for(int j=0;j<cnt;j++)
                    if(ok(j)) nk^=1<<j;
                if(dp[nx][ny][nk]==-1){
                    dp[nx][ny][nk]=dp[px][py][pk]+1;
                    q.push(node(nx,ny,nk));
                }
            }
        }
        printf("%d
    ",ans);
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
        for(int i=1,p;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='S') sx=i,sy=j;
                if(mp[i][j]>'0'&&mp[i][j]<'9'){
                    p=mp[i][j]-'1';cnt++;
                    gx[p]=i;gy[p]=j;
                }
            }
        }
        for(int i=0;i<cnt;i++) scanf("%d",&val[i]);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='B'){
                    gx[cnt]=i;gy[cnt]=j;val[cnt]=-100000;
                    cnt++;
                }
            }
        }
        w[0]=0;
        for(int S=1;S<(1<<cnt);S++){
            for(int i=0;i<cnt;i++){
                if(S&(1<<i)) w[S]+=val[i];
            }
        }
        bfs();
        return 0;
    }
  • 相关阅读:
    21. Node.Js Buffer类(缓冲区)-(一)
    20.Node.js EventEmitter的方法和事件
    19.Node.js EventEmitter
    18.Node.js 事件循环
    17.Node.js 回调函数--异步编程
    16.REPL 命令
    15.Node.js REPL(交互式解释器)
    14.NPM 常用命令
    **PHP Notice: Undefined index:...问题的解决方法
    **CI中的order_by在get_where之前
  • 原文地址:https://www.cnblogs.com/shenben/p/6394040.html
Copyright © 2011-2022 走看看