UVALive - 5857

思路:

状压dp,用map写

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";const int N = 20;
char s[N][N];
pii a[N];
int n, m, cnt, num[N];
LL p[N];
map<LL, int> dp[2];
int main() {p[0] = 1;for (int i = 1; i < N; ++i) p[i] = p[i-1]*10;while(~scanf("%d %d", &n, &m)) {if(!n && !m) break;for (int i = 0; i < n; ++i) scanf("%s", s[i]);cnt = 0;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if(isdigit(s[i][j])) {a[cnt].fi = i;a[cnt].se = j;num[cnt] = s[i][j]-'0';++cnt;}}}dp[0].clear(); dp[1].clear();int now = 0;dp[now][0] = 0;int k;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {now ^= 1;dp[now].clear();for (auto t : dp[now^1]) {LL x = t.fi;for (k = 0; k < cnt; ++k) {if((x/p[k])%10 != num[k] && (i*m+j)-(a[k].fi*m+a[k].se) >= m+2) break;}if(k < cnt) continue;if(dp[now].find(x) == dp[now].end()) dp[now][x] = t.se;else dp[now][x] = min(dp[now][x], t.se);if(s[i][j] == '.') continue;for (k = 0; k < cnt; ++k) {if(i-1 <= a[k].fi && a[k].fi <= i+1 && j-1 <= a[k].se && a[k].se <= j+1) {if((x/p[k])%10 == num[k]) break;x += p[k];}}if(k < cnt) continue;if(dp[now].find(x) == dp[now].end()) dp[now][x] = t.se + 1;else dp[now][x] = min(dp[now][x], t.se + 1);}}}LL x = 0;for (int i = 0; i < cnt; ++i) x += num[i]*p[i];printf("%d\n", dp[now][x]);}return 0;
}

转载于:https://www.cnblogs.com/widsom/p/10637220.html

UVALive - 5857 Captain Q's Treasure相关推荐

  1. Codeforces Round #660 (Div. 2)

    A - Captain Flint and Crew Recruitment 刚开始还想筛法求质数,最后发现是个脑筋急转弯 #define IO ios::sync_with_stdio(false) ...

  2. The UVALIVE 7716 二维区间第k小

    The UVALIVE 7716 二维区间第k小 /** 题意:给一个n * n的矩阵,有q个查询每次查询r,c,s,k表示已(r,c)为右上角 大小为s的正方形中 第k小的元素n <= 250 ...

  3. 逆序数 UVALive 6508 Permutation Graphs

    题目传送门 1 /* 2 题意:给了两行的数字,相同的数字连线,问中间交点的个数 3 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 4 用归并排序或线段树求 ...

  4. Infinite Fraction Path UVALive - 8207

    Infinite Fraction Path UVALive - 8207 题意: 给你n个数,每个数在0到9之间,每个数的下标一次是0~n-1,然后他所能走到的数为(i^2+1)%n,i为他本身的下 ...

  5. 4152. [AMPPZ2014]The Captain(稠密图最短路)

    4152. [AMPPZ2014]The Captain 显然稠密图的边数时n2n^2n2量级,我们不可能把所有边建立出来,这时候通常寻求一些性质详细见[论题选编]稠密图最短路 针对本题我们可以先这样 ...

  6. UVALive 8518 - Sum of xor sum

    UVALive 8518 - Sum of xor sum 做法:线段树维护:答案,边界在左端点的区间异或为1的个数,边界在右端点异或为1的个数,1的个数,区间长度,这样已经自洽了.(每次讲线段树,都 ...

  7. 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】

    4152: [AMPPZ2014]The Captain Time Limit: 20 Sec  Memory Limit: 256 MB Submit: 2107  Solved: 820 [Sub ...

  8. Networking UVALive - 2515 (最小生成树,适合kruskal)

    Networking UVALive - 2515 kruskal模板题了,不解释 #include<algorithm> #include<iostream> #includ ...

  9. UVALive 4126 Password Suspects(AC自动机+dp)

    题意就是长度为n的串,满足下列m个串是它的子串,问这个串有多少种,如果少于42种则字典序输出. dp[i][j][k]为,填充了i个字符,在ac自动机的j节点上,字串集合为k的方案数,填充i个字符可以 ...

  10. Paint the Roads UVALive - 2197

    Paint the Roads UVALive - 2197 网络流·费用流 题目大意: 有n个点,m条边,你的任务是选择其中的一些边,使得每条被选择的边组成一些没有公共边的回路,且每个城市恰好在其中 ...

最新文章

  1. FT(Fourier Transform)在滤波上的应用
  2. web前端java script学习2017.7.11
  3. 网络编程 socket模块 tcp协议 udp协议
  4. 百度昆仑AI芯片已完成研发:明年初量产 由三星代工
  5. Ionic 框架宣布 2019 年将正式支持 Vue 和 React
  6. zabbix-设置邮箱预警
  7. SAAS多租户实现方案_springboot 实现多租户_基于共享数据库_共享schema_共享数据表_基于baomidou_mybatis_plus---springcloud工作笔记158
  8. jsp,servlet中文乱码问题
  9. linux命令(43):awk的使用技巧
  10. 设置UITextField键盘上return key不可点击
  11. 实现简单的emoji发送功能
  12. ultraiso软碟通dmg转iso图文教程
  13. 分享电脑中截图的五种方法(包括截长图)
  14. linux双系统优先开启_更改双系统启动菜单(启动顺序)的2种方法
  15. Java的ASCII编码表
  16. 网站域名被墙(被封锁、被屏蔽、被和谐)后最好的解决方法
  17. Rust本地化实现 —— fluent
  18. 我内心深处最温柔的痛楚,你是否还能感知?
  19. 深度学习—利用TensorFlow2实现狗狗品种品种(mobilenet实现)
  20. java短学期教师总结_短学期心得体会(共10篇).docx

热门文章

  1. 腾讯云最新10元/月有效期到2018年8月20日
  2. Can‘t commit changes due to unresolved conflicts
  3. [转载][不转不行了^皿^]金庸小说中的十大“公司”
  4. 【图像处理技术】 | 黑科技解读 之 PS检测、弯曲拉平、切边增强、摩尔纹
  5. 一张图解释清楚大数据技术架构,堪称阿里的核心机密
  6. 记一个用PyPDF2裁剪pdf文件并用latex插入后出现大量空白的问题
  7. unit系统与linux系统区别,python+unittet在linux与windows使用的区别
  8. SDRAM内存条时序特点
  9. 计算机无法登陆提示rpc服务器不可用,电脑rpc服务器不可用,教你电脑rpc服务器不可用怎么解决...
  10. matlab为数据加表头,matlab xlswrite 表头