码迷,mamicode.com
首页 > 其他好文 > 详细

Red and Black

时间:2014-05-25 19:36:58      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

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

 

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

Red and Black,布布扣,bubuko.com

Red and Black

标签:des   style   class   blog   c   code   

原文地址:http://www.cnblogs.com/LWF5201314614/p/3750333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!