[ZOJ 1011] NTA (dfs搜索)
题目链接: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搜索)的更多相关文章
- ZOJ 1011 - NTA
题目大意:有一颗完全二叉树,给节点一个信号会从一个表中选择一对信号分别传递给两个子节点.最后判断所有叶子节点是否满足给定的规则.题目有点长,具体可参见原题. 首先是表格中数据的存储,由于会有多个元素, ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- hihocoder 1050 树中的最长路(动态规划,dfs搜索)
hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...
- sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...
- 蓝桥杯 历届试题 剪格子(dfs搜索)
历届试题 剪格子 时间限制:1.0s 内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
随机推荐
- 史上最全Java表单验证封装类
package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...
- Angular学习(3)- 双向梆定
示例代码: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 3& ...
- http://www.dayandeng.com/ 诈骗网站
http://www.dayandeng.com/ 诈骗网站 http://www.dayandeng.com/userfiles/media/2018/awzosv16.html 骗取你的京 ...
- web api 500 错误
改了API 方法内容后,就直接F5运行起来. 客户端用WebClient请求,老是返回500错误. 无意中重新编译了WEIAPI项目,客户端就正常了.
- LintCode "Continuous Subarray Sum"
A variation to a classical DP: LCS. class Solution { public: /** * @param A an integer array * @retu ...
- 如何解决WebkitBrowser使用出错“Failed to initialize activation context”
本文转载自:http://www.cnblogs.com/supjia/p/4695671.html 本篇文章主要介绍了"如何解决WebkitBrowser使用出错“Failed to in ...
- 【设计模式】工厂方法模式(Factory Method)
工厂方法模式 定义了一个创建对象的接口,但由子类决定要实现的类是哪一个.工厂方法让类把实例化推迟到子类.所有的工厂模式都用来封装对象的创建.工厂方法模式通过让子类决定改创建的对象是什么,来达到将对象创 ...
- android学习笔记33——资源ShapeDrawable
ShapeDrawable ShapeDrawable用于定义一个基本的几何图像(如,矩形.圆形.线条.......). 定义ShapeDrawable的XML文件的根元素是<shape.../ ...
- MyEclipse9中的不伤眼修改、FreeMarker插件、JQuery提示插件、全屏(FullScreen)插件的安装
============下载相关附件===================== http://files.cnblogs.com/fhtwins/eclipse-fullscreen_1.0.7.zi ...
- hibernate.cfg.xml讲解
<session-factory> <!-- 配置数据库连接信息 --> <!-- 数据库驱动 --> <property name="connec ...