码迷,mamicode.com
首页 > Web开发 > 详细

HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

时间:2015-05-27 20:37:21      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define max(a, b)(a > b ? a : b)
#define N 30

char maps[N][N];
int m, n, ans;
int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void DFS(int x, int y)
{
    int a, b, i;
    maps[x][y] = #;//自己站的地方需要标记,以后不能再走,否则会出错
    if(x < 0 || x >= m || y < 0 || y >= n)
        return ;
    for(i = 0 ; i < 4 ; i++)
    {
        a = x + d[i][0];
        b = y + d[i][1];
        if(a >= 0 && a < m && b >= 0 && b < n && maps[a][b] != #)
        {
            maps[a][b] = #;
            ans++;
            DFS(a, b);
        }
    }
    return ;
}

int main()
{
    int i, j;
    while(scanf("%d%d", &n, &m), m + n)
    {
        ans = 1;//能走的包括自己站的地方
        for(i = 0 ; i < m ; i++)
            scanf("%s", maps[i]);
        for(i = 0 ; i < m ; i++)
        {
            for(j = 0 ; j < n ; j++)
            {
                if(maps[i][j] == @)
                    DFS(i, j);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

标签:

原文地址:http://www.cnblogs.com/qq2424260747/p/4534233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!