D. Treasure Island
dfs大法好==
写半天bfs疯狂MLE
dfs标记掉路上的一些点
然后再跑一遍dfs
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%I64d",&x);
#define read(A) for(int i=0;i<n;i++)scanf("%I64d",&A[i]);
#define endl '\n'
#define fi first
#define se second
#define si signed
#define P pair<si,si>
#define pb push_back
#define maxn 1000000+100
bool A[maxn];
int n,m;
queue<P>q;
P fa[maxn];
bool vis[maxn];
int cnt=;
bool bfs()
{
while(!q.empty())q.pop();
q.push(P(,));
while(!q.empty())
{
P a=q.front();
q.pop();
si x=a.fi;
si y=a.se;
//B[x*m+y]++;
if(x==n-&&y==m-)
{
// cnt++;
//f=0;
return false;
}
if(x+<n)
{
if(A[(x+)*m+y]==)
{
fa[(x+)*m+y]=P(x,y);
q.push(P(x+,y));
}
}
if(y+<m)
{
if(A[x*m+y+]==)
{
fa[x*m+y+]=P(x,y);
q.push(P(x,y+)); }
}
} return true;
//else return false;
}
bool dfs(int x,int y)
{
vis[x*m+y]=;
int a=x+;
if(x==n-&&y==m-)return true;
if(a<n&&!vis[a*m+y]&&A[a*m+y]==){
if(dfs(a,y))return true;
}
int b=y+;
if(b<m&&!vis[x*m+b]&&A[x*m+b]==){
if(dfs(x,b))return true;
}
return false; }
signed main()
{
sc(n);
sc(m);
int t=;
for(int i=; i<n; i++)
{
getchar();
for(int j=; j<m; j++)
{
A[i*m+j]=(getchar()=='#'?:);
t+=A[i*m+j];
}
}
if(t==&&(n==||m==)){
cout<<<<'\n';
return ;
}
if(t==)
{
cout<<<<'\n';
return ;
}
if(n==||m==){
cout<<<<'\n';
return ;
}
int ans=;
if(!dfs(,)){
cout<<<<'\n';
return ;
}
vis[]=vis[(n-)*m+m-]=;
if(!dfs(,)){
cout<<<<'\n';
return ;
}
cout<<<<'\n';
/* if(bfs())
{
cout<<0<<'\n';
return 0;
}
//cout<<B[1*m+2]<<endl;
P x=fa[(n-1)*m+m-1];
si c,d;
while(true){
c=x.fi,d=x.se;
if(c==0&&d==0)break;
x=fa[c*m+d];
A[c*m+d]=1;
}
if(bfs()){
cout<<1<<'\n';
return 0;
}
cout<<ans<<'\n';
*/
}
D. Treasure Island的更多相关文章
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- Treasure Island DFS +存图
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...
- 【Gym 100971A】Treasure Island
题意 题目链接给你一个地图,'#'代表水,'.'代表陆地,'?'代表擦去的地图,可能是'#'也可能是'.'.地图中本该只有一块相连的陆地,若只有一种方案则输出确定的地图.若有多种方案,则输出‘Ambi ...
- Codeforces 106D Treasure Island 预处理前缀+暴力(水
主题链接:点击打开链接 意甲冠军: 特定n*m矩阵 # 是墙 . 和字母是平地 最多有26个字母(不反复出现) 以下k个指令. 每一个指令代表移动的方向和步数. 若以某个字母为起点,依次运行全部的指令 ...
- CodeForces - 1214D D. Treasure Island
题目链接:https://vjudge.net/problem/2728294/origin 思路:可以抽象成管道,先试试能不能找到一个通道能通到终点, 如果可以则封锁这个通道,一个石头即可, 再试试 ...
- Treasure Island(两遍dfs)-- Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)
题意:https://codeforc.es/contest/1214/problem/D 给你一个n*m的图,每次可以往右或者往下走,问你使(1,1)不能到(n,m)最少要放多少 ‘ # ’ . 思 ...
- Codeforces1214D. Treasure Island (dp + Hash)
题目链接:传送门 思路: 仔细观察可以发现,答案最多就是2,只要把(2,1)和(1,2)堵住就可以了. 答案是0的情况就是初始状态下,(1,1)就已经不可达(n,m)了,很好判断. 所以重点就是区分答 ...
- libconfig第二篇----两个小例子
本文只看粗体即可,太多catch语句.两个例子均来自libconfig包的example文件夹下面,. 例子一: #include <iostream> #include <ioma ...
随机推荐
- SSCursor 处理大量数据
使用游标的好处是不会将查询结果全部都放入内存中,避免了占用大量的内存,会从存储块中读取记录,并且一条一条的返回来 class DbConnection(object): def __init__(se ...
- idea运行时 Process finished with exit code -1073741819 (0xC0000005)
问题描述: idea中启动项目报 Process finished with exit code -1073741819 (0xC0000005) ,如图所示: 问题解决: ...
- P1398 [NOI2013]书法家
传送门 就是个普及组 $dp$ 合集,把 $NOI$ 从左到右拆成 $9$ 个部分,每个部分都可以分别 $dp$ 除了 $N$ 的中间部分比较恶心以外其他都还好,自己推一下然后就知道转移,就 $N$ ...
- maven配置生成可执行的jar:maven-shade-plugin
默认打包生成的jar是不能直接运行的,因为带有main方法的信息不会添加到mainifest中,需要借助maven-shade-plugin <project> ... <build ...
- 解决Asp.net Core中chtml文档中文乱码的问题
原因 由于Visual Studio 2017在保存chtml时,文本格式非utf-8,所以导致中文会出现乱码情况. 解决办法 在工具->扩展与更新中添加插件"ForceUTF8 (w ...
- 一个常用的通过curl发送HTTP请求的函数
function: function curl_get($url, $params) { return curl_http($url, $params, 'GET'); } function curl ...
- php邮件防注入以及实现经典代码
<?php function spamcheck($field) { // filter_var() 过滤 e-mail // 使用 FILTER_SANITIZE_EMAIL ...
- Java RMI 最简单实例
IHello.java import java.rmi.Remote; import java.rmi.RemoteException; public interface IHello extends ...
- docker概述和安装
一:概述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用 ...
- Web Api 接口测试工具:WebApiTestClient
前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员也不懂C#语法,API里面的接口也不能直接给他们看,没办法,只有整个详细一点的文档呗.由于接口个数有点多,每 ...