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. AC日记——食物链 codevs 1047

    1074 食物链 2001年NOI全国竞赛  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 动物王国中有 ...

  2. java获取整数的各位数值

    第一种是取模运算 int qian =input/1000; //千位除以1000 int bai = input/100%10;//百位除以100%10 int shi = input%100/10 ...

  3. Algorithm | Vector

    因为平常用的话只是vector的一些非常简单的功能,基本上把它当数组来用,现在也只是把这一部分写了一些. template<class T> class XVector { public: ...

  4. noip2017集训测试赛(三)Problem C: MST

    题面 Description 给定一个n个点m条边的连通图,保证没有自环和重边.对于每条边求出,在其他边权值不变的情况下,它能取的最大权值,使得这条边在连通图的所有最小生成树上.假如最大权值为无限大, ...

  5. 2016.7.12 eclipse和IDEA中mybatis generator插件的安装与使用

    Eclipse中的安装 http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html 1.下载插件 2.将插件generator的fea ...

  6. es6 - 模板

    'use strict'; // es5 let name = 'mrs'; let qb = 20; function logs() { return 'goods!'; } let html = ...

  7. JAVA学习第十四课(接口:implements及其基本应用)

    接口: 我们知道抽象类中能够定义抽象方法,也能够定义非抽象方法.当一个抽象类中的方法都是抽象方法的时候,我们就能够定义还有一种表现方式:接口(interface),所以接口是一种特殊的抽象类 接口的出 ...

  8. mysql:4种时间类型

    insert 12 ================= 养成良好的习惯,除了整形和浮点型不加'',其余都加,包括日期时间型

  9. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...

  10. (八)jQuery中的事件

    1.加载DOM在常规的JavaScript中,使用window.onload方法:而在jQuery中,使用$(document).ready(function(){ })方法.window.onloa ...