N-Damenproblem Hilfe



  • Hallo, ich brauche hilfe bei meinem code. Der folgende Algorithmus funktioniert, jeodch muss man ihn jederzeit mit einem 's' unterbrechen und mit 'e' das Programm beenden können. Den Befehl exit() und goto darf man leider nicht verwenden. Die Laufzeit wird berechnet, jedoch stimmt diese nicht genau.
    Die Anwendung ist als Konslenprogramm realisiert.

    //Auszug aus den verschiedenen Headerdateien:
    typedef struct board
    {
    	int iaArray[12];//Array for saving solution
    	int iLength;//Length of chessboard
    }board;
    
    typedef struct settings
    {
    	BOOL bSaveMode;
    	char caFileName[255];
    	BOOL bSingleStep;
    	int iSolutionCount;
    
    }settings;
    
    typedef struct Time
    {
    	clock_t tGetTime;
    	double dTime;
    }Time;
    
    #define TRUE 1//Values for BOOL
    #define FALSE 0
    #pragma warning(disable:4996)//Disable warning
    typedef int BOOL;//New type of value
    //_gotoxy() springt an einen Punkt x,y im Kommandofenster
    
    //Algorithmus:
    int NextQueen(int iRow, board* const bpsNewBoard, settings* const pssNewSettings, Time* ptTime1)
    {
    	BOOL bAbort = FALSE;
    
    	ptTime1->tGetTime = clock();//Get Current Time
    	if (iRow == bpsNewBoard->iLength)//Solution found!
    	{		
    		if (pssNewSettings->bSingleStep)
    		{
    			printBoard(bpsNewBoard);//If in SingleMode call print-function
    			printf("\n");
    		}
    
    		pssNewSettings->iSolutionCount++;//Increase SolutionCounter and print new total of solutions
    		_gotoxy(63, 23);
    		printf("%i", pssNewSettings->iSolutionCount);
    
    		ptTime1->dTime = ptTime1->dTime + difftime(clock(), ptTime1->tGetTime);//Get time needed for calculation
    		double dTimeInSeconds = ptTime1->dTime / CLOCKS_PER_SEC;
    		_gotoxy(39, 24);
    		printf("%.3lf", dTimeInSeconds);//print time
    
    		if (pssNewSettings->bSingleStep)
    		{
    			_getch();//Wait for user interaction in single mode
    		}
    
    	}
    	else
    	{
    		for (int iCurrentCol = 0; iCurrentCol < bpsNewBoard->iLength && !bAbort; iCurrentCol++)
    		{
    			if (NoAttack(iRow, iCurrentCol, bpsNewBoard))//Try to set queen
    			{
    				bpsNewBoard->iaArray[iRow] = iCurrentCol;//Save solution in array
    				NextQueen(iRow + 1, bpsNewBoard, pssNewSettings, ptTime1);
    			}
    		}
    	}
    	if (kbhit())
    	{
    		int iInput = _getch();
    		if (iInput == 'e')
    		{
    			bAbort = TRUE;
    
    			printf("bricht ab...");//Zum test, später rausmachen
    		}
    		if (iInput == 's')
    		{
    			printf("Algorithmus unterbrochen");//Zum test
    		}
    	}
    	return 0;
    }
    
    int NoAttack(int iRow, int iCol, const board* const bpsNewBoard)//Look for queens attacking
    {
    	BOOL bQueen = TRUE;//If Attack
    	int iCurrentRow = 0;
    	while ((iCurrentRow < iRow) && bQueen)
    	{
    		if ((iCol == bpsNewBoard->iaArray[iCurrentRow]) || (bpsNewBoard->iaArray[iCurrentRow] + iCurrentRow == iRow + iCol) || (bpsNewBoard->iaArray[iCurrentRow] - iCurrentRow == iCol - iRow))
    		{
    			bQueen = FALSE;//If no Attack
    		}
    		else
    		{
    			iCurrentRow++;
    		}
    	}
    	return bQueen;
    }
    

    Schon einmal Danke!



  • _getch() ist WinAPI, hat hier nichts zu suchen.
    Du hast keine Frage gestellt.
    Du kannst Codetags nichts benutzen. Viel Spaß im Subforum WinAPI.


Anmelden zum Antworten