您的位置:首页 > 编程语言

PKU ACM 1164 源代码

2011-01-05 18:21 183 查看
图的深度优先搜索DFS简单应用.

注:输入数据后再处理耗时0ms,而边输入边处理因为破坏了流水线作业导致耗时16ms。



源代码:

/*The Castle
Time Limit: 1000MS  Memory Limit: 10000K
Total Submissions: 4347  Accepted: 2450

Description

1   2   3   4   5   6   7
#############################
1 #   |   #   |   #   |   |   #
#####---#####---#---#####---#
2 #   #   |   #   #   #   #   #
#---#####---#####---#####---#
3 #   |   |   #   #   #   #   #
#---#########---#####---#---#
4 #   #   |   |   |   |   #   #
#############################
(Figure 1)

#  = Wall
|  = No wall
-  = No wall

Figure 1 shows the map of a castle.Write a program that calculates
1. how many rooms the castle has
2. how big the largest room is
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.

Input

Your program is to read from standard input. The first line contains the number of modules in the north-south direction
and the number of modules in the east-west direction. In the following lines each module is described by a number
(0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east),
8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall
to the north in module 2,1. The castle always has at least two rooms.
Output

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).
Sample Input

4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
Sample Output

5
9
*/

#include "stdlib.h"
#include "stdio.h"

#define MAX_MODULES_NUMBER 50
#define FOUR_DIRECTION 4
#define DIRECTION_WEST 1
#define DIRECTION_NORTH 2
#define DIRECTION_EAST 3
#define DIRECTION_SOUTH 4

#define COLOR_WHITE 0
#define COLOR_GRAY  1
#define COLOR_BLACK 2

typedef struct _CASTLE_STRUCT_
{
#define NO_WALL 0
#define WALL_EXIT 1
unsigned char ucWestWall;
unsigned char ucEastWall;
unsigned char ucNorthWall;
unsigned char ucSouthWall;
}CASTLE_STRUCT;

CASTLE_STRUCT gastCastle[MAX_MODULES_NUMBER][MAX_MODULES_NUMBER];
unsigned char gaucColor[MAX_MODULES_NUMBER][MAX_MODULES_NUMBER];
unsigned char gaucInput[MAX_MODULES_NUMBER][MAX_MODULES_NUMBER];

void DFS(int viNorthsouthIndex,int viEastwestIndex,int *vpiRoomSize)
{
int  iLoopDirection;

(*vpiRoomSize)++;

gaucColor[viNorthsouthIndex][viEastwestIndex] = COLOR_GRAY;

for(iLoopDirection = 1; iLoopDirection <= FOUR_DIRECTION; iLoopDirection++)
{

if(DIRECTION_WEST == iLoopDirection)
{
if(0 < viEastwestIndex)
{
if(NO_WALL == gastCastle[viNorthsouthIndex][viEastwestIndex].ucWestWall && COLOR_WHITE == gaucColor[viNorthsouthIndex][viEastwestIndex-1])
{
DFS(viNorthsouthIndex,viEastwestIndex-1,vpiRoomSize);
}
}
}
else if(DIRECTION_NORTH == iLoopDirection)
{
if(0 < viNorthsouthIndex)
{
if(NO_WALL == gastCastle[viNorthsouthIndex][viEastwestIndex].ucNorthWall && COLOR_WHITE == gaucColor[viNorthsouthIndex-1][viEastwestIndex])
{
DFS(viNorthsouthIndex-1,viEastwestIndex,vpiRoomSize);
}
}
}
else if(DIRECTION_EAST == iLoopDirection)
{
if(MAX_MODULES_NUMBER-1 > viEastwestIndex)
{
if(NO_WALL == gastCastle[viNorthsouthIndex][viEastwestIndex].ucEastWall && COLOR_WHITE == gaucColor[viNorthsouthIndex][viEastwestIndex+1])
{
DFS(viNorthsouthIndex,viEastwestIndex+1,vpiRoomSize);
}
}
}
else if(DIRECTION_SOUTH == iLoopDirection)
{
if(MAX_MODULES_NUMBER-1 > viNorthsouthIndex)
{
if(NO_WALL == gastCastle[viNorthsouthIndex][viEastwestIndex].ucSouthWall && COLOR_WHITE == gaucColor[viNorthsouthIndex+1][viEastwestIndex])
{
DFS(viNorthsouthIndex+1,viEastwestIndex,vpiRoomSize);
}
}
}
}

gaucColor[viNorthsouthIndex][viEastwestIndex] = COLOR_BLACK;

return;
}

void main(void)/*TheCastle*/
{
int  iNorthsouthModuleNumber = 0;
int  iEastwestModuleNumber = 0;
int  iLoopNorthsouth;
int  iLoopEastwest;
int  iLoopDirection;
int  iInput;
int  iRoomNum = 0;
int  iRoomSize = 0;
int  iLargestRoomSize = 0;

memset(gastCastle,NO_WALL,MAX_MODULES_NUMBER*MAX_MODULES_NUMBER*sizeof(CASTLE_STRUCT));
memset(gaucColor,COLOR_WHITE,MAX_MODULES_NUMBER*MAX_MODULES_NUMBER*sizeof(unsigned char));

scanf("%d",&iNorthsouthModuleNumber);
scanf("%d",&iEastwestModuleNumber);

for(iLoopNorthsouth = 0; iLoopNorthsouth < iNorthsouthModuleNumber; iLoopNorthsouth++)
{
for(iLoopEastwest = 0; iLoopEastwest < iEastwestModuleNumber; iLoopEastwest++)
{
scanf("%d",&gaucInput[iLoopNorthsouth][iLoopEastwest]);
}
}

for(iLoopNorthsouth = 0; iLoopNorthsouth < iNorthsouthModuleNumber; iLoopNorthsouth++)
{
for(iLoopEastwest = 0; iLoopEastwest < iEastwestModuleNumber; iLoopEastwest++)
{

for(iLoopDirection = 1; iLoopDirection <= FOUR_DIRECTION; iLoopDirection++)
{
if(1 == gaucInput[iLoopNorthsouth][iLoopEastwest]%2)
{
if(DIRECTION_WEST == iLoopDirection)
{

gastCastle[iLoopNorthsouth][iLoopEastwest].ucWestWall = WALL_EXIT;

}
else if(DIRECTION_NORTH == iLoopDirection)
{

gastCastle[iLoopNorthsouth][iLoopEastwest].ucNorthWall = WALL_EXIT;

}
else if(DIRECTION_EAST == iLoopDirection)
{

gastCastle[iLoopNorthsouth][iLoopEastwest].ucEastWall = WALL_EXIT;

}
else if(DIRECTION_SOUTH == iLoopDirection)
{

gastCastle[iLoopNorthsouth][iLoopEastwest].ucSouthWall = WALL_EXIT;

}
}

gaucInput[iLoopNorthsouth][iLoopEastwest] = gaucInput[iLoopNorthsouth][iLoopEastwest]>>1;
}

}
}

for(iLoopNorthsouth = 0; iLoopNorthsouth < iNorthsouthModuleNumber; iLoopNorthsouth++)
{
for(iLoopEastwest = 0; iLoopEastwest < iEastwestModuleNumber; iLoopEastwest++)
{
if(COLOR_WHITE == gaucColor[iLoopNorthsouth][iLoopEastwest])
{
iRoomNum++;
iRoomSize = 0;
DFS(iLoopNorthsouth,iLoopEastwest,&iRoomSize);
iLargestRoomSize = iLargestRoomSize>iRoomSize?iLargestRoomSize:iRoomSize;
}
}
}

printf("%d/n",iRoomNum);
printf("%d/n",iLargestRoomSize);

return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: