zoukankan      html  css  js  c++  java
  • HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #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
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    NOIP 2016 回文日期
    USACO Ski Course Design
    USACO Combination Lock
    USACO 利润Profits
    POJ 3368 Frequent values
    USACO Balanced Lineup
    JDOJ 1065 打倒苏联修正主义
    JDOJ 2174 忠诚
    VIJOS-P1514 天才的记忆
    VIJOS-P1423 最佳路线
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4534233.html
Copyright © 2011-2022 走看看