Exemplo n.º 1
0
    void writeOut() {
        qsort(hs, n, sizeof(Hole), hcmp);
        for (int i = 0; i < n; i++) {
            hs[i].id = i;
        }

        for (int i = 0; i < n-1; i++) {
            Hole * th = &hs[i];
            Hole * nx = &hs[i+1];
            if (th->x == nx->x) {
                assert(th->y != nx->y);
                nx->last = th;
                th->next = nx;
            } else {
                nx->last = NULL;
                th->next = NULL;
            }
        }

        int max = maxid();
        int res = 0;

        for (int id = 0; id < max; id++) {
            pairIt(id);
            if (findCycle()) {
                res++;
            }
        }

        fout = fopen("wormhole.out", "w"); assert(fout);
        fprintf(fout, "%d\n", res);

        fclose(fout);
    }
Exemplo n.º 2
0
// true if diagram intersects previous diagram at the begin
QList<QPair<int, bool> > FormSegmentator::findCycle(
		QList<QPair<int, bool> > const &polygon)
{
	Component firstDiagram = mAllComponents.at(polygon.at(0).first);
	Component lastDiagram = mAllComponents.at(polygon.back().first);
	int posFirst = polygon.at(0).second ? 0 : firstDiagram.size() - 1;
	int posLast = (!polygon.back().second) ? 0 : lastDiagram.size() - 1;
	int maxCycleSize = mAllComponents.size();
	QList<QPair<int, bool> > cycle;
	if (firstDiagram.at(posFirst).dist(lastDiagram.at(posLast))
			<= neighbourhoodRad)
	{
		return polygon;
	}
	for (int i = 0; i < mAllComponents.size(); i ++) {
		Component diagram = mAllComponents.at(i);
		if (diagram.back().dist(diagram.at(0)) <= neighbourhoodRad
				|| contains(polygon, i))
		{
			continue;
		}
		if (diagram.at(0).dist(lastDiagram.at(posLast)) <= neighbourhoodRad) {
			QList<QPair<int, bool> > newPolygon = polygon;
			newPolygon.push_back(QPair<int, bool>(i, true));
			newPolygon = findCycle(newPolygon);
			if (newPolygon.size() > 1 && newPolygon.size() <= maxCycleSize) {
				cycle = newPolygon;
				maxCycleSize = newPolygon.size();
			}
		}
		if (diagram.back().dist(lastDiagram.at(posLast)) <= neighbourhoodRad) {
			QList<QPair<int, bool> > newPolygon = polygon;
			newPolygon.push_back(QPair<int, bool>(i, false));
			newPolygon = findCycle(newPolygon);
			if (!newPolygon.empty() && newPolygon.size() <= maxCycleSize) {
				cycle = newPolygon;
				maxCycleSize = newPolygon.size();
			}
		}
	}
	return cycle;
}
Exemplo n.º 3
0
CycleDetect::Result CycleDetect::run (const CycleDetect::Parameter& parm)
{
    Identity g_id = parm.id_;
    Result result = { .hasCycle_ = false };

    NodeStatus node;
    Graph::VertexList list = logic_.verticesOf(g_id);
    try {
        findCycle(*list.begin(), NULL_IDENTITY, &node);
    }
    catch (CycleFound&) {
        result.hasCycle_ = true;
    }

    return result;
}

void CycleDetect::findCycle (const Identity v_id,
                             const Identity parent_id,
                             NodeStatus *node)
{
    if (node->statusOf(v_id) == NodeStatus::GREY) {
        throw CycleFound();
    }

    node->visit(v_id);
    Graph::VertexList neighbours = logic_.neighboursOf(v_id);
    std::for_each(neighbours.begin(), neighbours.end(),
                  [&] (const Identity id) {
                      if (id != parent_id &&
                          node->statusOf(id) != NodeStatus::BLACK) {
                          findCycle(id, v_id, node);
                      }
                  });

    node->leave(v_id);
}
Exemplo n.º 4
0
// solution 2
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    if(headA==NULL || headB == NULL) {
        return NULL;
    }
	//find out the tail of headA
	ListNode *cur = headA;
	while(cur->next) {
		cur=cur->next;
	}
	cur->next = headB;

	ListNode *res=findCycle(headA);
	cur->next = NULL;
	return res;
}
Exemplo n.º 5
0
//todo:: make this method more smart, maybe it would be unite some components incorrectly
void FormSegmentator::uniteCornersWithEdges()
{
	bool wasUnited = true;
	while (wasUnited) {
		wasUnited = false;
		int i = 0;
		while (i < mAllComponents.size() && !wasUnited) {
			// unnecessary because off condition to cycle at findCycle method
			if (mAllComponents.at(i).at(0).dist(mAllComponents.at(i).back())
					<= neighbourhoodRad) {
				i++;
				continue;
			}
			QList<QPair<int, bool> > polygon;
			polygon.push_back(QPair<int, bool>(i, true));
			polygon = findCycle(polygon);
			qDebug() << "found cycle" << polygon.size();
			if (polygon.empty()) {
				i++;
				continue;
			}
			wasUnited = true;
			while (polygon.size() > 1) {
				//TODO:: add comments!!!
				QPair<int, bool> firstMerge = polygon.at(0);
				QPair<int, bool> secondMerge = polygon.at(1);
				polygon.pop_front();
				polygon.pop_front();
				// !firstMerge.second = false
				isMergedDiagrams(firstMerge.first, secondMerge.first,
								 !firstMerge.second, secondMerge.second);
				for (int j = 0; j < polygon.size(); j ++) {
					if (polygon.at(j).first >= firstMerge.first) {
						polygon[j].first --;
					}
					if (polygon.at(j).first >= secondMerge.first) {
						polygon[j].first --;
					}
				}
				// push to polygon merged components, the first component always has true value
				polygon.push_front(QPair<int, bool>(mAllComponents.size() - 1, true));
			}
		}

	}
}
Exemplo n.º 6
0
void findCycle(NODEPTR p, int graph[][4], int baslangic, int bitis)
{
	int i, k;

	for (i = 0; i <= 3; ++i)
	{
		if (graph[baslangic - 1][i] == 1 && !isThere(p, i + 1))
		{
			place(&p, i + 1);
			break;
		}
	}

	if (i + 1 == bitis)
		return;

	findCycle(p, graph, i + 1, bitis);
}
Exemplo n.º 7
0
void MainWindow::on_startButton_clicked()
{
    row = ui->tableProvider->rowCount();
    column = ui->tableCustomer->columnCount();

    fillProviders();
    fillCustomers();
    fillTariff();
    if (isClosed())
    {
        createPlan();
        findUV();
        printUV();
        while (!planIsGood()) {
            findBadCell();
            findCycle();
            findMinAndBuildNewPlan();
            findUV();
            printUV();
        };
    };
    printPlan();
}
Exemplo n.º 8
0
int main()
{
	NODEPTR list = NULL;
	int baslangic, bitis;
	int graf[4][4] = {
		0, 1, 0, 1,
		1, 0, 1, 1,
		0, 1, 0, 1,
		1, 1, 1, 0
	};
	printf("Baslangic ve bitis dugumlerini giriniz.\nBaslangic :");
	scanf("%d", &baslangic);
	printf("Bitis     :");
	scanf("%d", &bitis);

	push(&list, baslangic);
	findCycle(list, graf, baslangic, bitis);

	printf("%d den %d dugumune gore olusacak yol : ", baslangic, bitis);
	for (NODEPTR p = list; p != NULL; p = p->next)
	{
		if (p->next == NULL)
			printf("%d", p->info);

		else
			printf("%d -> ", p->info);
	}





	getchar();
	getchar();
	return 0;
}