Zoj 2165 red and black

Source: Internet
Author: User

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 integersWAndH;WAndHAre the numbers of tiles inX-AndY-Directions, respectively.WAndHAre not more than 20.

There areHMore lines in the data set, each of which limit desWCharacters. 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 shocould output a line which contains the number of tiles he can reach from the initial tile (including itself ).


Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
. #. # [Email protected] #. #.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7
..#.#..
..#.#..
###.###
[Email protected]
###.###
..#.#..
..#.#..
0 0


Sample output

45
59
6
13

 

Question:

Two numbers m and n are given, representing n rows of M columns, which cannot exceed 20. Then, the diagram of N rows of M columns includes '. ',' # ',' @ ', 3 characters.

@ Indicates the initial position, '.' indicates the path, '#' indicates the wall, and calculates the maximum number of '.' to be reached. '@' is

 

BFS:

That is to say, the maximum connected block problem does not have to be solved using the same one-dimensional array of the queue.

 1 #include<stdio.h> 2 #include<string.h> 3 struct node 4 { 5    int x,y; 6 }q[410]; 7  8 struct node P, N; 9 int flag[25][25];10 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};11 char str[25][25];12 13 int main()14 {15      int c, r, i, j, front, rear;16      while(scanf("%d%d",&c,&r)!=EOF, c + r){17         memset(flag, 0, sizeof(flag));18         for(i = 0; i < r; i++)19             scanf("%s", str[i]);20 21         for(i = 0; i < r; i++){22             for(j=0;j<c;j++)23                 if(str[i][j] == ‘@‘) break;24             if(str[i][j] == ‘@‘) break;25         }26         N.x = i;27         N.y = j;28         flag[i][j] = 1;29         q[0] = N;30         front = 0;31         rear = 1;32 33         while(front < rear){34             N = q[front++];35             for(i = 0; i < 4; i++){36                 int tx = N.x + dir[i][0];37                 int ty = N.y + dir[i][1];38                 if(tx >= 0 && tx < r && ty >= 0&& ty < c && flag[tx][ty] == 0 && str[tx][ty] == ‘.‘){39                     P.x = tx;40                     P.y = ty;41                     q[rear++] = P;42                     flag[tx][ty] = 1;43                 }44             }45         }46         printf("%d\n",rear);47      }48      return 0;49 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.