航院1312 Red and Black

Problem J - Red and Black

题目来源:https://vjudge.net/contest/207868#problem/J

problem description:

There is a rectangular room,covered with square tiles. Each tile is colored either red or black. A man isstanding on a black tile. From a tile, he can move to one of four adjacenttiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count thenumber of black tiles which he can reach by repeating the moves describedabove.


Input

The input consists of multipledata sets. A data set starts with a line containing two positive integersWandH;W andH are the numbers of tiles in thex-andy- directions, respectively.W andH are not more than20.

There are H more linesin the data set, each of which includesW characters. Each characterrepresents 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, yourprogram should output a line which contains the number of tiles he can reachfrom the initial tile (including itself).


Sample Input

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


Sample Output

45
59
6
13

题意概括:从某点出发,计算从此点一共能到达多少个目标点

解题思路:广度优先搜索

AC代码:

#include<stdio.h>
#include<string.h>
 
struct note
{
  int x;
  int y;
};
 
int main(void)
{
  struct note que[450];
  char a[22][22]; 
  int book[22][22];
  int next[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  int head, tail;
  int i, j, k, n, m, startx, starty,p, q, tx, ty, flag;
  
  while(scanf("%d%d", &n, &m) != EOF)
  {
         getchar();
         if(n == 0 || m == 0)
         {
                break;
         }
         flag = 0;
         memset(book, 0, sizeof(book));
         for(i = 0; i <= m - 1; i ++)
         {
                scanf("%s", a[i]);
         }
         for(i = 0; i <= m - 1; i ++)
         {
                for(j = 0; j <= n - 1; j ++)
                {
                       if(a[i][j] == '@')
                       {
                              p = i;
                              q = j;
                              flag = 1;
                              break;
                       }
                }
                if(flag == 1)
                {
                       break;
                }
         }
         head = 1;
         tail = 1;
         que[tail].x = p;
         que[tail].y = q;
         tail ++;
         book[p][q] = 1;
         while(head < tail)
         {
                for(k = 0; k <= 3; k ++)
                {
                       tx = que[head].x + next[k][0];
                       ty = que[head].y + next[k][1];
                       if(tx < 0 || tx > m - 1 || ty < 0 ||ty > n - 1)
                       {
                              continue;
                       }
                       if(a[tx][ty] == '.' && book[tx][ty] ==0)
                       {
                              book[tx][ty] = 1;
                              que[tail].x = tx;
                              que[tail].y = ty;
                              tail ++;
                       }
                }
                head ++;
         }
         printf("%d\n", tail - 1);
  }     
  return 0;

错误分析:无错误

posted @ 2018-01-23 17:33  moonlight987  阅读(141)  评论(0编辑  收藏  举报