ACM 之 Red and Black

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)

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

理解:

相當(dāng)于求聯(lián)通塊,這種題做過一次在遇到就會感覺簡單很多.同樣用到DFS.
還有需要注意的是 , 輸入順序 , 題目是把第一個輸入的值當(dāng)做列,第二個輸入的值當(dāng)做行數(shù) .

代碼部分

#include<iostream>
using namespace std;
char a[21][21];int b[21][21],m,n,sum;
void dfs(int i,int j)
{
    if(a[i][j]=='#'||i<0||i>m-1||j<0||j>n-1||b[i][j]==1||a[i][j]!='.') return ;
    b[i][j]=1;sum++;
    dfs(i+1,j);
    dfs(i-1,j);
    dfs(i,j-1);
    dfs(i,j+1);
}
int main()
{
    while(cin>>n>>m)
    {
        int x,y;
        if(m==0&&n==0)
            return 0;
        sum=0;
        for(int i=0;i<21;i++)
        {
            for(int j=0;j<21;j++)
            {
                b[i][j]=0;
            }
        }
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='@')
                {
                    x=i;y=j;a[i][j]='.';
                }
            }
        }
        dfs(x,y);
        /*for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cout<<b[i][j];
            }
            cout<<endl;
        }*/
        cout<<sum<<endl;
    }
    return 0;
}

意見反饋 || 任何建議

聯(lián)系我(新浪)
郵箱:qianlizhihao@gmail.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,013評論 0 23
  • 過去的一年里,微軟成長為了一個分裂的存在。煥然一新、刮目相待、自食其言、垂垂老矣都是他的標(biāo)簽。可以說在這一年里,微...
    969227e884b9閱讀 323評論 0 1
  • 雨一直在下,凌晨5:20我被潤潤搖醒,她告訴我她在拉肚子,從昨晚半夜到現(xiàn)在已經(jīng)拉了好多次了,因為見我一直呼呼大睡,...
    敦敏_無我利他閱讀 291評論 0 0
  • 第一章 楔子 朦朧中,一位少女慢慢的起身尋找旁邊的女孩,那是她的妹妹。尋找自己和身上的物品。她在脖子...
    暮雪戀羽閱讀 159評論 0 0

友情鏈接更多精彩內(nèi)容