一、题目信息
Red and Black
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17456 | Accepted: 9198 |
Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13二、算法分析
本题要求包含@的连通区域,跟图形学中种子填充算法类似,采用堆栈,将@所在位置作为种子,依次填充该种子前后左右,直到堆栈为空。
三、参考代码
(采用GCC提交的话,不要包含<malloc.h><memory.h>等头文件)
#include <stdio.h>
#include <string.h>
//#include <malloc.h>
struct dirNode
{
unsigned x;
unsigned y;
struct dirNode * next;
};
struct stack
{
struct dirNode *top;
};
struct stack * Stack_init()
{
struct stack *p;
p = (struct stack *)malloc(sizeof(struct stack));
if(p != NULL)
p->top = NULL;
return p;
}
struct stack * Stack_insert(struct stack *s,int x,int y)
{
struct dirNode *p;
p = (struct dirNode *)malloc(sizeof(struct dirNode));
if(p != NULL)
{
p->x = x;
p->y = y;
p->next = NULL;
}
p->next = s->top;
s->top = p;
return s;
}
struct stack * Stack_pop(struct stack *s)
{
struct dirNode *p;
if (s == NULL || s->top == NULL)
return s;
p = s->top;
s->top = p->next;
free(p);
return s;
}
int main()
{
int W,H,i,j,room[20][20],res = 0,ii,jj,tmpi,tmpj;
short cal[4][2] = {-1,0,0,+1,+1,0,0,-1};
char tmp;
struct dirNode *stack = NULL;
while(scanf("%d%d",&W,&H) && W && H)
{
res = 0;
struct stack *s = Stack_init();
getchar();
for( i = 0 ; i < H ;i++)
{
for( j = 0 ; j < W; )
{
tmp = getchar();
room[i][j] = (tmp == '#') ? 0 : 1;
if (tmp == '@')
{
s = Stack_insert(s,i,j);
room[i][j] = 0;
res += 1;
}
j++;
}
getchar();
}
while(s->top!= NULL)
{
ii = s->top->x;
jj = s->top->y;
s = Stack_pop(s);
for(i = 0;i < 4 ;i++)
{
tmpi = ii + cal[i][0];
tmpj = jj + cal[i][1];
if( (tmpi < H)&&(tmpi >=0) &&(tmpj < W) && (tmpj >=0 ))
{
if(room[tmpi][tmpj] == 1)
{
room[tmpi][tmpj] = 0;
s = Stack_insert(s,tmpi,tmpj);
res++;
}
}
}
}
printf("%d\n",res);
}
//system("pause");
return 0;
}