zoukankan      html  css  js  c++  java
  • Red and Black

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=67590#problem/D
     
    Red and Black
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

     

    Sample Output

    45
    59
    6
    13
     
    CODE:
     1 #include<stdio.h>
     2 
     3 char a[25][25];
     4 
     5 int w, h;
     6 
     7 int f(int x, int y)
     8 {
     9     if(x < 0 || x >= h || y < 0 || y >= w)
    10         return 0;
    11     if(a[x][y] == '#')
    12         return 0;
    13     else
    14     {
    15         a[x][y] = '#';
    16         return 1 + f(x, y-1) + f(x, y+1) + f(x-1, y) + f(x+1, y);
    17     }
    18 }
    19 
    20 int main()
    21 {
    22     int i, j, x, y;
    23 
    24     while(scanf("%d%d", &w, &h), w+h)
    25     {
    26         for(i = 0; i < h; i++)
    27             for(j = 0; j < w; j++)
    28             {
    29                 scanf(" %c", &a[i][j]);
    30                 if(a[i][j] == '@')
    31                 {
    32                     x = i;
    33                     y = j;
    34                 }
    35             }
    36 
    37         printf("%d
    ", f(x, y));
    38     }
    39     return 0;
    40 }
    View Code

    受不鸟为啥这样不对,先放这好了:

    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    #define N 25
    
    int w, h, cou;
    char maps[N][N];
    int dir[4][2] = { {-1, 0}, {0, -1}, {0, 1}, {1, 0}};
    
    void DFS(int x, int y)
    {
        int i, nx, ny;
    
        cou++;
        
        maps[x][y] = '#';
    
        for(i = 0; i < 4; i++)
        {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if(maps[nx][ny] == '.' && nx >= 0 && nx < h && ny >= 0 && ny < w)
            {
                DFS(nx, ny);
            }
        }
    }
    int main()
    {
        int i, j;
    
        while(cin >> w >> h, w+h)
        {
           for(i = 0; i < h; i++)
    			for(j = 0; j < w; j++)
    				scanf(" %c", &maps[i][j]);
    
            for(i = 0; i < h; i++)
                for(j = 0; j < w; j++)
            {
                if(maps[i][j] == '@')
                    DFS(i, j);
            }
            cout << cou << endl;
        }
        return 0;
    }
    

      

    让未来到来 让过去过去
  • 相关阅读:
    黑马程序员JAVA基础Java 集合之Collections 工具类
    黑马程序员JAVA基础Java 集合之Set 接口
    黑马程序员JAVA基础Java 集合之List 集合
    通过回车来完成数据的提交
    BizTalk中多个子记录映射解决办法
    框架的展开与关闭
    一个有用的触发器
    SQL 中分隔字符串的获取
    未保存离开页面要给出提示
    常用的一些javascript小技巧 很实用
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4652125.html
Copyright © 2011-2022 走看看