C. Circling Round Treasures
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题意:

  要求在一张网格图上走出一条闭合路径,不得将炸弹包围进去,使围出的总价值减去路径长度最大。

分析:

  大致同poj3182,再加上状态压缩。w[x]表示该状态下的sum{val},dp[x][y][k]表示围圈,在k状态下的最短dis。|状态:选择了1-8中哪几个

  转移的时候 xor一下就好

  ps:bomb怎么搞?直接把他的val=-oo,扔到bfs中。正确性自行脑补

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=;
const int M=<<;
const int dx[]={,,,-};
const int dy[]={,-,,};
int dp[N][N][M],w[M],val[N];char mp[N][N];
int n,m,cnt,ans,sx,sy,gx[N],gy[N],px,py,pk,nx,ny,nk;
struct node{
int x,y,k;
node(int x=,int y=,int k=):x(x),y(y),k(k){}
};
bool ok(int j){
if(nx==gx[j]&&ny<gy[j]){
if(px<nx) return ;
}
if(px==gx[j]&&py<gy[j]){
if(px>nx) return ;
}
return ;
}
void bfs(){
memset(dp,-,sizeof dp);
queue<node>q;
q.push(node(sx,sy,));
dp[sx][sy][]=;
while(!q.empty()){
node t=q.front();q.pop();
px=t.x;py=t.y;pk=t.k;
if(px==sx&&py==sy) ans=max(ans,w[pk]-dp[px][py][pk]);
for(int i=;i<;i++){
nx=px+dx[i];ny=py+dy[i];nk=pk;
if(nx<||ny<||nx>n||ny>m||(mp[nx][ny]!='.'&&mp[nx][ny]!='S')) continue;
for(int j=;j<cnt;j++)
if(ok(j)) nk^=<<j;
if(dp[nx][ny][nk]==-){
dp[nx][ny][nk]=dp[px][py][pk]+;
q.push(node(nx,ny,nk));
}
}
}
printf("%d\n",ans);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%s",mp[i]+);
for(int i=,p;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='S') sx=i,sy=j;
if(mp[i][j]>''&&mp[i][j]<''){
p=mp[i][j]-'';cnt++;
gx[p]=i;gy[p]=j;
}
}
}
for(int i=;i<cnt;i++) scanf("%d",&val[i]);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='B'){
gx[cnt]=i;gy[cnt]=j;val[cnt]=-;
cnt++;
}
}
}
w[]=;
for(int S=;S<(<<cnt);S++){
for(int i=;i<cnt;i++){
if(S&(<<i)) w[S]+=val[i];
}
}
bfs();
return ;
}

Circling Round Treasures CodeForces - 375C的更多相关文章

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

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

  2. Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算

    You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...

  3. Circling Round Treasures(codeforces 375c)

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

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

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

  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 Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  8. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  9. Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)

    题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...

随机推荐

  1. 【转】awk的使用及字符串的操作

    awk教程 转载[awk的使用及字符串的操作] sed版: sed '1~2d' file #删除奇数行 sed '1~2!d' file #删除偶数行 awk版: awk 'NR%2==0' fil ...

  2. Hdoj 2509 Be the Winner

    Diciption Let's consider m apples divided into n groups. Each group contains no more than 100 apples ...

  3. ORACLE普通表转换成分区表

    转http://mp.weixin.qq.com/s?__biz=MzAwMjkyMjEwNg==&mid=2247484761&idx=1&sn=ce080581145931 ...

  4. Move resources allocated using unmanaged interface to managed devm interface

    转载:http://blog.csdn.net/swingboard/article/details/27207497   So today let’s talk about devm functio ...

  5. 第十八章 Python批量管理主机(paramiko、fabric与pexpect)

    这个人的文章不错:http://lizhenliang.blog.51cto.com/all/7876557 转载:http://lizhenliang.blog.51cto.com/7876557/ ...

  6. 压缩软件Snappy的安装

    1.下载源码,通过编译源码安装  tar -zxvf  /home/zfll/soft/snappy-1.1.2.tar.gz cd snappy-1.1.2 ./configure make sud ...

  7. MetaQ简单实用demo

    Metaq的生产者代码 import java.io.BufferedReader; import java.io.InputStreamReader; import com.taobao.metam ...

  8. 2016.6.21 maven使用cmd新建项目时,failed to execute goal org.apache.maven.plugins:maven-archetye-plugin:2.4:generate...

    在学习maven的时候,想要新建一个maven工程,在命令行执行create或generate命令. 错误如图所示: failed to execute goal org.apache.maven.p ...

  9. C#自定义MessageBox 按钮的Text

    运行效果: 代码: using System; using System.Drawing; using System.Runtime.InteropServices; using System.Tex ...

  10. remove-duplicates-from-sorted-list I&II——去除链表中重复项

    I.Given a sorted linked list, delete all duplicates such that each element appear only once. For exa ...