题目A:

题目B【https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=614】

题意:

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a) if it is the empty string

(b) if A and B are correct, AB is correct,

(c) if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

题解:虽然长度最长只有128,但是是多组输入,用区间DP会TLE。简单stack的应用。

#include<stack>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 1e6;
const int MAXN = ;
char S[MAXN];
int T, N;
int main ()
{
scanf("%d", &T);
getchar();
while(T--)
{
gets(S + );
N = strlen(S + );
if(S[] == ' ')//第一种情况
{
printf("Yes\n");
continue;
}
stack<char>st;
for(int i = ; i <= N; i++)
{
if(!st.empty() && ((st.top() == '(' && S[i] == ')') || (st.top() == '[' && S[i] == ']')))
st.pop();//匹配
else st.push(S[i]);
}
if(st.empty()) printf("Yes\n");
else printf("No\n");
}
return ;
}

题目E【http://poj.org/problem?id=2386】

题意:给出一个图,mp[i][j]=='.'表示该地为干,mp[i][j]='W'表示该地是水坑,两个水坑联通的条件是八个方向任意一个方向联通则联通。求不联通水坑的个数。

题解:DFS,DFS的次数表示非联通水坑的数量。

#include<stack>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN = ;
char mp[MAXN][MAXN];
int N, M;
void DFS(int x, int y)
{
mp[x][y] = '.';
for(int i = -; i <= ; i++)
for(int j = -; j <= ; j++)
if(mp[x+i][y+j] == 'W')
DFS(x+i, y+j);
}
int main ()
{
int ans = ;
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
scanf("%s", mp[i] + );
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
if(mp[i][j] == 'W') DFS(i, j), ans++;
printf("%d\n", ans);
}

训练[2]-DFS的更多相关文章

  1. 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs

    题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...

  2. Java实现 蓝桥杯VIP 算法训练 步与血(递推 || DFS)

    试题 算法训练 步与血 问题描述 有n*n的方格,其中有m个障碍,第i个障碍会消耗你p[i]点血.初始你有C点血,你需要从(1,1)到(n,n),并保证血量大于0,求最小步数. 输入格式 第一行3个整 ...

  3. ALGO-125_蓝桥杯_算法训练_王、后传说(DFS)

    问题描述 地球人都知道,在国际象棋中,后如同太阳,光芒四射,威风八面,它能控制横.坚.斜线位置. 看过清宫戏的中国人都知道,后宫乃步步惊心的险恶之地.各皇后都有自己的势力范围,但也总能找到相安无事的办 ...

  4. 2018.11.01 NOIP训练 图论(线段树+倍增+dfs序)

    传送门 一道挺妙的题. 对于询问点(u,v),如右图所示,我们可以发现存在一个点m在u->v的路径中,m子树的点到u是最近的,m子树外到v是最近的.其中dis(u,m)=(dis(u,v)-1) ...

  5. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  6. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  7. Black And White (DFS 训练题)

    G - Black And White ================================================================================ ...

  8. 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing

    acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...

  9. dfs/bfs专项训练

    A.棋盘问题——poj1321 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放 ...

随机推荐

  1. STL---基本算法---<stl_algobase.h>概述

    通过一个实例来说明这些算法的接口使用: #include <iostream> #include <algorithm> #include <functional> ...

  2. .Net程序员学用Oracle系列(3):数据库编程规范

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.书写规范 1.1.大小写风格 1.2.缩进风格 1.3.换行 1.4.其它 2.命名规范 2.1.数据库对象命名 2.2.变 ...

  3. 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)

    前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...

  4. 轻松搞定javascript日期格式化问题

    Date.prototype.format = function(f){ var d = this f = f || "yyyy-MM-dd hh:mm:ss" return f. ...

  5. 【CSS学习笔记】CSS初始化

    腾讯QQ官网(http://www.qq.com)样式初始化 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input ...

  6. buildroot 重新编译 package

    /************************************************************************* * buildroot 重新编译 package ...

  7. dev gridcontrol 绑定int型及日期型的列默认当值为0时显示空白及格式化日期显示方式

    xmlns:sys="clr-namespace:System;assembly=mscorlib" 如只显示日期的时间部分 <dxg:GridColumn Header=& ...

  8. TypeScript 学习四 面向对象的特性,泛型,接口,模块,类型定义文件*.d.ts

    1,面向对象的特性一:类,继承,见上一篇博客: 2,面向对象的特性二: 泛型(generic):参数化的类型,一般用来限制集合的内容:指定只能放某个类型的元素 如下图中的尖括号中的Person,就代表 ...

  9. NullSafe 的原理

    摘要 NullSafe is a simple category on NSNull that returns nil for unrecognised messages instead of thr ...

  10. table中td内容过长 省略号显示

    首先设置 css样式: table { table-layout: fixed;} HTML中的table代码: <tr> <th class="col-md-1" ...