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. cocos2d-x JS 纯代码实现人物头像裁剪

    有时候为了方便会直接用颜色层和过渡层来显示一些信息,但层只有方角没有圆角不太美观,于是我用剪切节点实现了一个圆角层.方便以后使用.   当然,如果使用Cosos Studio 操作会更好一些,省去了坐 ...

  2. 用int还是用Integer?

    昨天例行code review时大家有讨论到int和Integer的比较和使用. 这里做个整理,发表一下个人的看法.   [int和Integer的区别] int是java提供的8种原始类型之一,ja ...

  3. spring对JDBC的整合支持

    参考网址:https://blog.csdn.net/u013821825/article/details/51606171 springMVC,目前用到的jar包 spring IOC 5个包  + ...

  4. windows 服务器硬盘的分区

    进入Server 2012的操作系统,打开CMD框,输入:diskmgmt.msc,回车. 操作完第一步后会弹出“磁盘管理”的框.鼠标右键点击红框所在位置,选中“压缩卷”. 在“输入压缩空间量(MB) ...

  5. C# 基于DocumentFormat.OpenXml的数据导出到Excel

    using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.S ...

  6. hadoop2.4.1 伪分布

           最终的 /etc/profile :#在文件最后添加       # /etc/profile # System wide environment and startup program ...

  7. c#如何判断字符串是否含中文

    如代码: static bool ContainChinese(string input) { string pattern = "[\u4e00-\u9fbb]"; return ...

  8. PKCS#1

    ASN.1 syntax,octet string是一个8 bytes sequence string. RSA中涉及到的Data conversion: 1)I2OSP,Integer to Oct ...

  9. 43. Multiply Strings (大数乘法)

    DescriptionHintsSubmissionsDiscussSolution   Pick One Given two non-negative integers num1 and num2  ...

  10. hdu5441 并查集+克鲁斯卡尔算法

    这题计算 一张图上 能走的 点对有多少个  对于每个限制边权 , 对每条边排序,对每个查询排序 然后边做克鲁斯卡尔算法 的时候变计算就好了 #include <iostream> #inc ...