CDOJ—1387简单的双向搜索

这是一道裸的双向DFS


题目大意

给定三个大小为NN的整型数组A1,A2,A3A1,A2,A3,和三个整数B1,B2,B3B1,B2,B3。接下来依次做NN次操作:对于第ii次操作(i=1,2,..,N)(i=1,2,..,N),选择一个数jj(j=1,2,3j=1,2,3),令BjBj加上Aj[i]Aj[i]。问是否存一种操作序列,使得最后B1=B2=B3=0B1=B2=B3=0。

CDOJ题目传送门

#include 
#include 
#include 
using namespace std;

#define maxn 30

int N, ex[maxn], aux[maxn][3], ed;

set< pair < int, pair < int, int > > >ha;

bool dfs( int a, int b, int c, int d, int f){
    if ( d == ed ){
        if ( f ){
            ha.insert( make_pair( a, make_pair( b, c ) ) );
            return false;
        }
        if ( ha.count( make_pair( -ex[0] - a, make_pair( -ex[1] - b, -ex[2] - c)))) return true;
        return false;
    }
    if ( dfs( a + aux[d][0], b, c, d+1, f)) return true;
    if ( dfs( a, b + aux[d][1], c, d+1, f)) return true;
    if ( dfs( a, b, c + aux[d][2], d+1, f)) return true;
}
int main(){
    scanf( "%d", &N);
    for ( int i = 0; i < 3; ++i) scanf( "%d", ex + i );
    for ( int i = 0; i < N; ++i) for ( int j = 0; j < 3; ++j) scanf( "%d", &aux[i][j]);
    ed = N / 2 + 1;
    dfs( 0, 0, 0, 0, 1);
    ed = N;
    if ( dfs( 0, 0, 0, N / 2 + 1, 0 )) printf("YES\n");
    else printf("NO\n");
    return 0;

}

你可能感兴趣的:(暴力——搜索)