程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1312:Red and Black(DFS搜索),hdudfs

HDU 1312:Red and Black(DFS搜索),hdudfs

編輯:C++入門知識

HDU 1312:Red and Black(DFS搜索),hdudfs


 

               HDU 1312:Red and Black

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u  

Description

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 integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. 

There are H more lines in the data set, each of 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 two 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).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

題解:本題還是DFS搜索(上下左右),只是增加一個計數器,計算可以走的地方的個數。       規定地圖中有可通行的位置,也有不可通行的位置,已知起點,求起點的與它相連成一片的部分,在這道題裡輸出相連的位置的數目。     從起點開始,遍歷每一個到達的點的四個方向(不再是八個),到達一個位置就將這個位置的字符變成不可走的'#',並且計數+1。其實就是計數將可走變成不可走的操作進行了多少次。 這樣可以不用擔心走過了還會重復。    AC 代碼:如果你看過我的上一篇你一定會懂
#include<cstdio>
#include<cstring>
char pic[110][110];
int m,n,total;
int idx[110][110];

void dfs(int r,int c,int id)
{
    if(r<0||r>=m||c<0||c>=n)
        return;
    if(idx[r][c]==666||pic[r][c]!='.')
        return;
    idx[r][c]=id;
    total++;
    for(int dr=-1; dr<=1; dr++)
        for(int dc=-1; dc<=1; dc++)
            if(dr==0||dc==0)
                dfs(r+dr,c+dc,id);
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)==2&&m&&n)
    {
        for(i =0; i<m; i++)
            scanf("%s",pic[i]);
        memset(idx,0,sizeof(idx));
        total=0;
        for(i=0; i<m; i++)
            for(j=0; j<n; j++)
            {
                if(pic[i][j]=='@')
                {
                    pic[i][j]='.';
                    dfs(i,j,666);
                }
            }
        printf("%d\n",total);
    }
    return 0;
}

 

      這個是我在其他博客上看得到的方法,用#填充,可以一試!  
#include <iostream>
using namespace std;
char a[25][25];
int n,m,total;
int dr[4] = {0,1,0,-1};//行變化
int dc[4] = {1,0,-1,0};//列變化
//上面的原來一直不會用,知道的話非常方便
bool judge(int x,int y)
{
    if(x<1 || x>n || y<1 || y>m)
        return 1;
    if(a[x][y]=='#')
        return 1;
    return 0;
}
void dfs(int r,int c)
{
    total++;
    a[r][c]='#';               //走過一次,“。”變為“#”,避免重復
    for(int k=0; k<4; k++)
    {
        int lr = r + dr[k];
        int lc = c + dc[k];
        if(judge(lr,lc))
            continue;
        dfs(lr,lc);
    }

}
int main()
{
    while(cin>>m>>n&&m&&n)
    {
        int i,j,x,y;
        total=0;
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='@')             //這裡必須用變量x,y
                    x=i,y=j;

            }
        dfs(x,y);
        cout<<total<<endl;
    }
    return 0;
}

 

 

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