题目链接:传送门


思路:

仔细观察可以发现,答案最多就是2,只要把(2,1)和(1,2)堵住就可以了。

答案是0的情况就是初始状态下,(1,1)就已经不可达(n,m)了,很好判断。

所以重点就是区分答案为1和答案为2的情况。

如果答案为1的话,就说明从(1,1)到(n,m)的所有路径都经过同一个点(这样的点至少一个)。

实际上,求出(1,1)出发可达的所有点的集合S1,和所有可达(n,m)的点S2。这里用dp来跑可以O(nm)。其中坐标要压成一维的(因为n、m的范围没有给出),否则不好开内存。

再以到(1,1)的曼哈顿距离相同为条件,把两个点集划分成O(n+m)个集合。

这些集合的大小的最小值,就是答案。


代码:O(nm)

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 1000005
#define INF 0x3f3f3f3f
#define mk(x) (1<<x) // be conscious if mask x exceeds int
#define sz(x) ((int)x.size())
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mp(a,b) make_pair(a, b)
#define endl '\n'
#define lowbit(x) (x&-x) using namespace std;
typedef long long ll;
typedef double db; int n, m;
inline int getx(int t) {
return t / m;
}
inline int gety(int t) {
return t % m;
}
inline int gett(int x, int y) {
return x*m + y;
} char s[N];
bool vis11[N], visnm[N];
int main()
{
cin >> n >> m;
memset(vis11, false, sizeof vis11);
memset(visnm, false, sizeof visnm);
for (int i = ; i < n; i++)
scanf("%s", s + i*m);
vis11[] = visnm[n*m-] = true;
for (int t = ; t < n*m-; t++) {
int x = getx(t), y = gety(t);
if (s[t] == '#') continue;
if (x == ) {
int pret = gett(x, y-);
vis11[t] = vis11[pret];
}
else if (y == ) {
int pret = gett(x-, y);
vis11[t] = vis11[pret];
}
else {
int pret1 = gett(x, y-);
int pret2 = gett(x-, y);
vis11[t] = vis11[pret1] || vis11[pret2];
}
}
for (int t = n*m-; t > ; t--) {
int x = getx(t), y = gety(t);
if (s[t] == '#')
continue;
if (x == n-) {
int pret = gett(x, y+);
visnm[t] = visnm[pret];
}
else if (y == m-) {
int pret = gett(x+, y);
visnm[t] = visnm[pret];
}
else {
int pret1 = gett(x, y+);
int pret2 = gett(x+, y);
visnm[t] = visnm[pret1] || visnm[pret2];
}
}
int ans = ;
for (int d = ; d < n+m-; d++) {
int x = , y = d;
if (y >= m) {
y = m-;
x = d-y;
}
int cnt = ;
for (; x < n && y >= ; x++, y--) {
int tmpt = gett(x, y);
if (vis11[tmpt] && visnm[tmpt])
cnt++;
}
if (cnt == ) {
ans = ;
break;
}
if (cnt == )
ans = ;
}
cout << ans << endl;
return ;
}

Codeforces1214D. Treasure Island (dp + Hash)的更多相关文章

  1. 取数字(dp优化)

    取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...

  2. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  3. [Codeforces722E] Research Rover (dp+组合数学)

    [Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...

  4. 数据库里账号的密码,需要怎样安全的存放?—— 密码哈希(Password Hash)

    最早在大学的时候,只知道用 MD5 来存用户的账号的密码,但其实这非常不安全,而所用到的哈希函数,深入挖掘,也发现并不简单-- 一.普通的 Hash 函数 哈希(散列)函数是什么就不赘述了. 1.不推 ...

  5. ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

    题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...

  6. Rikka with Subset HDU - 6092 (DP+组合数)

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  7. 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)

    题面戳我 Solution 首先长度为偶数可以直接判掉 然后我们可以枚举删的位置,通过预处理的\(hash\),判断剩余部分是否划分成两个一样的 判重要注意,我们把字符串分为三个部分\(L_l+1+L ...

  8. 合法括号序列(dp+组合数学)

    键盘上有左括号(,右括号),和退格键-,共三个键. 牛牛希望按键n次,使得输入的字符串恰好一个合法的括号序列. 每按一次左括号(,字符串末尾追加一个左括号( 每按一次右括号),字符串末尾追加一个右括号 ...

  9. LightOJ - 1246 Colorful Board(DP+组合数)

    http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间 ...

随机推荐

  1. [LeetCode]-009-Palindrome_Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  2. modern php笔记---2.1、特性(命名空间、特性、性状)

    modern php笔记---2.1.特性(命名空间.特性.性状) 一.总结 一句话总结: legend2是真的非常好用,资质起码提升5倍,也就是学习效率提升了起码5倍 1.命名空间实质? 从技术层面 ...

  3. JNI写本地日志文件

    调试JNI库 我喜欢反编译APK 然后替换.so文件 然后再编译成APK 其中写日志的话 用fopen("/sdcard/lei.txt","wb+")

  4. kafka window安装与配置

    一.安装jdk1.8(此处省略) 二.安装zookeeper1. 下载安装包:http://zookeeper.apache.org/releases.html,解压到指定目录(如下) 2. 在系统变 ...

  5. 提示标签title折行

    使用代码换行共两种代码,均可实现html标签内title内容显示时换行.换行代码符分别为:“ ”和“ ”以上符合数字输入均必须英文半角模式输入.使用时候,在需要换行地方任选一种(组)换行符号代码即可.

  6. ES6对象的拓展

    属性的简洁表示法 ES6 允许直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = {foo}; //允许直接写入变量和函数作为 ...

  7. Mysql基本管理知识

    数据库的启动 [root@node80 ~]# /etc/init.d/mysqld start #mysqld是从安装包拷贝的mysql.server Starting MySQL. SUCCESS ...

  8. django项目部署过程

    django项目部署过程 1.上传代码 用git或者其他工具,如scp 代码上传后保证每个应用下的migrations文件夹里只有一个__init__.py文件,自己的迁移文件不要上传上来,具体的gi ...

  9. 2.转发。基于itchat的微信消息同步机器人

    原文:https://www.jianshu.com/p/7aeadca0c9bd# 看到了该网址有基于itchat的微信消息同步机器人,转过来继续研究.以下是转过来的: 最近 全栈数据工程师养成攻略 ...

  10. 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...