ZOJ 2165 Red and Black

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 HW 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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0


Sample Output

45
59
6
13

题目大意:

给出两个数m和n,代表n行m列,都不超过20,然后是n行m列的图,包括‘.‘ , ‘#‘ , ‘@‘3个字符。

@代表初始位置,‘.‘代表通路,‘#’代表墙,求最大可到达的 ‘.‘ 的数量 ‘@‘算一个

BFS:

即求最大连通块问题 不一定非要用队列 一维数组一样可以解决

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct node
 4 {
 5    int x,y;
 6 }q[410];
 7
 8 struct node P, N;
 9 int flag[25][25];
10 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
11 char str[25][25];
12
13 int main()
14 {
15      int c, r, i, j, front, rear;
16      while(scanf("%d%d",&c,&r)!=EOF, c + r){
17         memset(flag, 0, sizeof(flag));
18         for(i = 0; i < r; i++)
19             scanf("%s", str[i]);
20
21         for(i = 0; i < r; i++){
22             for(j=0;j<c;j++)
23                 if(str[i][j] == ‘@‘) break;
24             if(str[i][j] == ‘@‘) break;
25         }
26         N.x = i;
27         N.y = j;
28         flag[i][j] = 1;
29         q[0] = N;
30         front = 0;
31         rear = 1;
32
33         while(front < rear){
34             N = q[front++];
35             for(i = 0; i < 4; i++){
36                 int tx = N.x + dir[i][0];
37                 int ty = N.y + dir[i][1];
38                 if(tx >= 0 && tx < r && ty >= 0&& ty < c && flag[tx][ty] == 0 && str[tx][ty] == ‘.‘){
39                     P.x = tx;
40                     P.y = ty;
41                     q[rear++] = P;
42                     flag[tx][ty] = 1;
43                 }
44             }
45         }
46         printf("%d\n",rear);
47      }
48      return 0;
49 }

ZOJ 2165 Red and Black

时间: 2024-09-29 10:12:11

ZOJ 2165 Red and Black的相关文章

poj 1979 &amp;&amp; zoj 2165 Red and Black

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22409   Accepted: 12100 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

ZOJ 2165

Red and Black Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 tile

ZOJ red black tree

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <map> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 10

ZOJ 3671 Japanese Mahjong III

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3671 Japanese Mahjong III Time Limit: 2 Seconds      Memory Limit: 65536 KB Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this proble

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e

ZOJ 2477 Magic&#160;Cube(魔方)

ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 * 3 small cubes. We can unwrap the cube, it will become like this: 这是个有名的儿童游戏.游戏

2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among the vertices, of them are red, while the others are black. The root of the tree is vertex 1 and it's a red vertex.Let's define the cost of a red verte

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版

red hat7 系统可以ping通ip地址但是不能ping通域名

在red hat7中ifconfig后出现这样的情况,ens33是物理网卡,与eth0一样只是不同的名字.但是只能ping通ip地址不能ping通域名. 解决方法: 在文件 /etc/resolv.conf文件下更改nameserver的值 加上 nameserver 8.8.8.8  域名解析服务  nameserver 8.8.4.4为备用的 在文件 etc/sysconfig/network-scripts下修改 ifcfg-eth0 或者 ifcfg-ens33文件,设置静态ip地址,