Challenge Program 2.1.4 Exhaustion Search >> Depth First search exercises POJ1979 black and red

Source: Internet
Author: User

http://poj.org/problem?id=1979

Description

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

Input

The 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)
The end of the input is indicated by a line consisting of the zeros.

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).Ideas:(⊙v⊙) Well, the same as the example, from the @, search all the places you can go to, change it to a value (@ or what is true), and finally go through the map.
1#include <stdio.h>2 Charch[ -][ -];3 voidSolveintHintW);4 voidFind1 (intHintW);5 voidDfsintXintYintHintW);6 intMain ()7 {8     inth,w,i;9      while(SCANF ("%d%d", &w,&h)!=eof&&h!=0&&w!=0)Ten     { One GetChar (); A       for(i=0; i) - gets (Ch[i]); - Find1 (h,w); the solve (h,w); -     } -     return 0; - } +  - voidSolveintHintW) + { A     intA=0; at      for(intI=0; i) -          for(intk=0; k<w;k++) -             if(ch[i][k]=='@') -a++; -printf"%d\n", a); - } in  - voidFind1 (intHintW) to { +      for(intI=0; i) -          for(intk=0; k<w;k++) the             if(ch[i][k]=='@') *                 { $ DFS (I,K,H,W);Panax Notoginseng                     return; -                 } the } +  A voidDfsintXintYintHintW) the { +     intX1,y1,nx,ny; -ch[x][y]='@'; $      for(x1=-1; x1<=1; x1++) $     { -nx=x+X1; -             if(nx>=0&&nx'#'&&ch[nx][y]!='@') the DFS (NX,Y,H,W); -     }Wuyi      for(y1=-1; y1<=1; y1++) the     { -ny=y+Y1; Wu             if(ny>=0&&ny<w&&ch[x][ny]!='#'&&ch[x][ny]!='@') - DFS (X,NY,H,W); About     } $     return; -}

Challenge Program 2.1.4 Exhaustion Search >> Depth First search exercises POJ1979 black and red

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.