• hdu 1312(DFS)


                   Red and Black

    Tme Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18126    Accepted Submission(s): 11045

    Problem 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
    6 9
    ....#.
    .....#
    ......
    ......
    ......
    ......
    ......
    #@...#
    .#..#.
    11 9
    .#.........
    .#.#######.
    .#.#.....#.
    .#.#.###.#.
    .#.#..@#.#.
    .#.#####.#.
    .#.......#.
    .#########.
    ...........
    11 6
    ..#..#..#..
    ..#..#..#..
    ..#..#..###
    ..#..#..#@.
    ..#..#..#..
    ..#..#..#..
    7 7
    ..#.#..
    ..#.#..
    ###.###
    ...@...
    ###.###
    ..#.#..
    ..#.#..
    0 0
     Sample Output
    45
    59
    6
    13
     题意就是  找从@点开始出发,问所能到达的'.'点的个数(最开始理解错了意思,以为是一条路径的最大值,后面
    才发现是可以到达的‘.'点 ),直接DFS,
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #include<cstdlib>
    #include<vector>
    #include<set>
    #include<queue>
    #include<cstring>
    #include<string.h>
    #include<algorithm>
    #define INF 0x3f3f3f3f
    typedef long long ll;
    typedef unsigned long long LL;
    using namespace std;
    const int N = 1e6+10;
    const ll mod = 1e9+7;
    int t=0,flag=0;
    char s[26][26];
    int visited[26][26];
    int m,n;
    int _x[4]={0,1,-1,0};
    int _y[4]={1,0,0,-1};
    int ans;
    void DFS(int x,int y){
        for(int t=0;t<4;t++){
            int i=x+_x[t];
            int j=y+_y[t];
            if(i>=0&&j>=0&&visited[i][j]==0&&s[i][j]=='.'&&i<=n-1&&j<=m-1)
            {
                ans++;
                visited[i][j]=1;
                DFS(i,j);
            }
        }
    }
    int main(){
        while(scanf("%d%d",&m,&n)!=EOF){
            if(m==0&&n==0)break;
            memset(visited,0,sizeof(visited));
            int ii,jj;
            int i,j;
            for(ii=0;ii<n;ii++){
                for(jj=0;jj<m;jj++){
                    cin>>s[ii][jj];
                    if(s[ii][jj]=='@'){i=ii;j=jj;}
                }
            }
            ans=1;
            visited[i][j]=1;
            DFS(i,j);
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    数据库的操作封装成类
    简单搭建WEB框架及原理
    界面小项目之小米商品排列
    界面小项目之小米登录注册
    kubernetes版本1.7.6对比1.13.0
    docker将宿主机文件复制到容器内
    常用git命令
    centos7常用命令汇总
    openstack相关的命令总结
    kubernetes常用命令2
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6103158.html
Copyright © 2020-2023  润新知