[Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs)
题面
给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过。每步可以往下或往右走。问至少把多少个'.'变成'#',才能让从(1,1)出发不能到达(n,m)
\(n \times m \leq 10^6\)
分析
第一眼还以为是最小割,一看数据范围感觉会TLE。实际上搜索就可以了。
首先发现答案肯定是0,1,2中的一个(输出rand()%3),因为最多用2个'#',把(1,1)右侧和下方堵住就可以了。
然后注意到若存在一个点满足所有(1,1)到(n,m)的路径都经过那个点,那答案就为1.这实际上是一个有向图求割点的问题,类似HDU3313.
我们只需要dfs一遍,找出任意一条(1,1)到(n,m)的路径,然后把路径上的点标记为1。注意若(1,1)不能到(n,m),直接输出0.第二次dfs保证不经过路径上的点。如果第二次的时候(1,1)不能到(n,m),那就说明存在割点,答案为1。否则答案为2.
注意到数据范围\(n \times m \leq 10^6\),没有给出n,m的范围,所以要用vector存储字符矩阵。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 1000000
using namespace std;
int n,m;
char in[maxn+5];
vector<char>s[maxn+5];
struct node{
int x;
int y;
node(){
}
node(int _x,int _y){
x=_x;
y=_y;
}
};
const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
vector<bool>vis[maxn+5];
vector<bool>mark[maxn+5];
bool dfs(int x,int y){
if(x>n||y>m) return 0;
if(x==n&&y==m) return 1;
if(s[x][y]=='#'||vis[x][y]||mark[x][y]) return 0;
vis[x][y]=1;
bool flag=dfs(x+1,y);
if(!flag) flag=dfs(x,y+1);
//优先往(x+1,y)走,如果走不通再往(x,y+1)走,这样就可以避免被堵住的情况,详见评论区
if(flag) mark[x][y]=1;
return flag;
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",in+1);
s[i].resize(m+1);
for(int j=1;j<=m;j++) s[i][j]=in[j];
vis[i].resize(m+1);
mark[i].resize(m+1);
}
bool flag=dfs(1,1);
if(!flag) printf("0\n");
else{
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++) vis[i][j]=0;
}
mark[1][1]=mark[n][m]=0;
flag=dfs(1,1);
if(!flag) printf("1\n");
else printf("2\n");
}
}
[Codeforces 1214D]Treasure Island(dfs)的更多相关文章
- Treasure Island DFS +存图
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...
- Codeforces 106D Treasure Island 预处理前缀+暴力(水
主题链接:点击打开链接 意甲冠军: 特定n*m矩阵 # 是墙 . 和字母是平地 最多有26个字母(不反复出现) 以下k个指令. 每一个指令代表移动的方向和步数. 若以某个字母为起点,依次运行全部的指令 ...
- D. Treasure Island
D. Treasure Island dfs大法好== 写半天bfs疯狂MLE dfs标记掉路上的一些点 然后再跑一遍dfs #include<bits/stdc++.h> using n ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- 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)最少要放多少 ‘ # ’ . 思 ...
- Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)
Pursuit For Artifacts CodeForces - 652E Johnny is playing a well-known computer game. The game are i ...
随机推荐
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- 【java&c++】父子类中同名函数的覆盖问题
java和c++两门语言对于父子类中同名函数具有不同的处理方式. 先上两段代码: C++: class Basic { public: void test(string i){ cout <&l ...
- Let Us Adore 让我们来敬拜祂 中文歌词
Verse 1 诸天宣告 神的荣耀 万国万民 都将赞美 宣扬祂奇妙 The heavens declare The glory of God And all of the world Will j ...
- net core 返回值
//指定类下的子类 Type helloType = typeof(ActionResult); List<Type> types = new List<Type>(); fo ...
- python 输出一个随机数
题目:输出一个随机数. 程序分析:使用 random 模块. #!/user/bin/env python #coding:utf-8 import random print random.rando ...
- React Native中集成友盟社会化分享-----童叟无欺
1.下载所需的jar,下载地址https://developer.umeng.com/sdk/reactnative?spm=a211g2.211692.0.0.28967d238GW6mC 2.将以 ...
- java文件断点续传的简单实现
一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- HDU1575--Tr A(矩阵快速幂)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- Mysql中 BLOB字段转String的方法
转:https://www.cnblogs.com/renjie0520/p/5242350.html 1.通过sql直接转换 select CONVERT (*** USING utf8) AS u ...