[ZOJ 1011] NTA (dfs搜索)

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

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

给了树的前序遍历序。

直接dfs搜索。

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <string>
  4 #include <iostream>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <sstream>
  8 #include <cctype>
  9 #include <vector>
 10 #include <map>
 11 #include <set>
 12 #include <iterator>
 13 #include <functional>
 14 #include <cmath>
 15 #include <numeric>
 16 #include <ctime>
 17 using namespace std;
 18 typedef long long LL;
 19 typedef pair<int,int> PII;
 20 typedef vector<int> VI;
 21 #define PB push_back
 22 #define MP make_pair
 23 #define SZ size()
 24 #define CL clear()
 25 #define AA first
 26 #define BB second
 27 #define EPS 1e-8
 28 #define ZERO(x) memset((x),0,sizeof(x))
 29 const int INF = ~0U>>1;
 30 const double PI = acos(-1.0);
 31
 32 int n,m,k;
 33 char in[1000];
 34 vector<PII> v[1000][1000];
 35 int tree[10000];
 36 char ch[2];
 37
 38 bool isAccepted(int sig){
 39     return sig>=n-m;
 40 }
 41
 42 bool dfs(int rt,int sig){
 43 //    printf("rt = %d sig = %d\n",rt,sig);
 44
 45     int rtVal = tree[rt];
 46
 47     if( rtVal==-1 ){
 48         return isAccepted(sig);
 49     }
 50 //    printf("rtVal = %d\n",rtVal);
 51     bool res = false;
 52     for(int i=0;i<v[sig][rtVal].SZ;i++){
 53         res = dfs(rt<<1,v[sig][rtVal][i].AA);
 54         if(!res) continue;
 55         res &= dfs(rt<<1|1,v[sig][rtVal][i].BB);
 56         if( res ) break;
 57     }
 58
 59     return res;
 60 }
 61
 62 int main(){
 63     int kase = 1;
 64     while( scanf("%d%d%d",&n,&m,&k)!=EOF ){
 65         getchar();
 66         for(int i=0;i<1000;i++) for(int j=0;j<1000;j++) v[i][j].CL;
 67         if( n==0&&m==0&&k==0 ) break;
 68         for(int i=0;i<n;i++){
 69             for(int j=0;j<k;j++){
 70                 gets(in);
 71                 istringstream sss(in);
 72                 int a,b;
 73                 while( sss>> a >> b ){
 74                     v[i][j].PB(MP(a,b));
 75 //                    printf("v[%d][%d].PB(%d,%d)\n",i,j,a,b);
 76                 }
 77             }
 78         }
 79 //        puts("*****");
 80         if( kase!=1 ) puts("");
 81         printf("NTA%d:\n",kase++);
 82         int q;
 83         while( true ){
 84             memset(tree,-1,sizeof(tree));
 85             scanf("%d",&q);
 86             if( q==-1 ) break;
 87             for(int i=1;i<=(1<<(q+1))-1;i++){
 88                 scanf("%s",&ch);
 89                 if( strcmp(ch,"*")==0 ){
 90                     tree[i] = -1;
 91                 } else {
 92                     tree[i] = ch[0]-‘a‘;
 93                 }
 94 //                printf("%d ",tree[i]);
 95             }//puts("");
 96
 97             bool ok = dfs(1,0);
 98             if(ok) puts("Valid");
 99             else puts("Invalid");
100         }
101
102     }
103     return 0;
104 }
时间: 2024-08-29 18:52:52

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

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

hdu1372 dfs搜索之国际象棋的马

原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到另一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四个方向.而是由"日" 字组成的8个方向.虽然是国际象棋的马,但是其实和中国象棋的马走法还是一样的. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std;

URAL 1298 knight dfs搜索

1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard. No, he did not like usual chess game, and in addition, he d

nyist oj 19 擅长排列的小明(dfs搜索+STL)

擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) 输出 在1-n中选

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu1455 dfs搜索之凑棍子

原题地址 这道题和poj的拯救少林神棍是一样的题目. 要用给出的小棍凑成等长的棍子,求能凑成的棍子的最小长度. 直观的包里思路就是枚举所有可能的长度,然后不停的测试小棍组合,先把小棍加入组合,然后不合适就推翻这一根小棍,再测试下一个小棍,直到推翻所有的小棍. 在枚举的时候,我们只需从最长的小棍长,枚举到小棍总长的一半就行了.然后如果再不符合的话,那么就说明所有小棍只能组合成一根棍子了. 我原先看过关于poj上拯救少林神棍这道题目的详细讲解.一个DFS搜索题,这里DFS共有四种剪枝方案: 所给出的

nyist oj 58 最少步数(dfs搜索)

最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0,0,1,1,0,0,0,1 1,0,1,0,1,1,0,1,1 1,0,0,0,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,1,0,0,1 1,1,0,1,0,0,0,0,1 1,1,1,1,1,1,1,1,1 0表示道路,1表示墙. 现在输入一个道路的坐标作为

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

链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of n vertices. Each vertex contains a low