int main() { while(getline(cin, line)) { istringstream parsing(line); STACK matrioshkas; SETFALSE(flag1); SETTRUE(flag2); while(parsing >> m && flag2) { SETTRUE(flag1); if(m < 0) { m = labs(m); if(!matrioshkas.empty()) { (matrioshkas.top().second <= m) ? SETFALSE(flag2) : matrioshkas.top().second -= m; } PUSH(matrioshkas, make_pair(m, m)); } else { if(matrioshkas.empty() || matrioshkas.top().first != m) SETFALSE(flag2); else POP(matrioshkas); } } if(!flag1 || !matrioshkas.empty()) SETFALSE(flag2); cout << ((flag2) ? ":-) Matrioshka!\n" : ":-( Try again.\n"); } return 0; }
// verifies equality of two STACK objects friend int operator==(STACK a, STACK b) { // Take care of empty cases. int status = 0; // set status to false if (a.empty() && b.empty()) status = 1; // both STACK objects are empty // Handle non-empty cases else if (a.N == b.N) { // number of items on each STACK are equal // check equality of each item for (int i = 0; i < N; i++) { if (a.s[i] == b.s[i]) continue; else break; } if (i == N) // all items in both STACK's are equal status = 1; } return status; } // end friend function operator==
void show_impl(STACK stack) { std::cout << "["; while (!stack.empty()) { std::cout << stack.back(); stack.pop_back(); if (!stack.empty()) {std::cout << ", "; } } std::cout << "]" << std::endl; return; }
void emptyStacks(VVI &inp,STACK &sti,STACK &stj,int &val){ while(!sti.empty()){ int i = sti.top(); sti.pop(); int j = stj.top(); stj.pop(); if(inp[i][j]<val){ inp[i][j] = val; } } val = 0; }
REGION Acquisition::seedFill( REG_COLOR colorID, unsigned int u, unsigned int v) { #define canBePainted( colorID, u, v) (!analisedPixel(u,v) && calibracaoParam.getHardColor(ImBruta[v][u]) == colorID) static STACK s; REGION region; double su=0, sv=0, suu=0, svv=0, suv=0; int nPixel=0; unsigned int v1; unsigned int vMax=0,vMin=ImBruta.nlin(); unsigned int uMax=0,uMin=ImBruta.ncol(); bool expanLeft, expanRight; region.nPixel=0; region.colorID = colorID; if ( !canBePainted(colorID,u,v) ) return region; s.empty(); if (!s.push(u,v)) { cerr << "Buffer estourou 1!\n"; return region; } while(s.pop(u,v)) { v1 = v; while(v1>0 && canBePainted(colorID,u,v1-1)) v1--; expanLeft = expanRight = false; while(v1<ImBruta.nlin() && canBePainted(colorID,u,v1) ) { analisedPixel.setValue(u,v1,true); if(v1 < vMin) vMin = v1; if(v1 > vMax) vMax = v1; if(u < uMin) uMin = u; if(u > uMax) uMax = u; su += u; sv += v1; suu += u*u; svv += v1*v1; suv += u*v1; nPixel++; if(!expanLeft && u>0 && canBePainted(colorID,u-1,v1)) { if(!s.push(u-1,v1)) { cerr << "Buffer estourou 2!\n"; return region; } expanLeft = true; } else if(expanLeft && u>0 && !canBePainted(colorID,u-1,v1)) { expanLeft = false; } if(!expanRight && u<(ImBruta.ncol()-1) && canBePainted(colorID,u+1,v1)) { if(!s.push(u+1,v1)) { cerr << "Buffer estourou 3!\n"; return region; } expanRight = true; } else if(expanRight && u<(ImBruta.ncol()-1) && !canBePainted(colorID,u+1,v1)) { expanRight = false; } v1++; } } //testa se a regiao é uma linha vertical ou horizontal if((vMax-vMin) > LINE_THRESHOLD || (uMax-uMin) > LINE_THRESHOLD) return region; region.center.u() = su/nPixel; region.center.v() = sv/nPixel; region.nPixel = nPixel; double varu, varv, varuv; varu = suu/nPixel - (su/nPixel)*(su/nPixel); //a varv = svv/nPixel - (sv/nPixel)*(sv/nPixel); //c varuv = suv/nPixel - (su/nPixel)*(sv/nPixel); //b //testa se a regiao eh simetrica, ou seja, nao tem como calcular o angulo do segundo momento. double lim_zero = 0.001; if(fabs(varuv) < lim_zero && fabs(varu-varv) < lim_zero){ //a figura é simetrica region.symetric = true; region.orientation = 0.0; }else{ region.symetric = false; region.orientation = -atan2(varuv,varu-varv)/2.0; } return region; }