1. 程式人生 > >HDU 1312Red and Black(dfs)

HDU 1312Red and Black(dfs)

Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem 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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]
#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### [email protected] ###.### ..#.#.. ..#.#.. 0 0

Sample Output

45
59
6
13

Source

Asia 2004, Ehime (Japan), Japan Domestic 想法:簡單dfs 程式碼:
#include<stdio.h>
#include<string.h>
char a[25][25];
int sx,sy,ex,ey,n,m,sum;
int vir[4][2]={0,1,1,0,-1,0,0,-1};
void dfs(int x,int y)
{
    sum++;
    a[x][y]='#';
    for(int i=0;i<4;i++)
    {
        int tx=x+vir[i][0];
        int ty=y+vir[i][1];
        if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]=='.')
        {
            dfs(tx,ty);
        }
    }
    return ;
}
int main()
{
    while(scanf("%d %d",&m,&n)&&n+m)
    {
        getchar();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i]+1);
            for(int j=1;j<=m;j++)
            {
               if(a[i][j]=='@')
               {
                  sx=i,sy=j;
               }
            }
        }
       sum=0;
       dfs(sx,sy);
       printf("%d\n",sum);
    }
    return 0;
}


相關推薦

HDU 1312Red and Blackdfs

Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 1   Accepted Subm

HDU 5952 Counting Cliquesdfs

lap ont there ins -- icpc output stream script Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja

HDU 2660 Accepted Necklace DFS

%d stones 01背包 pla time 要求 highest size bits Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja

HDU 1241 Oil DepositsDFS

這道題是一個比較水的搜尋題,思路就是先找到一塊油田,然後找這塊油田周圍的油田,這樣組成了一大塊油田,看一共有多少大塊油田,輸出。 附程式碼如下: #include<iostream> #include<cstring> #include<cstdio> u

【POJ】1979 Red and BlackBFS

Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 44023 Accepted: 23850 Description There is a

【HDU4016】Magic Bitwise And Operationdfs

Magic Bitwise And Operation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1716    Acce

Red and Black板子

題目描述 題面 There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a bl

hdu - 1426 數獨dfs

搜尋樹的概念 將搜尋過程中每一步的狀態變成樹的一個接點; 根節點為搜尋樹的初始狀態; 搜尋便是不斷擴充套件這棵樹,直至找到目標狀態位置; 搜尋演算法的優化和改正便在於如何拓展搜尋樹的節點; 解題第一步:如何將狀態轉化為樹中的結點,並構造搜尋樹; dfs:對於一個合法的狀態A,對於其所有的子狀

HDU 6351 Beautiful NowDFS

Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 750    Accepted

ZOJ—— 2165 Red and Black搜尋

題目: Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is

HDU 1312 Red and Blackbfs,dfs均可,個人傾向bfs

spec int ger time scrip follow stdio.h stack line 題目代號:HDU 1312 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Li

POJ 1979 Red and Black簡單DFS

either www enter ont false num present direction roo Red and Black Description There is a rectangular room, covered with square tile

HDU 3969 Hawk-and-Chickendfs+tarjan縮點優化,網上最詳細解析!!!

Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4170 &nb

HDU 5054 Alice and Bob數學

esp contain before mod see min roc axis factor 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5054 Problem Description Bob and A

HDU 6060 RXD and dividingLCA

是我 lin size include continue 最大 完全 ref bsp 【題目鏈接】 http://acm.hdu.edu.cn/showproblem.php?pid=6060 【題目大意】   給一個n個節點的樹,要求將2-n號節點分成k部

HDU 3887 Counting OffspringDFS

pac ria div cst key rst n-1 include http Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java

HDU 1426 Sudoku Killerdfs 解數獨

java oid recommend 還要 targe 個數 else content 字符數 傳送門: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 M

HDU - 6266 - HDU 6266 Hakase and Nano 博弈論

hdu 一次 都是 有一個 類型 center ade printf 博弈 題意: 有兩個人從N個石子堆中拿石子,其中一個人可以拿兩次,第二個人只能拿一次。最後拿完的人勝利。 思路: 類型 Hakase先 Hakase後 1 W L 1 1 W W 1

HDU 1016 Prime Ring ProblemDFS

環狀的陣列,故注意判斷首圍相加是否為素數 一個用來存數,一個用來標記是否使用過該數 由n個數組成的環,就有n個素數對,這個是結束條件 #include<stdio.h> int n; int num[25]; int flag[25]; int p

HDU - 1069 Monkey and BananaDP

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mea