首页 > Web开发 > 详细

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

时间:2015-05-27 20:34:51      阅读:415      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!