博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1979 Red and Black
阅读量:6919 次
发布时间:2019-06-27

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

Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 34862   Accepted: 18856

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

4559613

Source

#include 
#include
#include
#include
#include
#define maxn 105using namespace std;int n,m,num,vis[maxn][maxn];int dx[]={
1,-1,0,0},dy[]={
0,0,1,-1};char mapn[maxn][maxn];struct node{ int x,y;};void bfs(int a,int b){ queue
q; node p; p.x = a; p.y = b; num++; vis[a][b] = 1; q.push(p); while(!q.empty())//加入队列的都是可以走到的点,所以只需ans++即可 { node now = q.front(); int x = now.x,y = now.y; q.pop(); for(int i=0;i<4;i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(xx<0||yy<0||xx>(n-1)||yy>(m-1)||mapn[xx][yy]=='#'||vis[xx][yy]) continue; num++; node tmp; tmp.x = xx; tmp.y = yy; q.push(tmp); vis[xx][yy] = 1; } }}int main(){ while(cin >> m >> n)//注意这里输入的是m,n { memset(vis,0,sizeof(vis)); num = 0; if(n==0||m==0) break; for(int i=0;i
> mapn[i][j]; for(int i=0;i

 

转载于:https://www.cnblogs.com/l609929321/p/6696580.html

你可能感兴趣的文章
java.util.concurrent.ExecutionException: org.ap...
查看>>
git ssh免密配置
查看>>
面试题(12)
查看>>
WordPress超级文本小工具:Enhanced Text Widget
查看>>
Linux文件系统简介
查看>>
eclipse下安装m2e的maven插件报错的各类解决方案(含pom editor没装好的解决方案)
查看>>
java23中设计模式——创建模式——singleton(单列)
查看>>
【微信小程序开发】来看看,那些年我们踩过的坑~
查看>>
微信小程序联盟:官方文档+精品教程+demo集合(6月16日更新,持续更新中……)...
查看>>
重载new与delete
查看>>
MySQL管理与优化(9):存储过程和函数
查看>>
Java程序员须知的七个日志管理工具
查看>>
mac 下安装编译 yasm
查看>>
Eclipse更改maven项目名
查看>>
Android:资源 id 及资源 id 的动态获取
查看>>
蓝萝卜blu Hibernate4中使用getCurrentSession报Could not o
查看>>
android R文件相关问题
查看>>
设计模式:命令
查看>>
Chrom API 学习笔记
查看>>
Android findViewById与findViewWithTag()
查看>>