ZOJ 2165 Red and Black

题目

简单搜索,注意W,H各表示什么;

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 25;

char map1[MAXN][MAXN];

int W, H;
int stepnum;
int dir[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };

bool Cango(int x, int y)
{
     if(x >= 0 && y >= 0 && x < H && y < W && map1[x][y] != '#')
          return true;
     else
          return false;
}


void DFS( int x, int y)
{
     stepnum++;
     map1[x][y] = '#';
     for(int i = 0; i < 4; ++i)
     {
          int xx = x + dir[i][0];
          int yy = y + dir[i][1];
          if( Cango(xx, yy) )
               DFS(xx, yy);
     }
}

int main()
{
     int i, j;
     int si, sj;
     while((cin>>W>>H) && W && H)
     {
          for(i = 0; i < H; ++i)
          {
               for(j = 0; j < W; ++j)
               {
                    cin>>map1[i][j];
                    if(map1[i][j] == '@')
                         si = i, sj = j;
               }
          }
          stepnum = 0;
          DFS(si, sj);

          cout<<stepnum<<endl;
     }
     return 0;
}

/**

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

*/


 

你可能感兴趣的:(DFS)