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.


题目大意

  要求在网格图中,从指定点出发,走出一条回路(可以自交,有重边),不穿过任何一个物品(炸弹、障碍或者宝藏),使得围出来的图形不包含任何一个Bomb,并且最大化围住的宝藏的价值和减去走的步数。

判断一个点是否在多边形内的算法(射线法)

  1. 以这一点为端点,作出一条不穿过多边形任何一个顶点的射线
  2. 数与多边形的交点个数
  3. 如果交点个数为奇数,则这一点在多边形内,否则在多边形外

  考虑增加一维k,把从每个物品引出向上的一条射线与路径的交点数的奇偶性用二进制压位。即$f[i][j][k]$表示当前在点$(i, j)$,状态为k的最短路径长度。

  如何转移?这是个好问题。考虑穿过物品引出来的射线的几种情况:

  然后发现一个格子一个点好像不太好处理,(因为自己智商-inf,所以想不出来不拆点的做法),所以决定把一个格子拆成左右两个点,中间连一条权值为0的双向边,于是对于与路径相交就很好处理了。

  最后再枚举一下状态,统计答案就行了。

Code

 /**
* Codeforces
* Problem#375C
* Accepted
* Time: 31ms
* Memory: 3672k
*/
#include <bits/stdc++.h>
using namespace std; typedef bool boolean;
typedef pair<int, int> pii;
#define fi first
#define sc second const int N = , S = << ; typedef class Status {
public:
int x;
int y;
int mark; Status (int x = , int y = , int mark = ):x(x), y(y), mark(mark) { }
}Status; int n, m;
int co, ct = , cb = ;
pii s;
int val[];
pii pos[];
int f[N][N << ][S];
boolean vis[N][N << ][S];
boolean exist[N][N << ];
char str[N]; inline void init() {
scanf("%d%d", &n, &m);
memset(exist, true, sizeof(exist));
for (int i = ; i < n; i++) {
scanf("%s", str);
for (int j = ; j < m; j++) {
if (str[j] == '.') continue;
exist[i][j << ] = exist[i][(j << ) | ] = (str[j] == 'S');
if (str[j] == 'S')
s = pii(i, j << );
else if (str[j] == 'B')
pos[ - (++cb)] = pii(i, j);
else if (str[j] != '#')
pos[str[j] - '' - ] = pii(i, j), ct = max(ct, str[j] - '');
}
}
memcpy (pos + ct, pos + ( - cb), sizeof(pii) * cb);
for (int i = ; i < ct; i++)
scanf("%d", val + i);
co = cb + ct;
} const int mov[][] = {{, }, {, -}, {, }, {-, }}; boolean sameGrid(int x1, int y1, int x2, int y2) {
return (x1 == x2 && (y1 >> ) == (y2 >> ));
} queue<Status> que;
inline void spfa() {
m = m << ;
que.push(Status(s.fi, s.sc, ));
memset(f, 0x3f, sizeof(f));
f[s.fi][s.sc][] = ;
while (!que.empty()) {
Status e = que.front();
que.pop();
vis[e.x][e.y][e.mark] = false;
for (int d = , x, y, c; d < ; d++) {
Status eu (e.x + mov[d][], e.y + mov[d][], e.mark);
if (eu.x < || eu.x >= n || eu.y < || eu.y >= m) continue;
if (!exist[eu.x][eu.y]) continue;
c = sameGrid(e.x, e.y, eu.x, eu.y) ? () : ();
if(!c) {
x = eu.x, y = eu.y >> ;
for (int i = ; i < co; i++) {
if (pos[i].sc == y && pos[i].fi > x)
eu.mark ^= ( << i);
}
}
if (f[e.x][e.y][e.mark] + c < f[eu.x][eu.y][eu.mark]) {
f[eu.x][eu.y][eu.mark] = f[e.x][e.y][e.mark] + c;
if (!vis[eu.x][eu.y][eu.mark]) {
// cerr << eu.x << " " << eu.y << " " << eu.mark << " " << f[eu.x][eu.y][eu.mark] << endl;
vis[eu.x][eu.y][eu.mark] = true;
que.push(eu);
}
}
}
}
} inline void solve() {
int res = ;
for (int i = , cmp; i < ( << ct); i++) {
cmp = ;
for (int j = ; j < ct; j++)
if (i & ( << j))
cmp += val[j];
cmp -= f[s.fi][s.sc][i];
if (cmp > res)
res = cmp;
}
printf("%d\n", res);
} int main() {
init();
spfa();
solve();
return ;
}

Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算的更多相关文章

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

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

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

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

  3. Circling Round Treasures CodeForces - 375C

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

  4. Circling Round Treasures(codeforces 375c)

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

  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 Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力

    Magic Forest 题意:就是在1 ~ n中找三个值,满足三角形的要求,同时三个数的异或运算还要为0: , where  denotes the bitwise xor of integers  ...

  8. 【最短路】【位运算】It's not a Bug, it's a Feature!

    [Uva658] It's not a Bug, it's a Feature! 题目略 UVA658 Problem PDF上有 试题分析:     本题可以看到:有<=20个潜在的BUG,那 ...

  9. CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)

    题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...

随机推荐

  1. Cocos Creator 动作(动画)笔记

    动作cc.ActionInterval 和cc.ActionInstant; var action = cc.moveTo(2, 100, 100); // 创建一个移动动作node.runActio ...

  2. gitlab4.0_安装

    一,安装环境 OS:redhat7.4 二,安装依赖包 yum -y groupinstall 'Development Tools'  ===>待验证 yum -y install pytho ...

  3. 笔记 : CSS3实现背景渐变过渡

    使用CSS3的人都知道背景background-image是可以线性渐变(linear-gradient)和径向渐变(radial-gradient),但是想要做到过渡动画,单纯的background ...

  4. css3d旋转

    一.包裹层添加 -webkit-perspective: 800px; -moz-perspective: 800px; 使子元素获得3D效果支持   二.自持子元素需支持3D效果 -webkit-t ...

  5. 基于bootstrap的jQuery多级列表树插件

    简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...

  6. “编程利器”:VSCode

    原先一直使用sublime text3,并且认为它是很好的编程利器. 但最近写代码时,发现很多代码还是提示的不够完整.我们知道,当代码名字很长时,还没有提醒,这是非常苦恼的一件事!同时它的调试功能也不 ...

  7. Maven的作用、用途、内涵、愿景

    maven被许多人认为是一个构建工具.许多人最初是从熟悉ant而转到maven的,因此很自然地这样认为maven是一个构建工具.但是maven并不仅仅是一个构建工具,也不是ant的一个替代工具.mav ...

  8. 准备dbcp2-2.1.1和pool2-2.4.2 、commons-dbcp-1.4jar包

    下载地址:https://pan.baidu.com/s/1gtcW36Lz6Yt-j9WlTu31Pw

  9. caffe生成voc格式lmdb

    要训练ssd基本都是在liu wei框架下改,生成lmdb这一关照葫芦画瓢总遇坑,记录之: 1. labelmap_voc.prototxt要根据自己的分类修改,比如人脸检测改成这样: item { ...

  10. JetBrains WebStorm打开多个项目project的方法

    JetBrains WebStorm打开多个项目project的方法File-->Settings-->Directories点击右侧 + Add content root,选择目录后即可 ...