USACO 2.1 The Castle

#include <stdio.h>
#define DEBUG 1
#define TESTCASES 8



int numOfRows, numOfColumns;
int wall[50][50][4];
int numOfRooms;
int roomNumArray[50][50];
int roomSize[2501];
int directionArray[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};

void printRoomNumArray(){
	int i, j;
	for (i = 0; i < numOfRows; i++){
		for (j = 0; j < numOfColumns; j++)
			printf("%d ", roomNumArray[i][j]);
		printf("\n");
	}
	printf("\n");
}

void numberRoom(int row, int column, int roomNum){
	//printRoomNumArray();
	if (roomNumArray[row][column] != 0)
		return;
	roomNumArray[row][column] = roomNum;
	roomSize[roomNum]++;
	int directionIndex;
	for (directionIndex = 0; directionIndex < 4; directionIndex++){
		int x = row + directionArray[directionIndex][0];
		int y = column + directionArray[directionIndex][1];
		if (x >= 0 && x < numOfRows && y >= 0 && y < numOfColumns && wall[row][column][directionIndex] != 1 && roomNumArray[x][y] == 0)
			numberRoom(x, y, roomNum);
	}
}
	
int main(){
#if DEBUG
	int testCase;
	for (testCase = 1; testCase <= TESTCASES; testCase++){
		char inputFileName[20] = "inputx.txt";
		inputFileName[5] = '1' +  (testCase - 1);
		freopen(inputFileName, "r", stdin);
		printf("\n#%d\n", testCase);
#endif

	scanf("%d%d", &numOfColumns, &numOfRows);
	int row, column;
	for (row = 0; row < numOfRows; row++)
		for (column = 0; column < numOfColumns; column++){
			int num;
			scanf("%d", &num);
			int direction;
			for (direction = 0; direction < 4; direction++)
				wall[row][column][direction] = (num >> direction) & 1;		
		}

	int roomNum = 1;
	for (row = 0; row < numOfRows; row++)
		for (column = 0; column < numOfColumns; column++){
			roomSize[roomNum++] = 0;
			roomNumArray[row][column] = 0;
		}

	int maxRoomSize = 0;
	roomNum = 0;
	for (row = 0; row < numOfRows; row++)
		for (column = 0; column < numOfColumns; column++)
			if (roomNumArray[row][column] == 0){
				numberRoom(row, column, ++roomNum);
				if (roomSize[roomNum] > maxRoomSize)
					maxRoomSize = roomSize[roomNum] ;
			}
		

	printf("%d\n%d\n", roomNum, maxRoomSize);

	int roomSizeCreated = 0;
	int maxRoomSizeCreated = 0;
	int rowToRemove = -1, columnToRemove = -1;
	char directionOfWallRemoved = '0';
	for (column = 0; column < numOfColumns; column++)
		for (row = numOfRows - 1; row >= 0; row--){
			int currentRoomNum =  roomNumArray[row][column];

			int eastColumn;
			if ((eastColumn = column + 1) < numOfColumns){
				int eastRoomNum = roomNumArray[row][eastColumn];
				if (currentRoomNum != eastRoomNum)
					if ( (roomSizeCreated = roomSize[eastRoomNum] + roomSize[currentRoomNum]) > maxRoomSizeCreated){
						maxRoomSizeCreated = roomSizeCreated;
						rowToRemove = row;
						columnToRemove = column;
						directionOfWallRemoved = 'E';
					}
						
			}
			int northRow;
			if ((northRow = row - 1) >= 0){
				int northRoomNum = roomNumArray[northRow][column];
				if (northRoomNum != currentRoomNum)
					if ( (roomSizeCreated = roomSize[northRoomNum] + roomSize[currentRoomNum]) > maxRoomSizeCreated){
						maxRoomSizeCreated = roomSizeCreated;
						rowToRemove = row;
						columnToRemove = column;
						directionOfWallRemoved = 'N';
					}
			}
		}

	printf("%d\n%d %d %c\n", maxRoomSizeCreated, rowToRemove + 1, columnToRemove + 1, directionOfWallRemoved);

#if DEBUG
	}
#endif
	return 0;
}

你可能感兴趣的:(USACO,DFS,Castle,the,2.1,连通分量)