void ParenCat( void ) { //========================== // Check if ) matches ( as opposed to [. // called on ) // sequence itnode *cit; bool ok_to_axe; bool all_const_opns; cit = findMatch( &ok_to_axe, &all_const_opns ); if( cit != NULL ) { // consider: a(1)(2:3)//c if( ( cit->opr == OPR_LBR ) && ok_to_axe ) { ReqNOpn(); cit->is_catparen = 1; cit = CITNode; AdvanceITPtr(); FreeOneNode( cit ); // check for CHAR(73) - CHAR is allowed in constant expressions } else if( (cit->opr != OPR_FBR) || !all_const_opns || ((cit->link->flags & SY_CLASS ) != SY_SUBPROGRAM) || (!(cit->link->flags & SY_INTRINSIC)) || (cit->link->sym_ptr->ns.si.fi.index != IF_CHAR) ) { ChkConstCatOpn( CITNode->link ); } } BackTrack(); }
void ChkCatOpn( void ) { //=========================== // Check if ) is the start of a concatenation operand. // Called on ) rel sequence since only relational operators are allowed with // character arguments. // Consider: // if( a(1)//a(2) .eq. 'ab' )then // We want to evaluate 'ab' first. Otherwise, a(2) would get evaluated, // followed by 'ab' and finally a(1) -- which is incorrect. itnode *cit; bool ok_to_axe; cit = findMatch( &ok_to_axe, NULL ); if( cit != NULL ) { if( cit->opr == OPR_FBR ) { if( cit->link->opr == OPR_CAT ) { CatOpn(); return; } } } BackTrack(); }
static void ParenExpr( void ) { //================================= // Finish off evaluation of a parenthesized expression. // don't evaluate constants enclosed in parentheses // so that they can be folded. Consider: (3+4)+5 if( CITNode->opn.us != USOPN_CON ) { // Consider: CHARACTER A // IF( ('9') .NE. (A) ) CONTINUE // make sure that we can optimize the character operation if( CITNode->opn.us == USOPN_NNL ) { if( CITNode->typ == FT_CHAR ) { int ch_size; ch_size = CITNode->size; if( OptimalChSize( ch_size ) ) { CITNode->value.st.ss_size = ch_size; CITNode->opn.us |= USOPN_SS1; } } } PushOpn( CITNode ); GParenExpr(); } BackTrack(); }
void CatAxeParens( void ) { //============================== // Remove LBR on ( // sequence. // // Before: | After: // --------------- | --------------- // | opr1 | PHI | | released** // --------------- | --------------- // CIT ==> | ( | opn2 | | CIT ==> | opr1 | opn2 | // --------------- | --------------- // | // | opn3 | | | // | opn3 | // --------------- | --------------- // ... | ... // // ** see KillOpnOpr() for case where first node is start-node of expr BackTrack(); ReqNOpn(); MoveDown(); if( CITNode->is_catparen ) { KillOpnOpr(); } else { FiniCat(); } }
int main() { clock_t start, finish; double duration; int nn; while (scanf_s("%d", &nn) != EOF) { n = nn; sum = 0; for (int i = 0; i <= n; ++i) x[i] = 0; start = clock(); BackTrack(); //BackTrack1(1); finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf("%f seconds\n", duration); //BackTrack1(1); printf("%d\n", sum); } return 0; }
void CatOpn( void ) { //======================== // Process a concatenation operand. GenCatOpn(); BackTrack(); }
static void ProcOpn( void ) { //=============================== // Process operand and then scan backwards. // Currently called for = -| sequence, and most % opr sequences. EvalOpn(); BackTrack(); }
void AdvError( int code ) { //============================ // Advance CITNode, call error routine, reset CITNode. // Use this routine to get error-pointer ( "carrot" ) in proper place MoveDown(); Error( code ); BackTrack(); }
void BackTrack(int n) { int i,j; if(n==m) { flag=1; return; } if(flag) return; for(i=0;i<6;i++) if(!strcmp(member[n][0],sz[i])) break; for(j=0;j<6;j++) if(!strcmp(member[n][1],sz[j])) break; if(data[i]) { data[i]--; BackTrack(n+1); data[i]++; } if(data[j]) { data[j]--; BackTrack(n+1); data[j]++; } return; }
int main() { int i,j,k,l,n,t,x,y,blank=0,test=0; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); for(i=0,l=n/6;i<6;i++) data[i]=l; for(i=flag=0;i<m;i++) scanf("%s %s",member[i][0],member[i][1]); BackTrack(0); printf("%s\n",flag ? "YES" : "NO"); } return 0; }
// Returns the size cost of a string for a lattice path that // ends at the specified lattice node. int BeamSearch::SizeCost(SearchObject *srch_obj, SearchNode *node, char_32 **str32) const { CharSamp **chars = NULL; int char_cnt = 0; if (!node) return 0; // Backtrack to get string and character segmentation chars = BackTrack(srch_obj, node, &char_cnt, str32, NULL); if (!chars) return WORST_COST; int size_cost = (cntxt_->SizeModel() == NULL) ? 0 : cntxt_->SizeModel()->Cost(chars, char_cnt); delete []chars; return size_cost; }
int main() { int i,j,test=0,data[8],diff,min; BackTrack(0); while(scanf("%d",&data[0])!=EOF) { for(i=1;i<8;i++) scanf("%d",&data[i]); for(i=0,min=1000;i<top;i++) { for(j=diff=0;j<8;j++) if(soln[i][j]!=data[j]) diff++; if(diff<min) min=diff; } printf("Case %d: %d\n",++test,min); } return 0; }
void BackTrack(int n) { int i; if(n==8) { for(i=0;i<8;i++) soln[top][i]=col[i]; top++; return; } for(i=0;i<8;i++) { if(!row[i] && !d1[7+i-n] && !d2[i+n]) { row[i]=d1[7+i-n]=d2[i+n]=1; col[n]=i+1; BackTrack(n+1); row[i]=d1[7+i-n]=d2[i+n]=0; } } return; }
static void USCleanUp( void ) { //================================= // Clean up text list after expression error has occurred // releasing all nodes on the way, leaving: // +------------------+ // CIT ==> | OPR_TRM | | // +------------------+ // | OPR_TRM | | // +------------------+ // NOTE : CITNode must not be pointing at the end of expression terminal itnode *junk; itnode *first; while( CITNode->opr != OPR_TRM ) { BackTrack(); } first = CITNode; switch( first->opn.us & USOPN_WHAT ) { case USOPN_NWL: case USOPN_ASS: if( first->list != NULL ) { FreeITNodes( first->list ); } } CITNode = CITNode->link; while( CITNode->opr != OPR_TRM ) { junk = CITNode; CITNode = CITNode->link; FreeOneNode( junk ); } first->link = CITNode; CITNode = first; CITNode->typ = FT_NO_TYPE; CITNode->opn.ds = DSOPN_PHI; }
void CatBack( void ) { //========================= // Scan back on = // sequence if RHS is a char expression. // All parens and lists must have been removed already. // Consider: l = a//b .eq. c//d // vs // c = x//y//z itnode *itptr; itptr = CITNode->link->link; // point one operator past "//" for(;;) { if( itptr->opr == OPR_TRM ) break; if( itptr->opr != OPR_CAT ) { FiniCat(); return; } itptr = itptr->link; } FoldCat(); BackTrack(); // eg : a = b//c//d//e }
static void Generate( void ) { //================================ // Generate code. TYPE typ1; TYPE typ2; OPTR op; OPR opr; itnode *next; unsigned_16 mask; uint res_size; next = CITNode->link; if( next->opn.ds == DSOPN_PHI ) { BadSequence(); } else { typ1 = CITNode->typ; typ2 = next->typ; opr = next->opr; if( RecNOpn() ) { typ1 = FT_NO_TYPE; CITNode->size = next->size; if( (opr != OPR_PLS) && (opr != OPR_MIN) && (opr != OPR_NOT) ) { BadSequence(); return; } } op = OprNum[ opr ]; if( typ1 == FT_NO_TYPE ) { mask = LegalOprsU[ typ2 - FT_FIRST ]; } else { mask = LegalOprsB[ ( typ2 - FT_FIRST ) * LEGALOPR_TAB_COLS + typ1 - FT_FIRST ]; } if( (( mask >> ( op - OPTR_FIRST ) ) & 1) == 0 ) { // illegal combination MoveDown(); if( typ1 == FT_NO_TYPE ) { TypeErr( MD_UNARY_OP, typ2 ); } else if( typ1 == typ2 ) { TypeErr( MD_ILL_OPR, typ1 ); } else { TypeTypeErr( MD_MIXED, typ1, typ2 ); } BackTrack(); } else if( DoGenerate( typ1, typ2, &res_size ) ) { if( ( opr >= OPR_FIRST_RELOP ) && ( opr <= OPR_LAST_RELOP ) && ( (ResultType == FT_COMPLEX) || (ResultType == FT_DCOMPLEX) || (ResultType == FT_XCOMPLEX) ) && ( opr != OPR_EQ ) && ( opr != OPR_NE ) ) { // can only compare complex with .EQ. and .NE. Error( MD_RELOP_OPND_COMPLEX ); } else { if( ( next->opn.us == USOPN_CON ) && ( ( CITNode->opn.us == USOPN_CON ) || ( typ1 == FT_NO_TYPE ) ) ) { // we can do some constant folding ConstTable[ op ]( typ1, typ2, op ); } else { // we have to generate code if( CITNode->opn.us == USOPN_CON ) { AddConst( CITNode ); } else if( next->opn.us == USOPN_CON ) { AddConst( next ); } GenOprTable[ op ]( typ1, typ2, op ); } } switch( opr ) { case OPR_EQV: case OPR_NEQV: case OPR_OR: case OPR_AND: case OPR_NOT: if( _IsTypeInteger( typ1 ) ) { Extension( MD_LOGOPR_INTOPN ); } break; case OPR_EQ: // relational operators case OPR_NE: case OPR_LT: case OPR_GE: case OPR_LE: case OPR_GT: ResultType = FT_LOGICAL; res_size = TypeSize( ResultType ); break; case OPR_FLD: case OPR_DPT: // set result size to size of field res_size = next->size; FixFldNode(); break; } CITNode->size = res_size; CITNode->typ = ResultType; FixList(); } }