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=;
const int M=<<;
const int dx[]={,,,-};
const int dy[]={,-,,};
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=,int y=,int k=):x(x),y(y),k(k){}
};
bool ok(int j){
if(nx==gx[j]&&ny<gy[j]){
if(px<nx) return ;
}
if(px==gx[j]&&py<gy[j]){
if(px>nx) return ;
}
return ;
}
void bfs(){
memset(dp,-,sizeof dp);
queue<node>q;
q.push(node(sx,sy,));
dp[sx][sy][]=;
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=;i<;i++){
nx=px+dx[i];ny=py+dy[i];nk=pk;
if(nx<||ny<||nx>n||ny>m||(mp[nx][ny]!='.'&&mp[nx][ny]!='S')) continue;
for(int j=;j<cnt;j++)
if(ok(j)) nk^=<<j;
if(dp[nx][ny][nk]==-){
dp[nx][ny][nk]=dp[px][py][pk]+;
q.push(node(nx,ny,nk));
}
}
}
printf("%d\n",ans);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%s",mp[i]+);
for(int i=,p;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='S') sx=i,sy=j;
if(mp[i][j]>''&&mp[i][j]<''){
p=mp[i][j]-'';cnt++;
gx[p]=i;gy[p]=j;
}
}
}
for(int i=;i<cnt;i++) scanf("%d",&val[i]);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='B'){
gx[cnt]=i;gy[cnt]=j;val[cnt]=-;
cnt++;
}
}
}
w[]=;
for(int S=;S<(<<cnt);S++){
for(int i=;i<cnt;i++){
if(S&(<<i)) w[S]+=val[i];
}
}
bfs();
return ;
}

Circling Round Treasures CodeForces - 375C的更多相关文章

  1. CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]

    C - Circling Round Treasures 题意: 在一个$n*m$的地图上,有一些障碍,还有a个宝箱和b个炸弹.你从(sx,sy)出发,走四连通的格子.你需要走一条闭合的路径,可以自交 ...

  2. Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算

    You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...

  3. Circling Round Treasures(codeforces 375c)

    题意:要求在一张网格图上走出一条闭合路径,不得将炸弹包围进去,使围出的总价值减去路径长度最大. /* 类似于poj3182的做法,只不过出现了多个点,那么就用状态压缩的方法记录一个集合即可. */ # ...

  4. Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)

    题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...

  5. 【CF375C】Circling Round Treasures

    Portal --> CF375C Solution 一个有趣的事情:题目中有很大的篇幅在介绍如何判断一个位置在不在所围的多边形中 那么..给了方法当然就是要用啊 ​ 首先是不能包含\('B'\ ...

  6. CF221C Circling Round Treasures

    题目大意 给定一个$n\times m$的网格$(n,m\leq 20)$,每个格子都是$S\space \#\space B\space x\space .$中第一个. $S$表示起点,保证有且仅有 ...

  7. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  8. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  9. Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)

    题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...

随机推荐

  1. springBoot 编写接口

    @Slf4j @RestController public class testController { @Autowired StringRedisTemplate redis; @RequestM ...

  2. ASP.NET MVC 实现 AJAX 跨域请求

    ASP.NET MVC 实现AJAX跨域请求的两种方法 和大家分享下Ajax 跨域的经验,之前也找了好多资料,但是都不行,后来看到个可行的修改了并测试下 果然OK了   希望对大家有所帮助! 通常发送 ...

  3. 误加all_load引起的程序报错

    一.为什么要加-all_load 在64位的mac系统和iOS系统下,链接器有一个bug,会导致只包含有类别的静态库无法使用-ObjC标志来加载文件.解决方法是使用-all_load或者-force_ ...

  4. 牛客网 牛客小白月赛1 J.おみやげをまらいました

    J.おみやげをまらいました   链接:https://www.nowcoder.com/acm/contest/85/J来源:牛客网     随便写写.   代码: 1 #include<ios ...

  5. CodeForces 702B Powers of Two【二分/lower_bound找多少个数/给出一个数组 求出ai + aj等于2的幂的数对个数】

    B. Powers of Two   You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i,  ...

  6. ylbtech-czgfh(规范化)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-czgfh(规范化)-数据库设计 DatabaseName:czgfh(财政规范化) Model:账户模块.系统时间设计模块.上报自评和审 ...

  7. 【GLSL教程】(六)逐顶点的光照 【转】

    引言 在OpenGL中有三种类型的光:方向光(directional).点光(point).聚光(spotlight).本教程将从方向光讲起,首先我们将使用GLSL来模仿OpenGL中的光. 我们将向 ...

  8. TestCodis的工具代码

    关于redis的操作demo代码如下: import java.util.HashMap; import java.util.Iterator; import java.util.List; impo ...

  9. 关于Web项目的pom文件处理

    pom文件的方式需要修改的是 <packaging>war</packaging> <profiles> <profile> <id>com ...

  10. tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样。

    tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样. imp sys/as TRANSPORT_TABLESPACE=Y datafiles= C:\oracle\pro ...