Circling Round Treasures CodeForces - 375C

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\n",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;
}

 

转载于:https://www.cnblogs.com/shenben/p/6394040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值