DFS PKU 1979

水题保平安

Red and Black

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 22640   Accepted: 12223

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

Sample Output

45
59
6
13
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
//#include <map>
using namespace std;
#define MAXN 20 + 5

char map[MAXN][MAXN];
int w,h;
int vis[MAXN][MAXN];
int num;

void DFS(int x,int y){
    if(x-1>=0&&x-1<h&&y>=0&&y<w&&vis[x-1][y]==0){
        if(map[x-1][y] == '.'){
            num++;
            vis[x-1][y] = 1;
            DFS(x-1,y);
        }
    }
    if(x>=0&&x<h&&y+1>=0&&y+1<w&&vis[x][y+1]==0){
        if(map[x][y+1] == '.'){
            num++;
            vis[x][y+1] = 1;
            DFS(x,y+1);
        }
    }
    if(x+1>=0&&x+1<h&&y>=0&&y<w&&vis[x+1][y]==0){
        if(map[x+1][y] == '.'){
            num++;
            vis[x+1][y] = 1;
            DFS(x+1,y);
        }
    }
    if(x>=0&&x<h&&y-1>=0&&y-1<w&&vis[x][y-1]==0){
        if(map[x][y-1] == '.'){
            num++;
            vis[x][y-1] = 1;
            DFS(x,y-1);
        }
    }
}

int main(){
    int i,j;

    while(~scanf("%d%d",&w,&h)){
        if(w==0 && h==0){
            break;
        }
        for(i=0;i<h;i++){
            scanf("%s",map[i]);
        }
        memset(vis,0,sizeof(vis));
        int flag = 0;
        for(i=0;i<h;i++){
            for(j=0;j<w;j++){
                if(map[i][j] == '@'){
                    vis[i][j] = 1;
                    num = 0;
                    DFS(i,j);
                    flag = 1;
                    break;
                }
            }
            if(flag == 1){
                break;
            }
        }
        printf("%d\n",num+1);
    }

    return 0;
}
时间: 2024-08-04 05:18:33

DFS PKU 1979的相关文章

DFS PKU 1562

简单的DFS Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12801   Accepted: 6998 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectan

POJ 1979 Red and Black dfs 难度:0

http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; const int maxn = 21; bool vis[maxn][maxn]; char maz[maxn][maxn]; int n,m; const int dx[4] = {1,-1,0,0}; const int dy[4] = {0,0,1,-1}; int ans; bool in(int

pku 2488 A Knight&#39;s Journey (搜索 DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 9822 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey aro

hdu 1979 DFS + 字典树剪枝

http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 373    Accepted Submission(s): 155 Problem Description There is a matrix of 4*4, yo

《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

B - Red and Black Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1979 Description There is a rectangular room, covered with square tiles. Each tile is col

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

DFS:Red and Black(POJ 1979)

红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一次哈哈哈哈 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX_N 20 4 5 static int dp[MAX_N][MAX_N]; 6 static int startx; 7 static int starty;

poj 1979 dfs

水过,注意边界不能超出. #include <iostream> using namespace std; int n, m, sx, sy, dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}, count; char diagram[23][23]; void get_diagram(void) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> diag

DFS + 剪枝 PKU 1088

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 78527   Accepted: 29187 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17