Red and Black


Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 13

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem 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. 

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) 

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

Source

Asia 2004, Ehime (Japan), Japan Domestic
原本代码:2014.5.24
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 int D,SIGN,Len_X,Len_Y;
 5 char Map[100][100];
 6 int Sign[100][100];
 7 
 8 int Sign_Part(int x,int y)
 9 {
10 
11     int ii,jj;
12     if(x>=Len_X||x<0||y>=Len_Y||y<0)return 0;
13     if(Map[x][y]=='#')return 0;
14     else Map[x][y]='#';
15     /*putchar('\n');
16        for(ii=0;ii<Len_X;ii++)
17         {
18             for(jj=0;jj<Len_Y;jj++)
19                 printf("%c",Map[ii][jj]);
20             putchar('\n');
21         }*/
22     Sign[x][y]=1;
23     SIGN++;
24     if(Map[x+1][y]!='#'&&x+1<Len_X&&Sign[x+1][y]!=1)
25     {
26         Sign_Part(x+1,y);
27         Map[x+1][y]='.';
28     }
29     if(Map[x-1][y]!='#'&&x-1>=0&&Sign[x-1][y]!=1)
30     {
31         Sign_Part(x-1,y);
32         Map[x-1][y]='.';
33     }
34     if(Map[x][y+1]!='#'&&y+1<Len_Y&&Sign[x][y+1]!=1)
35     {
36         Sign_Part(x,y+1);
37         Map[x][y+1]='.';
38     }
39     if(Map[x][y-1]!='#'&&y-1>=0&&Sign[x][y-1]!=1)
40     {
41         Sign_Part(x,y-1);
42         Map[x][y-1]='.';
43     }
44     return 0;
45 
46 }
47 
48 int main()
49 {
50     int i,j,Begin_x,Begin_y;
51     while(scanf("%d%d",&Len_Y,&Len_X)!=EOF)
52     {
53         if(Len_X==0&&Len_Y==0)break;
54         for(i=0;i<Len_X;i++)
55         {
56             getchar();
57             for(j=0;j<Len_Y;j++)
58             {
59                 scanf("%c",&Map[i][j]);
60                 if(Map[i][j]=='@')
61                 {Begin_x=i;Begin_y=j;}
62             }
63         }
64         SIGN=0;
65         memset(Sign,0,sizeof(Sign));
66         Sign_Part(Begin_x,Begin_y);
67         printf("%d\n",SIGN);
68 
69     }
70 
71     return 0;
72 }
View Code

 

修改:2015.5.8
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 using namespace std;
 5 int D,SIGN,Len_X,Len_Y;
 6 char Map[100][100];
 7 void BFS(int x,int y)
 8 {
 9     if(Map[x][y]=='#')return ;
10     else
11     {
12         SIGN++;
13         Map[x][y]='#';
14         BFS(x-1,y);
15         BFS(x+1,y);
16         BFS(x,y-1);
17         BFS(x,y+1);
18     }
19 }
20 
21 int main()
22 {
23     int i,j,Begin_x,Begin_y;
24     while(scanf("%d%d",&Len_Y,&Len_X)!=EOF)
25     {
26         if(Len_X==0&&Len_Y==0)break;
27         for(i=0;i<=Len_X+1;i++)
28         {
29             for(j=0;j<=Len_Y+1;j++)
30             {
31                 if(i==0||j==0||i==Len_X+1||j==Len_Y+1)Map[i][j]='#';
32                 else
33                 {
34                     scanf(" %c",&Map[i][j]);
35                     if(Map[i][j]=='@')
36                     {Begin_x=i;Begin_y=j;}
37                 }
38 
39             }
40         }
41         SIGN=0;
42         BFS(Begin_x,Begin_y);
43         printf("%d\n",SIGN);
44 
45     }
46     return 0;
47 }
View Code

 

posted @ 2014-05-24 20:20  Wurq  阅读(214)  评论(0编辑  收藏  举报