题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011

题目大意:在一棵树上,给你起始状态,问你能否到达终止状态。

给了树的前序遍历序。

直接dfs搜索。

 #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
#include <ctime>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int n,m,k;
char in[];
vector<PII> v[][];
int tree[];
char ch[]; bool isAccepted(int sig){
return sig>=n-m;
} bool dfs(int rt,int sig){
// printf("rt = %d sig = %d\n",rt,sig); int rtVal = tree[rt]; if( rtVal==- ){
return isAccepted(sig);
}
// printf("rtVal = %d\n",rtVal);
bool res = false;
for(int i=;i<v[sig][rtVal].SZ;i++){
res = dfs(rt<<,v[sig][rtVal][i].AA);
if(!res) continue;
res &= dfs(rt<<|,v[sig][rtVal][i].BB);
if( res ) break;
} return res;
} int main(){
int kase = ;
while( scanf("%d%d%d",&n,&m,&k)!=EOF ){
getchar();
for(int i=;i<;i++) for(int j=;j<;j++) v[i][j].CL;
if( n==&&m==&&k== ) break;
for(int i=;i<n;i++){
for(int j=;j<k;j++){
gets(in);
istringstream sss(in);
int a,b;
while( sss>> a >> b ){
v[i][j].PB(MP(a,b));
// printf("v[%d][%d].PB(%d,%d)\n",i,j,a,b);
}
}
}
// puts("*****");
if( kase!= ) puts("");
printf("NTA%d:\n",kase++);
int q;
while( true ){
memset(tree,-,sizeof(tree));
scanf("%d",&q);
if( q==- ) break;
for(int i=;i<=(<<(q+))-;i++){
scanf("%s",&ch);
if( strcmp(ch,"*")== ){
tree[i] = -;
} else {
tree[i] = ch[]-'a';
}
// printf("%d ",tree[i]);
}//puts(""); bool ok = dfs(,);
if(ok) puts("Valid");
else puts("Invalid");
} }
return ;
}

[ZOJ 1011] NTA (dfs搜索)的更多相关文章

  1. ZOJ 1011 - NTA

    题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点.最后判断所有叶子节点是否满足给定的规则.题目有点长,具体可参见原题. 首先是表格中数据的存储,由于会有多个元素, ...

  2. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  4. hihocoder 1050 树中的最长路(动态规划,dfs搜索)

    hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...

  5. sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  6. 蓝桥杯 历届试题 剪格子(dfs搜索)

    历届试题 剪格子 时间限制:1.0s   内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ...

  7. DFS搜索题素数环

    素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...

  8. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  9. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

随机推荐

  1. comet4j文档

    Comet4J配置参数表 <!--Comet4J配置 --> <listener> <description>Comet4J容器侦听</description ...

  2. weblogic性能调优参考

    1.weblogic内存的调优 2.weblogic性能调优 各个方面比较全内容较多 3.记录一次weblogic 11g压力测试性能调优过程 经调优后,具体java启动参数如下: /app/webl ...

  3. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  4. azure 云上MySQL最新版本 MySQL5.7.11 批量自动化一键式安装 (转)

    --背景云端 以前都喜欢了源码安装mysql,总觉得源码是高大上的事情,不过源码也需要时间,特别是make的时候,如果磁盘和cpu差的话,时间很长很长,在虚拟机上安装mysql尤其甚慢了. 现在业务发 ...

  5. [转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)

    首先说几个最常用的关键字,“eq” 和 “==”等同,可以使用 “and” 表示并且,“or”表示或者.“!" 和 "not” 都表示取反. 一.针对wireshark最常用的自然 ...

  6. Angular学习(8)- 路由

    示例: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 12&l ...

  7. LintCode "Swap Two Nodes in Linked List"

    Nothing special. Just take care of corner cases. class Solution { public: /** * @param head a ListNo ...

  8. 剑指offer系列51---扑克牌顺子

    [题目]抽五张扑克牌,判断五张扑克牌是不是顺子,大小王可看做任何数,0代替. package com.exe10.offer; import java.util.Arrays; /** * [题目]抽 ...

  9. Python标准库

    Python标准库是随Python附带安装的,它包含大量极其有用的模块.熟悉Python标准库是十分重要的,因为如果你熟悉这些库中的模块,那么你的大多数问题都可以简单快捷地使用它们来解决. sys模块 ...

  10. Python控制流语句(if,while,for)

    if.py number=23 guess=int(input("enter an int:")) if guess==number: print ("congratul ...