训练[2]-DFS
题目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的更多相关文章
- 计蒜客 ACM训练联盟周赛 第一场 从零开始的神棍之路 暴力dfs
题目描述 ggwdwsbs最近被Zeratul和Kyurem拉入了日本麻将的坑.现在,ggwdwsbs有13张牌,Kyurem又打了一张,加起来有14张牌.ggwdwsbs想拜托你帮他判断一下,这14 ...
- Java实现 蓝桥杯VIP 算法训练 步与血(递推 || DFS)
试题 算法训练 步与血 问题描述 有n*n的方格,其中有m个障碍,第i个障碍会消耗你p[i]点血.初始你有C点血,你需要从(1,1)到(n,n),并保证血量大于0,求最小步数. 输入格式 第一行3个整 ...
- ALGO-125_蓝桥杯_算法训练_王、后传说(DFS)
问题描述 地球人都知道,在国际象棋中,后如同太阳,光芒四射,威风八面,它能控制横.坚.斜线位置. 看过清宫戏的中国人都知道,后宫乃步步惊心的险恶之地.各皇后都有自己的势力范围,但也总能找到相安无事的办 ...
- 2018.11.01 NOIP训练 图论(线段树+倍增+dfs序)
传送门 一道挺妙的题. 对于询问点(u,v),如右图所示,我们可以发现存在一个点m在u->v的路径中,m子树的点到u是最近的,m子树外到v是最近的.其中dis(u,m)=(dis(u,v)-1) ...
- 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 ...
- 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 ...
- Black And White (DFS 训练题)
G - Black And White ================================================================================ ...
- 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing
acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...
- dfs/bfs专项训练
A.棋盘问题——poj1321 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放 ...
随机推荐
- 精通CSS+DIV基础总结(三)
Div+CSS可是实现简单的网页设计,对于需要添加动作,更方便的交互,Javascript给我们提供了方便:数据的存储可以利用XML使其更加方便:而对于减少页面与服务器的交互,可以利用Ajax技术,与 ...
- [置顶] linux学习之samba安装问题详解
一.首先查看是否安装samba,命令为:rpm -qa | grep samba 出现如下包表示已经安装,否则没有安装 samba-winbind-clients-3.5.10-125.el6.i68 ...
- [置顶] IT老男人读《因为痛,所以叫青春》
最近偶然,从别人的书桌上看到这本书,其中有个关于时间的解释,很是让为成功焦虑的老男人受用. 因此,我喜欢将人生的80年跟一天中的24小时进行对照. 人生时钟的计算方法十分简单.24小时相当于144 ...
- lll
//// whywhy unsigned int T = 1; ~T = 4294967294; T = 2;~T= 4294967293 ;T = 0;~T=4294967295; int T = ...
- kubernetes入门之快速部署
角色说明 这里主要有三个角色,分别部署不同的服务. 角色 服务 etcd etcd master kube-apiserver/kube-scheduler/kube-controller node ...
- jQuery Ajax用法
jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍. 我们先从最简单的方法看起 ...
- __import__简介
__import__() import 语句通过调用__import__()来完成工作,提供这个函数是为了让有特殊需要的用户覆盖他,实现自定义.__import__最大的好处就是可以使程序在r ...
- IMG图片垂直居中的问题
之前老是碰到图片文字位置调整的问题,图片不按自己的要求变化,后来发现其实很简单. <P><img src="" style="vertical-alig ...
- Array类型方法总结(未写完)
转换方法: toString() toLocaleString() valueOf() 栈方法: push()---在数组末尾添加项,返回修改后的长度, pop()---在数组末尾移除项,返 ...
- angularJS 自定义元素和属性
创造自定义元素和属性的方法是:directive('string',function(){ return{}; }); ①函数接收两个参数:一个字符串(指令的名字),一个函数: ②回调函数必须返回一个 ...