博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ Problem Set - 2165 Red and Black
阅读量:4679 次
发布时间:2019-06-09

本文共 2245 字,大约阅读时间需要 7 分钟。

ZOJ Problem Set - 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 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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45

59
6
13

#include
#include
#include
#define MAX 30using namespace std;char map[MAX][MAX];int count=0;int w,h;int dir[4][2]={
{0,-1},{-1,0},{0,1},{1,0}};void dfs(int r,int c){// cout<<"r: "<
<<" c: "<
<
h||c>w) { return; } map[r][c]='#'; for(int i=0;i<4;i++) { if(map[r+dir[i][0]][c+dir[i][1]]=='.') { // map[r+dir[i][0]][c+dir[i][1]]='#'; count++; dfs(r+dir[i][0],c+dir[i][1]); // map[r+dir[i][0]][c+dir[i][1]]='.'; } }}int main(){ while(cin>>w>>h) { if(w==0||h==0)break; int sr,sc; count=0; memset(map,0,sizeof(map)); for(int i=1;i<=h;i++) { for(int j=1;j<=w;j++) { cin>>map[i][j]; if(map[i][j]=='@') { sr=i;sc=j; } } } map[sr][sc]='#'; //cout<<"Mark #1"<

 

转载于:https://www.cnblogs.com/jackwuyongxing/p/3366493.html

你可能感兴趣的文章
CRM创建物料FM2
查看>>
20145228《信息安全系统设计基础》第四次实验实验报告
查看>>
周报_2012第13周(2012/03/25-2012/03/31)
查看>>
读Google三大论文后感
查看>>
3分钟搞明白信用评分卡模型&模型验证
查看>>
ubuntu14.04 mysql数据库允许远程访问设置
查看>>
1. Ruby基础知识
查看>>
ETL总结(扫盲版)
查看>>
Android Studio插件之FindBugs
查看>>
jisuanke
查看>>
Pull解析器解析XML文件和生成XML文件
查看>>
HDU——1059Dividing(母函数或多重背包)
查看>>
Python Logging模块的简单使用
查看>>
2012放假明细
查看>>
匈牙利算法求解任务分配问题
查看>>
开源协议的比较
查看>>
1115 Counting Nodes in a BST (30 分)建立二叉搜索树,求每层结点数目
查看>>
Linux修改hostname与免密码登录
查看>>
node全局对象 文件系统
查看>>
力扣——找数左下角的值
查看>>