Beispiel #1
0
int main()
{	
	/*int arr[] = {
		1,   2,  3,  4,
		5,   6,  7,  8, 
		9,  10, 11,  0,
		13, 14, 15,  12
	};*/
	int i;
	NODE node;
	for(i = 1; i <=16; i++){
		scanf("%d",&node.arr[i]);
		if( node.arr[i] == 0 )
			node.spos = i;
	}
		
	//node.spos 	=  4;
	node.depth	=  0;
	node.gvalue     = -1;
	node.no		= -1;
	node.paddr 	= 0;
	node.dir	= -10;

 	int spos, r, c, x;
	spos = node.spos-1;
        r = spos/4;
        c = spos%4+1;
        x = (r+c)%2;
	
	findGoalvalue(&node);
//	if( node.valid == -1 )
//		exit(0);
	INODE = node;
	insertNode(node, 0);
	solveFifteen();
	
}		
Beispiel #2
0
int main(int argc, char *argv[])
{
    if (argc < 2)
    {
             puts("ERR#1: Missing argument!");
             return MISSING_PARAMETER;
    }
        
    /* copy argv[1] to variable initState */
    char initState[strlen(argv[1])];
    strcpy(initState, argv[1]);
    
    int i, j;
    int length = strlen(initState);
    struct GameState state;
    
    /* count rows */
    rows = getRowsCount(initState);
    
    if (rows == -1)
    {
        printf("ERR#2: Malformed input!");
        return MALFORMED_INPUT;
    }
    
    /* there is one less semicolon than rows */
    rows++;
    if (rows < MINIMUM_ROWS)
    {
        printf("ERR#3: Field too small!");
        return FIELD_TOO_SMALL;
    }
        
    /* allocation of memory for initial state of game */
    state.tilesPosition = (int **)malloc(rows * sizeof(int *));
    if (state.tilesPosition == NULL)
    {
        printf("ERR#5: Out of memory!");
        return OUT_OF_MEMORY;
    }
    for (i = 0; i < rows; i++)
    {
        state.tilesPosition[i] = (int *)malloc(rows * sizeof(int *));
        if (state.tilesPosition[i] == NULL)
        {
            printf("ERR#5: Out of memory!");
            return OUT_OF_MEMORY;
        }
        for (j = 0; j < rows; j++)
        {
            state.tilesPosition[i][j] = 0;    
        } 
    }
    
    int usedNumbers[rows * rows];
    for (i = 0; i < rows * rows; i++)
    {
        usedNumbers[i] = 0;
    }
    
    rows = 0;
    int cols = 0;
    int prevCols = 0;
    int number = 0;
    for (i = 0; i < length; i++)
    {
        if (initState[i] == ';')
        {
            /* if count of columns doesn't match, input format isn't right */
            if (cols != prevCols && prevCols != 0)
            {
                printf("ERR#2: Malformed input!");
                return MALFORMED_INPUT;
            }
            state.tilesPosition[rows][cols] = number;
            usedNumbers[number] = 1;
            
            /* init for next row */
            rows++;
            prevCols = cols;
            cols = 0;
            number = 0;
            
            /* if next character after semicolon is space, jump after it */
            if (initState[i + 1] == ' ')
            {
                i += 2;    
            }
        }
        if (initState[i] == ' ')
        {
            state.tilesPosition[rows][cols] = number;
            usedNumbers[number] = 1;
            cols++;    
            number = 0;
        }
        if (isdigit(initState[i]))
        {
            number = number * 10 + (initState[i] - 48);
        }
    }
    state.tilesPosition[rows][cols] = number;
    usedNumbers[number] = 1;
    
    rows++;
    
    for (i = 0; i < rows * rows; i++)
    {
        if (usedNumbers[i] == 0)
        {
            printf("ERR#2: Malformed input!");
            return MALFORMED_INPUT;
        }
    }
    
    int isSolvable = isSolvableState(&state);
    if (isSolvable == 0)
    {
        printf("ERR#4: Non-existent solution!");
        return NONEXISTENT_SOLUTION;
    }
    
    /* init of first element in priority queue */
    state.manhattanDistance = getManhattanDistance(state);
    state.distance = 0;
    state.next = (struct GameState *) malloc(sizeof(struct GameState *));
    if (state.next == NULL)
    {
        printf("ERR#5: Out of memory!");
        return OUT_OF_MEMORY;
    }
    state.prev = (struct GameState *) malloc(sizeof(struct GameState *));
    if (state.prev == NULL)
    {
        printf("ERR#5: Out of memory!");
        return OUT_OF_MEMORY;
    }
    state.next = NULL;
    state.prev = NULL;
    
    /* insert first element to priority queue */
    insertPQ(&state);
    
    int solved = solveFifteen();
    
    if (solved == OUT_OF_MEMORY)
    {
        printf("ERR#5: Out of memory!");
        return OUT_OF_MEMORY;
    }
        
    /* system("PAUSE"); */
    return 0;
}