1504061021-hd-red and Black

Source: Internet
Author: User

Red and BlackTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 11542 Accepted Submission (s): 7185

problem Descriptionthere is a rectangular and covered with square tiles. Each tile is colored either red or black. A man was standing on a black tile. From a tiles, he can move to one of the four adjacent tiles. But he can ' t move on red tiles, and 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.
 
InputThe input consists of multiple data sets. A data set starts with a line containing the positive integers W and H; W and H is the numbers of tiles in the X-and y-directions, respectively. W and H is not more than 20.

There is H more lines in the data set, and each of the 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)
 
OutputFor 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.#..........#.#######. #.#.....#.. #.#.###.#.. #.#[email protected]#.#. #.#####.#.. #.......#.. #########............ 11 6..#. #.. #....#.. #.. #....#.. #.. ###.. #.. #.. #@...#.. #.. #....#.. #.. #.. 7 7..#.#....#.#. ###.###[email protected]###.###. #.#....#.#.. 0 0
 
Sample Output
4559613
Thinking of solving problemsThis is a most basic deep search, but also a friend taught me, before are not understand. deep Search is from a point to the upper and lower left and right four directions to expand, and then to expand the point of the base to the upper and lower left and right to expand judgment. so it takes two arrays, bianx[4] and biany[4] to store the corresponding changes of x and Y, so be sure to look at the variation of X and Y. and then you need to judge the location, that is, within the range 1<=x<=n&&1<=y<=mProblem Solving Code
#include <cstdio> #include <cstring> #include <iostream>//using namespace std;//c++ to add these two header files char map[ 22][22];int bx[5]={0,1,0,-1};int by[5]={1,0,-1,0};int sum;int n,m;bool judge (int a,int b) {if (a<1| | a>m| | b<1| | B>n) return    false;else{if (map[a][b]!= ' # ')    return true;else    return false;}} void Dfs (int a,int b) {int i;int nowa,nowb;sum++;map[a][b]= ' # '; for (i=0;i<4;i++) {//fixed-point four changes up or down nowa=a+bx[i];nowb= B+by[i];if (judge (NOWA,NOWB))    Dfs (NOWA,NOWB);//Direct recursive call is good else    continue;}} int main () {//int n,m;int i,j,k;int stax,stay;while (scanf ("%d%d", &n,&m), n+m) {memset (map,0,sizeof (map)); for ( i=1;i<=m;i++) for    (j=1;j<=n;j++)    {    cin>>map[i][j];    if (map[i][j]== ' @ ')    {    stax=i;    stay=j;    }    } Sum=0;dfs (Stax,stay); cout<<sum<<endl;//If the output requires a newline, add <<endl}return 0;}


1504061021-hd-red and Black

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.