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. color xml arm相关

    #-------------------------------------------------------------------------- # C O L O R S #--------- ...

  2. 从零开始一起学习SLAM | 三维空间刚体的旋转

    刚体,顾名思义,是指本身不会在运动过程中产生形变的物体,如相机的运动就是刚体运动,运动过程中同一个向量的长度和夹角都不会发生变化.刚体变换也称为欧式变换. 视觉SLAM中使用的相机就是典型的刚体,相机 ...

  3. 闪存卡被创建pv报错

    背景:某机器有2块闪存卡,利用LVM,将其挂载到一个目录供测试使用: 之前厂商已经安装了闪存卡对应的驱动,fdisk可以看到闪存卡信息,但是在pvcreate创建时,遭遇如下错误: # pvcreat ...

  4. jquery评分效果Rating精华版

    参考:https://blog.csdn.net/bluceyoung/article/details/8573629

  5. Javascript-do_while....

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. laravel使用过程总结

    docker-compose exec -T workspace php artisan route:list  //查看路由 laravel数据存入session,会出现Session store ...

  7. docker中crontab无法执行

    1.下载的镜像是ubuntu最简版,默认没有安装crontab 2.业务需求需要crontab 最早解决方案 1.在宿主机里面 1 3  * * * root  cd /data/wwwroot/xx ...

  8. 多线程实现Thread.Start()与ThreadPool.QueueUserWorkItem两种方式对比

    Thread.Start(),ThreadPool.QueueUserWorkItem都是在实现多线程并行编程时常用的方法.两种方式有何异同点,而又该如何取舍? 写一个Demo,分别用两种方式实现.观 ...

  9. Robot - 1. robot framework环境搭建

    Fom:https://www.cnblogs.com/puresoul/p/3854963.html 一. robot framework环境搭建: 官网:http://robotframework ...

  10. GZIPOutputStream GZIPInputStream

    GZIP is appropriate for single data stream. Example: Compress one file public class Demo8 {  public ...