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. python QQTableView中嵌入复选框CheckBox四种方法

    搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四 ...

  2. ES6之字符串扩展

    ES6字符串新增的常用方法: 1. includes(): 字符串中是否包含某个字符或字符串, 包含的两个字符必须是相连的 let str = 'hello, world' str.includes( ...

  3. javascript获取style兼容性问题

    获取css 样式的方法有三种 : style, currentStyle , getComputedStyle style (无兼容性问题) 获取语法: ele.style.attr : 设置语法:e ...

  4. 准备MyBatis

    MyBatis下载:https://github.com/mybatis/mybatis-3/releases MyBatis文件目录: 中文参考文档:http://www.mybatis.org/m ...

  5. 【转】robot framework + python实现http接口自动化测试框架

    前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测试框架,以至于后期rd每修改一个bug,经常导致之前没有问题的case又产生了 ...

  6. 【2017-03-02】C#集合,结构体,枚举

    集合 集合与数组的区别 数组:同一类型,固定长度 集合:不同类型,不固定长度 使用集合前需要:     引用命名空间:using System.Collections; 1.普通集合 定义: Arra ...

  7. OpenCV LK光流法测试

    OpenCV版本: 3.2.0 例程文件目录/samples/cpp/lkdemo.cpp 原始程序是采集相机数据,台式机没有摄像头,用Euroc测试集,偷ORB_SLAM2 /Examples/Mo ...

  8. tcp/ip 3次握手和4次挥手

    tcp/ip 3次握手和4次挥手

  9. django之路由分析

    URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. URLconf配置 基本格式: from django.conf.urls i ...

  10. selenium-grid 分布式 实现同一脚本在不同pc上运行

    1.使用版本:selenium 3.11.0chrome 65phantomjs 2.1.1selenium-server selenium-server-standalone-2.46.0.jar ...