程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 2386Lake Counting(dfs)

poj 2386Lake Counting(dfs)

編輯:C++入門知識

 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18826 Accepted: 9463

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

Source

USACO 2004 November
這道題也算是dfs中的基礎題,跟 nyist 27 有點類似,但是這道題wa了我好久啊。。。。最後終於ac了
#include
char map[101][101];
int n,m;
int move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};//這裡定義八個可行方向
void dfs(int x,int y)
{
    map[x][y]='.'; //標記走過的路徑
    for(int i=0;i<8;i++)
    {
        int dx=x+move[i][0];
        int dy=y+move[i][1];
        if(dx>=0&&dx=0&&dy=0&&nx=0&&ny
這個題目讓我也意識到了以前沒有注意到的小細節,以後做題的時候就不能再犯了 ,對於連續字符串的輸入的時候要特別注意,是不是把換行符加入進去了,改變了原字符串的結構,對於連續子都串的輸入要特別注意!!!

下面這道題和上面這道題基本類似,就果斷水過啦~ poj1562
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11971 Accepted: 6474

Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

Input

The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

Output

are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2

Source

Mid-Central USA 1997
下面是我的代碼
#include 
#include 
char map[102][102];
int move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int n,m;
void dfs(int x,int y)
{
    map[x][y]='*';
    for(int i=0;i<8;i++)
    {
        int dx=x+move[i][0];
        int dy=y+move[i][1];
        if(dx>=0&&dx=0&&dydfs的基礎題水過。 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved