//returns 1 if did something successfully. -1 if impossible. 0 if nothing happened int placeRow(int row, int num){ int i, j, test=0, empties=0; //find for(i=0;i<9;i++){ if(check(board[row][i],num) && !check(board[row][i],0)) return 0; if(check(board[row][i],0) && !check(notBoard[row][i],num) && !scanCol(i,num) && !scanBox((row/3)*3+i/3,num)) empties+=pow(2.0,i); } //place if(!empties) return -1; for(i=0;i<9;i++){ if(empties==pow(2.0,i)){ board[row][i]=pow(2.0,num); return 1; } } //eliminate remaining spaces in that box. if(empties<8){ for(i=(row/3)*3;i<(row/3+1)*3;i++){ for(j=0;j<3;j++){ if(i!=row && !check(notBoard[i][j],num)){ notBoard[i][j]+=pow(2.0,num); test=1; } } } if(test) return 1; } if(empties<64 && !(empties%8)){ for(i=(row/3)*3;i<(row/3+1)*3;i++){ for(j=3;j<6;j++){ if(i!=row && !check(notBoard[i][j],num)){ notBoard[i][j]+=pow(2.0,num); test=1; } } } if(test) return 1; } if(!(empties%64)){ for(i=(row/3)*3;i<(row/3+1)*3;i++){ for(j=6;j<9;j++){ if(i!=row && !check(notBoard[i][j],num)){ notBoard[i][j]+=pow(2.0,num); test=1; } } } if(test) return 1; } return 0; }
void FillerBase::setIndex(uInt scanno, uInt cycleno, uInt ifno, uInt polno, uInt beamno) { RecordFieldPtr<uInt> beamCol(row_.record(), "BEAMNO"); RecordFieldPtr<uInt> ifCol(row_.record(), "IFNO"); RecordFieldPtr<uInt> polCol(row_.record(), "POLNO"); RecordFieldPtr<uInt> cycleCol(row_.record(), "CYCLENO"); RecordFieldPtr<uInt> scanCol(row_.record(), "SCANNO"); *beamCol = beamno; *cycleCol = cycleno; *ifCol = ifno; *polCol = polno; *scanCol = scanno; }
int placeNum(void){ int i, j, num, result=0; for(i=0;i<9;i++){ for(j=0;j<9;j++){ for(num=1;num<=9;num++){ if(check(board[i][j],0) && !check(board[i][j],num) && !check(notBoard[i][j],num) && !scanRow(i,num) && !scanCol(j,num) && !scanBox((i/3)*3+j/3,num)){ board[i][j]+=pow(2.0,num); result=1; } else if(check(board[i][j],0) && check(board[i][j],num) && (check(notBoard[i][j],num) || scanRow(i,num) || scanCol(j,num) || scanBox((i/3)*3+j/3,num))){ board[i][j]-=pow(2.0,num); result=1; } } for(num=1;num<=9;num++){ if(board[i][j]==1) return -1; else if(board[i][j]==pow(2.0,num)+1){ board[i][j]--; result=1; } } } } return result; }
int placeBox(int box, int num){ int i, j, test=0, empties=0; //find for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(check(board[(box/3)*3+i][(box%3)*3+j],num) && !check(board[(box/3)*3+i][(box%3)*3+j],0)) return 0; if(check(board[(box/3)*3+i][(box%3)*3+j],0) && !check(notBoard[(box/3)*3+i][(box%3)*3+j],num) && !scanRow((box/3)*3+i,num) && !scanCol((box%3)*3+j,num)) empties+=pow(2.0,3*i+j); } } //place if(!empties) return -1; for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(empties==pow(2.0,3*i+j)){ board[(box/3)*3+i][(box%3)*3+j]=pow(2.0,num); return 1; } } } //eliminate remaining spaces in that row for(i=0,j=-1;i<9;i++){ if(check(empties,i)){ if(j==-1 || j==i/3) j=i/3; else{ j=-1; break; } } } if(j!=-1){ for(i=0;i<9;i++){ if(i/3!=box%3 && !check(notBoard[(box/3)*3+j][i],num)){ notBoard[(box/3)*3+j][i]+=pow(2.0,num); test=1; } } if(test) return 1; } //eliminate remaining spaces in that column for(i=0,j=-1;i<9;i++){ if(check(empties,i)){ if(j==-1 || j==i%3) j=i%3; else{ j=-1; break; } } } if(j!=-1){ for(i=0;i<9;i++){ if(i/3!=box/3 && !check(notBoard[i][(box%3)*3+j],num)){ notBoard[i][(box%3)*3+j]+=pow(2.0,num); test=1; } } if(test) return 1; } return 0; }
//does file input and guesses 'n checks int main(void){ int i, j, m, n, result, num, skip=0, guess=0, complete=0, guessList[81]={0}; FILE* puzzle; printf("About to overwrite 'enter_puzzle.txt'\n"); system("PAUSE"); puzzle=fopen("enter_puzzle.txt","w"); for(i=0;i<9;i++){ for(j=0;j<9;j++){ fprintf(puzzle,"0 "); if(j%3==2) fprintf(puzzle," "); } fprintf(puzzle,"\n"); if(i%3==2) fprintf(puzzle,"\n"); } fclose(puzzle); printf("\nLocate the file 'enter_puzzle.txt' and enter your puzzle.\n"); printf("Empty spaces should be left as 0. When done, click 'Save'.\n\n"); system("PAUSE"); puzzle=fopen("enter_puzzle.txt","r"); if(puzzle!=NULL){ for(i=0;i<9;i++){ for(j=0;j<9;j++) fscanf(puzzle,"%i",&board[i][j]); } fclose(puzzle); } else{ printf("\nError opening file.\n"); system("PAUSE"); return; } for(i=0;i<9;i++){ for(j=0;j<9;j++){ board[i][j]=pow(2.0,board[i][j]); notBoard[i][j]=1; } } result=solver(); if(result==1) printf("\nThe puzzle is complete! Open 'solution.txt'.\n\n"); if(result==-1) printf("\nThis puzzle is impossible.\n\n"); else{//guess and check while(!complete){ if(!skip) num=1; while(num<=9){ if(!skip) i=0; while(i<9){ if(!skip) j=0; while(j<9){ skip=0; if(check(board[i][j],0) && !check(notBoard[i][j],num) && !scanRow(i,num) && !scanCol(j,num) && !scanBox((i/3)*3+j/3,num)){ board[i][j]=pow(2.0,num); result=solver(); if(result==-1){ puzzle=fopen("enter_puzzle.txt","r"); for(m=0;m<9;m++){ for(n=0;n<9;n++){ fscanf(puzzle,"%i",&board[m][n]); board[m][n]=pow(2.0,board[m][n]); notBoard[m][n]=1; } } fclose(puzzle); solver(); for(guess=0;guessList[guess];guess++){ board[(guessList[guess]/10)%10][guessList[guess]%10]=pow(2.0,(guessList[guess]/100)%10); solver(); } } else if(result==1){ complete=1; num=10; i=9; j=9; } else{ guessList[guess]=num*100+i*10+j; printf("Guess %i: Place %i in row %i, column %i\n",guess,(guessList[guess]/100)%10,(guessList[guess]/10)%10+1,guessList[guess]%10+1); guess++; num=1; i=0; j=-1; } } j++; } i++; } num++; } if(guess && !complete){ guess--; printf("Undo guess %i\n",guess); num=(guessList[guess]/100)%10; i=(guessList[guess]/10)%10; j=guessList[guess]%10+1; skip=1; guessList[guess]=0; puzzle=fopen("enter_puzzle.txt","r"); for(m=0;m<9;m++){ for(n=0;n<9;n++){ fscanf(puzzle,"%i",&board[m][n]); board[m][n]=pow(2.0,board[m][n]); notBoard[m][n]=1; } } fclose(puzzle); solver(); for(guess=0;guessList[guess];guess++){ board[(guessList[guess]/10)%10][guessList[guess]%10]=pow(2.0,(guessList[guess]/100)%10); solver(); } } else break; } if(complete){ printf("\nThe puzzle is complete! Open 'solution.txt'.\n"); printf("Note: there may or may not be multiple solutions.\n\n"); } else printf("\nThis puzzle is impossible. Please disregard 'solution.txt'.\n\n"); } system("PAUSE"); return 0; }
void runAnalysis(int *dic, Args args, std::istream *in, profile &pro) { int s, fieldc=0, linec=0; char consensus; std::string name, column, number, line; std::vector <std::string> field; getline(*in, line); field=split(line, args.cdel); count_t samples=(field.size()-3)/3; count_t ** count=new count_t* [samples]; for(count_t i = 0 ; i < samples; ++i) count[i] = new count_t[9]; site_t site(samples); pro.setsamples(samples); pro.setcolumns(args.c); pro.set_delim_column(args.cdel); pro.set_delim_quartet(args.qdel); if (not (args.noheader) ) pro.writeheader(); /* If multiple files are given as arguments, the default behavoir should be to merge the files, * an option should be given to append the files, but in the event that duplicat ids exist in * files being appended the program should throw an error or warning. */ while( std::getline(*in, line)!=NULL){ field=split(line, args.cdel); ++linec; fieldc = field.size(); if(fieldc < 5){ printf("WARNING[mapgd proview]: Skipping line %d with only %d fields.\n", linec, fieldc); continue; } for(count_t i = 0 ; i < samples; ++i) memset(count[i], 0, sizeof(count_t)*9 ); site.id0 = field[0]; consensus = field[2][0]; if (site.extra_ids.size()==0) site.extra_ids.push_back(std::string(1, consensus)); else site.extra_ids[0] = consensus; for (count_t i = 0; i < samples; ++i){ column = field[4+i*3]; scanCol(column, dic, count[i], consensus); } bool run=false; for (count_t x=0 ; x<samples ; ++x){ s=0; for(count_t y=0; y<8; ++y) s += count[x][y]; if (s>=args.min) run=true; } if (!args.notrim){ for (count_t x=0 ; x<samples ; ++x) for (count_t y=0 ; y<4 ; y++) if (biased(count[x][y], count[x][y+4], args.pvalue) ) run=false; } if(run){ site.id1 = field[1]; for(count_t x=0 ; x<samples ; x++){ for (count_t y=0 ; y<4 ; y++){ site.sample[x].base[y]=count[x][y]+count[x][y+4]; } } pro.write(site); } } }