Last active
August 29, 2015 14:12
-
-
Save descilla/70df9aeeaa501c660be6 to your computer and use it in GitHub Desktop.
Springerproblem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// knightrider | |
// | |
// Created by Simon W. on 04.01.15. | |
// Copyright (c) 2015 Simon W. & Andy S. All rights reserved. | |
// | |
int springer( int zeile, int spalte, int num ); | |
#include <stdio.h> | |
#include <time.h> | |
#define N 8 | |
#define MAX N*N-1 | |
//int n = 8; | |
int board[N][N] = { 0 }; //erreichte Felder | |
//int moves[8][2] = { {-2,-1}, {-1,-2}, {-2,1}, {-1,2}, {2,-1}, {1,-2}, {2,1}, {1,2}}; //moegliche Schritte #1 | |
//int moves[8][2] = { {-2,1}, {-2,-1}, {-1,2}, {-1,-2}, {2,1}, {2,-1}, {1,2}, {1,-2}}; //moegliche Schritte #2 | |
//int moves[8][2] = { {-1,2}, {-1,-2}, {-2,1}, {-2,-1}, {2,1}, {2,-1}, {1,2}, {1,-2}}; //moegliche Schritte #3 | |
int moves[8][2] = {{2,1}, {2,-1}, {-1,2}, {-1,-2}, {-2,1}, {-2,-1}, {1,2}, {1,-2}}; //moegliche Schritte #4 | |
//int it = 0; | |
int main(int argc, const char * argv[]) { | |
//Starzeit | |
clock_t prgstart, prgende; | |
prgstart=clock(); | |
//Initial Step | |
//Rekursion starten | |
springer(0, 0, 1); | |
printf("%d ok: %d,%d\n", 0,0,0); | |
//Endzeit | |
prgende=clock(); | |
printf("Laufzeit %.5f Sekunden\n",(float)(prgende-prgstart) / CLOCKS_PER_SEC); | |
//printf("It: %d", it); | |
return 0; | |
} | |
int springer( int zeile, int spalte, int num ){ | |
//++it; | |
int nzeile, nspalte, i; | |
//Platz belegen | |
board[zeile][spalte] = num; | |
//Alle 8 Moeglichen Zuege durchprobieren | |
for(i = 0; i < 8; i++){ | |
//++it; | |
//Neue Position | |
nzeile = zeile + moves[i][0]; | |
nspalte = spalte + moves[i][1]; | |
//Ist Zug ist im Spielfeld | |
if(nzeile >= 0 && nzeile < N && nspalte >= 0 && nspalte < N){ | |
//Platz ist frei? | |
if(!board[nzeile][nspalte]){ | |
//Ende oder Rekursion | |
if(num == MAX || springer(nzeile, nspalte, num+1)){ | |
printf("%d ok: %d,%d\n", num, nzeile,nspalte); | |
return 1; | |
} | |
} | |
} | |
} | |
// Freigeben, falls Nachfolgende Schritte nicht moeglich | |
board[zeile][spalte] = 0; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment