Circling Round Treasures CodeForces - 375C
1 second
256 megabytes
standard input
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:
- 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.
- You need to find out if the point p of the table that is not crossed by the polyline lies inside the polyline.
- Let's draw a ray that starts from point p and does not intersect other points of the table (such ray must exist).
- 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.
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 i, vi ( - 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".
Print a single integer — the maximum possible profit you can get.
4 4
....
.S1.
....
....
10
2
7 7
.......
.1###2.
.#...#.
.#.B.#.
.3...4.
..##...
......S
100
100
100
100
364
7 8
........
........
....1B..
.S......
....2...
3.......
........
100
-100
100
0
1 1
S
0
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的更多相关文章
- CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]
C - Circling Round Treasures 题意: 在一个$n*m$的地图上,有一些障碍,还有a个宝箱和b个炸弹.你从(sx,sy)出发,走四连通的格子.你需要走一条闭合的路径,可以自交 ...
- 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 ...
- Circling Round Treasures(codeforces 375c)
题意:要求在一张网格图上走出一条闭合路径,不得将炸弹包围进去,使围出的总价值减去路径长度最大. /* 类似于poj3182的做法,只不过出现了多个点,那么就用状态压缩的方法记录一个集合即可. */ # ...
- Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)
题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...
- 【CF375C】Circling Round Treasures
Portal --> CF375C Solution 一个有趣的事情:题目中有很大的篇幅在介绍如何判断一个位置在不在所围的多边形中 那么..给了方法当然就是要用啊 首先是不能包含\('B'\ ...
- CF221C Circling Round Treasures
题目大意 给定一个$n\times m$的网格$(n,m\leq 20)$,每个格子都是$S\space \#\space B\space x\space .$中第一个. $S$表示起点,保证有且仅有 ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...
随机推荐
- hdu 5020(斜率的表示+STL)
Revenge of Collinearity Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- Cryptography I 学习笔记 --- 总结
在b站上大概的看完了Dan Boneh的密码学,对现代密码学总算有了一个粗浅的认识. 总算能在纸上手写RSA公式并且证明之了,蛤蛤. 总体的感触就是,现代密码学是一个非常博大精深的体系,我等程序员最重 ...
- svn安装配置使用小总结
1svn:版本控制系统服务端与客户端协作服务端:subversion客户端:eclipse_svn_site-1.10.5.zip插件1安装问题: 1subversion版本过高 会出现版 ...
- 洛谷——1968 美元汇率(DP)
题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 ...
- 多线程一共就俩问题:1.线程安全(访问共享数据) 2.线程通信(wait(),notify())
多线程一共就俩问题:1.线程安全(访问共享数据) 2.线程通信(wait(),notify()) 1.线程安全,无非就是加锁,访问共享资源时,synchronized 2.线程通信,就是控制各个线程之 ...
- html使用示例
select标签 <select name="Area" id="Area" class="sel"> <option v ...
- HDU 1041 Computer Transformation 数学DP题解
本题假设编程是使用DP思想直接打表就能够了. 假设是找规律就须要数学思维了. 规律就是看这些连续的0是从哪里来的. 我找到的规律是:1经过两次裂变之后就会产生一个00: 00经过两次裂变之后也会产生新 ...
- 访问C指针的指针
#include <stdio.h> #include <stdlib.h> int main(int argc,char **argv){ void* vp; void** ...
- 【前端阅读】——《活用PHP、MySQL建构Web世界》摘记之高级应用
一.高级应用 1.计数器 计数器的原理很简单,只有两步: 第一步就是读写一个数字,第二步就是显示出来.一般CGI'大多直接写到文件系统,当然也可以利用MySQL来存储这个数字,完成第一步的操作. 第二 ...
- VC++动态链接库(DLL)编程深入浅出(二)
好,让我们正式进入动态链接库的世界,先来看看最一般的DLL,即非MFC DLL 上节给大家介绍了静态链接库与库的调试与查看,本节主要介绍非MFC DLL. 4.非MFC DLL 4.1一个简单的DLL ...