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. 关于Stuck Archiver的疑问

    客户使用crsctl stat res -t命令去查看RAC集群状态时,发现异常,知晓Stuck Archiver代表归档满,问我们为什么RAC是同一个库,只有实例1显示Stuck Archiver, ...

  2. mx:Panel (面板容器) mx:Button (按钮) 默认大小

    1.默认组件大小 <mx:Panel title="默认的面板容器大小和按钮控件大小"> <!-- 使用控件大小默认值 --> <mx:Button ...

  3. git 语法

    $ git init  // 初始化一个Git仓库 会生成一个.git目录 $ git status   // 查看仓库的状态 $ git add .   // 将所有修改添加到暂存区 $git ad ...

  4. Browsersync结合gulp和nodemon实现express全栈自动刷新

    Browsersync能让浏览器实时.快速响应你的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试. ...

  5. LeetCode67.二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: ...

  6. GeoJSON 和 TopoJSON

    GeoJSON 和 TopoJSON 是符合 JSON 语法规则的两种数据格式,用于表示地理信息. 1. GeoJSON GeoJSON 是用于描述地理空间信息的数据格式.GeoJSON 不是一种新的 ...

  7. turtle库基础练习

    1.画一组同切圆 import turtle turtle.circle(10) turtle.circle(20) turtle.circle(30) turtle.circle(40) turtl ...

  8. Lua语言总结

    [1]要退出交互模式和解释器,只需输入“os.exit()” [2]在交互模式执行程序块可以使用函数dofile,这个函数就可以立即执行一个文件.应用示例:dofile("f:/myLua/ ...

  9. python pandas简单使用处理csv文件

    这里jira.csv是个大文件 1) >>> import pandas >>> jir=pandas.read_csv(r'C:\Temp\jira.csv') ...

  10. maven的profile详解

    详细内容请见:https://www.cnblogs.com/wxgblogs/p/6696229.html Profile能让你为一个特殊的环境自定义一个特殊的构建:profile使得不同环境间构建 ...