Example #1
0
int main(int argc, char *argv[]){
    int i, j;

    /* File Input/Output Variables */
    FILE *output, *input;
    char* endFile = ".tour";
    char* outputFile;
    char* inputFile = argv[1];
    input = fopen(inputFile, "r");

    /* Creating .tour output file */
    int len = strlen(inputFile);
    char fileName[len];
    for (i=0; i< len; i++) {
        fileName[i] = inputFile[i];
	}
    outputFile = strcat(fileName, endFile);
    output = fopen(outputFile, "w");

    /* Copy Cities */
    int** cities;
    int lineCount;
    cities = buildArrays(input, &lineCount);

    // Debug Code - print cities
    /*for(i = 0; i < lineCount; i++) {
        for(j = 0; j < 3; j++) {
            printf("%d ", cities[i][j]);
        }
        printf("\n");
    }*/

    /* Create Cost Matrix */
    int** costM;
    costM = buildCostMatrix(cities, lineCount);

    // Debug Code - print Cost Matrix
    /*for(i = 0; i < lineCount; i++) {
        for(j = 0; j < lineCount; j++) {
            printf("%d ", costM[i][j]);
        }
        printf("\n");
    }*/

    /* Zero out rows and columns */
    subtractMinimum(costM, lineCount);

    /* To-do, put the following into function and figure what it all means */
    int flag[lineCount][lineCount];
    int nrz[lineCount];
    int ncz[lineCount];

    int nrz1[lineCount];
    int ncz1[lineCount];


    int min = INT_MAX;
    // a = minimum number of lines
    int a = 0, noz = 0;
    // Initializing all flag spots to 0
    for(i = 0; i < lineCount; i++) {
        for(j = 0; j < lineCount; j++) {
            flag[i][j] = 0;
        }
    }

    int cn;
    // Count and mark number of zeros in rows
    for(i = 0; i < lineCount; i++) {
        cn = 0;
        for(j = 0; j < lineCount; j++) {
            if(costM[i][j] == 0) {
                cn++;
                flag[i][j] = 1;
            }
        }
        // Record number of zeros in specific row
        nrz[i] = cn;
        // Record overall number of zeros
        noz = noz + cn;
    }

    // Count and mark number of zeros in columns
    for(i = 0; i < lineCount; i++) {
        cn = 0;
        for(j = 0; j < lineCount; j++) {
            if(costM[j][i] == 0) {
                cn++;
                flag[j][i] = 1;
            }
        }
        // Record number of zeros in specific column
        ncz[i] = cn;
        // Record overall number of zeros
        noz = noz + cn;
    }

    // Copy arrays recording number of zeros in rows and columns
    for(i = 0; i < lineCount; i++) {
        nrz1[i] = nrz[i];
        ncz1[i] = ncz[i];
    }

    int k = 0;
    // while there is row or column that has a zero in it
    while(nrz[k] != 0 || ncz[k] != 0) {

        for(i = 0; i < lineCount; i++) {
            // Count number of flagged spots in row, record in nrz
            cn = 0;
            for(j = 0; j < lineCount; j++) {
                if(flag[i][j] == 1)
                    cn++;
                nrz[i] = cn;
            }

            // If there is only 1 flag in row
            if(nrz[i] == 1) {
                for(j = 0; j < lineCount; j++) {
                    // Find the flag
                    if(flag[i][j] == 1) {
                        // Reassign it
                        flag[i][j] = 2;
                        // Find other flags in column
                        for(k = 0; k < lineCount; k++) {
                            // Reassign flag
                            if(flag[k][j] == 1) {
                                flag[k][j] = 0;
                            }
                        }
                    }
                }
            }
        }

        // Traverse through columns
        for(i = 0; i < lineCount; i++) {
            cn = 0;
            // Count number of flags in column
            for(j = 0; j < lineCount; j++) {
                if(flag[j][i] == 1) {
                    cn++;
                    ncz[i] = cn;
                }
            }
            // If there was only 1 flag in column
            if(ncz[i] == 1) {
                for(j = 0; j < lineCount; j++) {
                    // Search for that flag
                    if(flag[j][i] == 1) {
                        // Reassign it
                        flag[j][i] = 2;
                        // Find all other flags in row, reassign to 0
                        for(k = 0; k < lineCount; k++) {
                            if(flag[j][k] == 1) {
                                flag[j][k] = 0;
                            }
                        }
                    }
                }
            }
        }
        k++;
    }

    a = 0;
    for(i = 0; i < lineCount; i++) {
        for(j = 0; j < lineCount; j++) {
            if(flag[i][j] == 2) {
                a++;
            }
        }
    }

    if (a == lineCount) {
        fprintf(output, "%d\n", a);
        for(i = 0; i < lineCount; i++) {
            for(j = 0; j < lineCount; j++) {
                if(flag[i][j] == 2) {
                    fprintf(output, "%d\n", i);
                }
            }
        }
    }

    else {
        int rf[lineCount];
        int sr[lineCount];
        int cf[lineCount];
        int sc[lineCount];

        // Initialize all to zero
        for(i = 0; i < lineCount; i++) {
            rf[i] = 0;
            sr[i] = 0;
            cf[i] = 0;
            sc[i] = 0;
        }

        int m;
        for(k = lineCount; (k > 0 && noz != 0); k--) {
            for(i = 0; i < lineCount; i++) {
               m = 0;
                for(j = 0; j < lineCount; j++) {
                    if((flag[i][j] == 4)&&(costM[i][j] == 0))
                        m++;
                }
                sr[i] = m;
            }

            for(i = 0; i < lineCount; i++) {
                if(nrz1[i] == k && nrz1[i] != sr[i]) {
                    rf[i] = 1;
                    for(j = 0; j < lineCount; j++) {
                        if(costM[i][j] == 0)
                            flag[i][j] = 4;
                    }
                    noz = noz - k;
                }
            }
            int l;
            for(i = 0; i < lineCount; i++) {
                l = 0;
                for(j = 0; j < lineCount; j++) {
                    if((flag[j][i] == 4) && (costM[j][i] == 0))
                        l++;
                }
                sc[i] = l;
            }

            for(i = 0; i < lineCount; i++) {
                if(ncz1[i] == k && ncz1[i] != sc[i]) {
                    cf[i] = 1;
                    for(j = 0; j < lineCount; j++) {
                        if(costM[j][i] == 0)
                            flag[j][i] = 4;
                    }
                    noz = noz - k;
                }
            }

            for(i = 0; i < lineCount; i++) {
                for (j = 0; j < lineCount; j++) {
                    if(flag[i][j] != 3) {
                        if(rf[i] == 1 && cf[j] == 1) {
                            flag[i][j] = 3;
                            if(cost[i][j] == 0)
                                noz = noz + 1;
                        }
                    }
                }

            }
        }

        for(i = 0; i < lineCount; i++) {
            for(j = 0; j < lineCount; j++) {
                if(rf[i] != 1 && cf[j] != 1) {
                    if(min > costM[i][j])
                        min = cost[i][j];
                }
            }
        }

        for(i = 0; i < lineCount; i++) {
            for(j = 0; j < lineCount; j++) {
                if(rf[i] != 1 && cf[j] != 1)
                    cost[i][j] = cost[i][j] - min;
            }
        }

        for(i = 0; i < lineCount; i++) {
            for(j = 0; j < lineCount; j++) {
                if(flag[i][j] == 3)
                    cost[i][j] = cost[i][j] + min;
            }
        }
    }


    destroy(cities, costM, lineCount);
	close(output);
    close(input);
    return 0;
}
Example #2
0
/*
	render takes all of the arrays that are contained in the render class
	and builds the draw arrays that will be used to manage all of the
	graphics on the screen
*/
void renderer::render(void)
{
	bool check = false;
	if(buildOk) buildArrays();

	//Enable clientStates so that the drawArrays has
	//the correct array information
	//Vertex is for the spacial information
	//Color is for the shading of the object (when no texture is
	//available the color is very visible
	//Texture is for the image information
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	
	//Image Data for character Texture is not
	//Actually set up in the system, this needs
	//To be investigated
	glEnable(GL_TEXTURE_2D);

	tileData -> setupTexture();
	tileData -> enableSetUp();

	glVertexPointer(2, GL_INT, 0, tileArray);
	glColorPointer(3, GL_DOUBLE, 0, tileColors);
	glTexCoordPointer(2, GL_DOUBLE, 0, tileData -> textureArray);
	glDrawArrays(GL_TRIANGLES, 0, tempVertices.size());
	tileData -> disableSetUp();

	glClear(GL_DEPTH_BUFFER_BIT);
	playerData -> setupTexture();
	playerData -> enableSetUp();

	glVertexPointer(2, GL_INT, 0, *playerArray);
	glColorPointer(3, GL_DOUBLE, 0, *playerColors);
	glTexCoordPointer(2, GL_DOUBLE, 0, playerData -> textureArray);
	glDrawArrays(GL_TRIANGLES, 0, 6);
	playerData -> disableSetUp();

	glClear(GL_DEPTH_BUFFER_BIT);
	for(unsigned int i = 0; i < actorArrays.size(); i++)
	{
		if( i == 0 || (i > 0 && characterData[i].getImageName() != characterData[i-1].getImageName()))
		{
			characterData[i].setupTexture();
			characterData[i].enableSetUp();
			check = true;
		}
		glVertexPointer(2, GL_INT, 0, *actorArrays.at(i));
		glColorPointer(3, GL_DOUBLE, 0, *actorColors.at(i));
		glTexCoordPointer(2, GL_DOUBLE, 0, characterData[i].textureArray);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		if(check) 
		{
			characterData[i].disableSetUp();
			check = false;
		}
		glClear(GL_DEPTH_BUFFER_BIT);
	}

	glDisable(GL_TEXTURE_2D);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}