Solutions Using DFS in zoj 2165

Source: Internet
Author: User

Recently learning Graph TheoryAlgorithmIn fact, I have been learning for a long time and found that I have not made any progress. The reason is that I writeCodeThe time is still too short. Now, I start to use what I learned to solve some problems and help myself better understand the essence of algorithms! Come on!

In zoj, I found some questions and wrote them!

The following is a question!

 

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

Sample Input

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#.......#.

.#########.

...... 11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7

..#.#..

..#.#..

###.###

...@...

###.###

..#.#..

..#.#..

0 0

Sample output

45

59

6

13

Below is the code I wrote using DFS search!

# Include <cstdio>
CharMap [21] [21];
IntM, N;
IntCount;
IntDir [4] [2] = {{0,1},{0,-1},{1,0},{-1,0}; // Indicates the direction of movement. This array can be used to represent
IntDFS (IntX,IntY)
{
IntI, XX, YY;
Map [x] [Y] ='#';
For(I =0; I <4; I ++)
{
Xx = x + dir [I] [0];
YY = Y + dir [I] [1];
If(Xx <0| YY <0| XX> = M | YY> = N)
Continue;
If(Map [XX] [YY] ='.')
{
Map [XX] [YY] ='#';
Count ++;
//Printf ("% d", count );
DFS (XX, YY );
}
}
ReturnCount;
}
IntMain ()
{
While(1)
{
IntI, j, X, Y;
Scanf ("% D", & N, & M );
If(M =0& Amp; n =0)
Break;
Char temp;
Scanf ("% C", & temp); // you have already entered a number on the current side. If you want to enter a number next to it, press Enter, and enter is a character, so it should be absorbed,This method can be used now!
For(I =0; I <m; I ++)
{
 For(J =0; J <n; j ++)
{
Scanf ("% C", & Map [I] [J]);
If(Map [I] [J] ='@')
{
X = I;
Y = J;
}
}
Scanf ("% C", & Temp );
}
Count =0;
DFS (x, y );
Printf ("% D \ n", Count +1);// Because @ is also a black brick, Count must be added!
}
}

This is to use DFS to calculate the number of bricks in a maze. Now I can only think of using DFS for search. In fact, the use of DFS is very simple, but it is not very easy to understand. Now I will summarize it!

When an image is stored using an adjacent matrix,

DFS (vertex I)

{Visited [I] = 1;

For (j = 0; j <n; j ++) // for all other vertex J

{

If (edge [I] [J] = 1 & visted [J]) // vertex J has not been accessed, and J is the adjacent vertex of I!

{

// Write the code before recursive search

 

DFS (vertex J)

// The following is the rollback position, but you need to write code in this place to restore the original situation!

}

}

}

If an adjacent table is used to store a graph

DFS (vertex I)

{Vistited [I] = 1;

// P is the header pointer of I;

While (P! = NULL)

{

If (visit [J]! = 1)

{

DFS (j );

}

}

}

This is a summary of some things. In the actual process, we need to learn more!

 

 

 

 

 

 

 

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.