Treasure Island DFS +存图
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n×mn×m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 11 to nn from top to bottom and columns with consecutive integers from 11 to mm from left to right. Denote the cell in rr-th row and cc-th column as (r,c)(r,c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n,m)(n,m).
Vasya got off the ship in cell (1,1)(1,1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x,y)(x,y) he can move only to cells (x+1,y)(x+1,y) and (x,y+1)(x,y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1,1)(1,1) where Vasya got off his ship and (n,m)(n,m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
First line of input contains two positive integers nn, mm (3≤n⋅m≤10000003≤n⋅m≤1000000), sizes of the island.
Following nn lines contains strings sisi of length mm describing the island, jj-th character of string sisi equals "#" if cell (i,j)(i,j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1,1)(1,1), i.e. the first cell of the first row, and he wants to reach cell (n,m)(n,m), i.e. the last cell of the last row.
It's guaranteed, that cells (1,1)(1,1) and (n,m)(n,m) are empty.
Print the only integer kk, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
2 2
..
..
2
4 4
....
#.#.
....
.#..
1
3 4
....
.##.
....
2
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1,1)(1,1) to (n,m)(n,m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1,1)(1,1) to (n,m)(n,m) impossible.
题目大意: 起点是(1,1),,,终点(n,m),只能向下走或者向右走 ,#不能走,女巫可以将一个点变成#。问最少需要将几个点变成#才能使其不能到达终点(n,m)。
题解:先跑一边dfs,如果不可以到达的话,输出0,即不能到达,如果可以到达,把路径标记一下。然后再跑一遍dfs,如果可以不到达的话,输出1,输出2;
存图: 题目中给的数据范围是n*m<1e6 二维数组肯定不行。由于n*m所以我们开一个1e6的一维数组,然后没m个存一次,一共存m次
dfs版AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=1E6+;
int d[][]={,,,};
int n,m;
char mp[N];
bool mark[N];
bool flag= false ;
void dfs(int id){
if(flag) return ;
if(id== n*m-){
flag =true;
return ;
}
else {
int x=id%m;//列
int y=id/m;//行
for(int i=;i<;i++){
int dx=y+d[i][];
int dy=x+d[i][];
if(dx>=n||dy>=m||mark[dx*m+dy]) continue ;
mark[dx*m+dy]=;
dfs(dx*m+dy);
if(flag) return ;
}
}
return ;
} int main(){
cin>>n>>m;
for(int i=;i<n;i++){
scanf("%s",mp+i*m);
for(int j=i*m;j<(i+)*m;j++){
if(mp[j]=='#') mark[j]=;
}
}
int i;
for(i=;i<=;i++){
mark[]=;
mark[n*m-] =;
flag= false ;
dfs();
if(!flag){
cout<<i<<endl;
break;
}
}
if(flag) puts("");
return ;
}
Treasure Island DFS +存图的更多相关文章
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- D. Treasure Island
D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- How far away(DFS+vector存图)
There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...
- B - Cow Marathon DFS+vector存图
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...
- Pants On Fire(链式前向星存图、dfs)
Pants On Fire 传送门:链接 来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)
Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...
随机推荐
- TensorFlow系列专题(九):常用RNN网络结构及依赖优化问题
欢迎大家关注我们的网站和系列教程:http://panchuang.net/ ,学习更多的机器学习.深度学习的知识! 目录: 常用的循环神经网络结构 多层循环神经网络 双向循环神经网络 递归神经网络 ...
- 编译原理:LL(1)文法的判断,递归下降分析程序
1. 文法 G(S): (1)S -> AB (2)A ->Da|ε (3)B -> cC (4)C -> aADC |ε (5)D -> b|ε 验证文法 G(S)是不 ...
- Python常用模块之configparser
ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置内容 ...
- JAVA实现图片验证
一.什么是图片验证码? 可以参考下面这张图: 我们在一些网站登陆的时候,经常需要填写以上图片的信息. 这种图片验证方式是我们最常见的形式,它可以有效的防范恶意攻击者采用恶意工具,来进行窃取用户的密码 ...
- 在ES5实现ES6中的Object.is方法
ES6中对象的扩展里面添加了一个Object.is方法,用于比较两个值是否严格相等.内部计算与 === 行为基本一致.那么我们怎么在不支持这个方法的ES5中实现呢? 首先我们需要搞清楚两点,1:Obj ...
- python:列表切片知识的总结
列表的切片操作时对其中的单个或者多个索引对应元素的操作,具有如下特点: ①.切片区间是左闭右开区间 ②.切片的下标可以表示负数,-1表示倒数第一个数,-2表示倒数第二个数 ③.默认步长是1,可增加第三 ...
- flex布局取消子元素(img、div等)缩放:
取消子元素(img.div等)缩放: 父元素: display: flex ; 子元素: flex-shrink: 0;
- SQL Server 存储过程分页。
create proc proc_Product@page int, -- 页数@row int -- 一页有几行Asdeclare @newpage int set @newpage = (@ ...
- 如何连接到Oracle数据库?
如何连接到Oracle数据库? 使用SQL * Plus连接Oracle数据库服务器 SQL * Plus是交互式查询工具,我们在安装Oracle数据库服务器或客户端时会自动安装.SQL * Pl ...
- 关于TD信息树自己的体验和建议
自己选择的是17级学长13组的TD消息树,通过对这个软件的使用,感觉整体上还是很好的,他的主要功能就相当于把微信的朋友圈还有qq的好友动态等功能集合到了一起.这个软件整体就相当于一个空间,可以加好友互 ...