• Poj1979 Red and Black (DFS)


    Red and Black
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 47466   Accepted: 25523

    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) 
    The end of the input is indicated by a line consisting of two zeros. 

    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
    

    Source

     
    神坑的是:xy方向需要换一下
     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath>
     4 using namespace std;
     5 char a[35][35];
     6 int n,m;
     7 int res=0;
     8 int dx[4]={1,-1,0,0};
     9 int dy[4]={0,0,1,-1};
    10 void dfs(int x,int y)
    11 {
    12     res++;
    13     a[x][y]='#';
    14     for(int i=0;i<4;i++){
    15         int nx=x+dx[i],ny=y+dy[i];
    16         if(nx>=0&&nx<n&&ny>=0&&ny<m&&a[nx][ny]=='.'){
    17             dfs(nx,ny);
    18         }
    19     }
    20     return ;
    21 }
    22 void solve()
    23 {
    24     for(int i=0;i<n;i++){
    25         for(int j=0;j<m;j++){
    26             if(a[i][j]=='@'){
    27                 dfs(i,j);
    28             }
    29         }
    30     }
    31 }
    32 int main()
    33 {
    34     while(cin>>m>>n&&n!=0&&m!=0){
    35         
    36         res=0;
    37         for(int i=0;i<n;i++){
    38             for(int j=0;j<m;j++){
    39                 cin>>a[i][j];
    40             }
    41         } 
    42         solve();
    43         cout<<res<<endl;
    44     } 
    45     return 0;
    46 }

     脱离参考书自己再根据自己的理解过一遍:

     1 #include <iostream>
     2 #include <algorithm> 
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 using namespace std;
     7 int n,m;
     8 char a[25][25];
     9 int sx,sy,nx,ny;
    10 int dx[4]={0,0,1,-1};
    11 int dy[4]={1,-1,0,0};
    12 int res;
    13 void dfs(int x,int y)
    14 {
    15     res++;
    16     a[x][y]='#';
    17     for(int i=0;i<4;i++){
    18         nx=x+dx[i],ny=y+dy[i];
    19         if(nx>=0&&nx<m&&ny>=0&&ny<n&&a[nx][ny]=='.'){
    20             dfs(nx,ny);
    21         }
    22     }
    23 }
    24 int main()
    25 {
    26     while(cin>>n>>m&&(n&&m)){
    27         for(int i=0;i<m;i++){
    28             for(int j=0;j<n;j++){
    29                 cin>>a[i][j];
    30                 if(a[i][j]=='@'){
    31                     sx=i,sy=j;
    32                 }
    33             }
    34         }
    35         res=0;
    36         dfs(sx,sy);
    37         cout<<res<<endl;
    38     }    
    39     return 0;
    40 }
  • 相关阅读:
    Xcode 6 下添加pch头文件
    兵器簿之github的配置和使用
    sql 2005性能调优
    C#遍历枚举(Enum)值
    使用 jQuery 调用 ASP.NET AJAX Page Method
    强制不使用“兼容性视图”的HTML代码
    HR在ERP实施过程中的作用
    WdatePicker日历添加事件,在任意月改变时处理日期事件
    JQuery实现表格自动增加行,对新行添加事件
    获取元素离文档各边的距离
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10560942.html
Copyright © 2020-2023  润新知