The common red and black of the winter vacation brush problem

Source: Internet
Author: User

Description-Topic description

There is a rectangular room full of square tiles, each tile color is not red or black. Someone on a piece of brick, he can move to the adjacent four pieces of brick. But he can only walk black bricks, not red bricks.

Hit a program to get a count of how many blocks of red bricks you can walk on.

Input-Enter

Multiple sets of test cases. Each set of arrays begins with two positive integers w and h;w and H, respectively, representing the number of tiles in X-and Y-directions. W and W are not more than 20.

There are also h rows of data, each containing a W character. Each character represents a colored tile as follows.

'. '-a piece of black brick
' # '-a piece of red brick
' @ '-a man on a black brick (a group of data one person)
The input ends with a line of two zeros.

Output-Outputs

For each set of test cases, output the number of tiles (including the starting brick) that he can reach from the starting brick.

Sample Input-Enter sample

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

Sample output-export example

4559613

Analysis:
Feel this series of topics are compared to water a little, but for the introduction of the search, or will be more suitable is, idle to have no matter to write the problem.
For BFS and DFS, the feeling BFS is generally used to solve the most-valued problem, and then Dfs simply confirms the answer to a question or whether a request is reachable.
This question is simply asked to reach the number of bricks, so the use of Dfs.

The input map is traversed to find the current position, then set as the starting point, set up an array of directions to determine the direction of the forward, that is, up and down,
The result of marking the passing position and then traversing the entire map is the answer.


Code:
  

#include <stdio.h>
#include <string.h>

int n, m, ans=0;
Char map[25][25];
BOOL ISVISITED[25][25];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

BOOL IsIn (int x, int y) {

if (x<0 | | x>=n | | y<0 | | y>=m | | isvisited[x][y] = = True | | map[x][y] = = ' # ')
return false;
return true;
}

void doIt (int x, int y) {

Isvisited[x][y] = true;
ans++;
}

void DFS (int x, int y) {

for (int i=0;i<4;i++)
if (IsIn (x+dir[i][0], y+dir[i][1])) {
DoIt (X+dir[i][0], y+dir[i][1]);
DFS (X+dir[i][0], y+dir[i][1]);
}
}

int main () {

while (scanf ("%d%d", &m, &n) = = 2) {
if (n==0 && m==0) break;
int x, y;
for (int i=0; i<n; i++) {
scanf ("%s", Map[i]);
for (int j=0; j<m; j + +)
if (map[i][j] = = ' @ ') {
X=i;
Y=j;
}
}
memset (isvisited,false,sizeof (isvisited));
ans = 1;
Isvisited[x][y] = true;
DFS (x, y);
printf ("%d\n", ans);
}
return 0;
}

The common red and black of the winter vacation brush problem

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.