Red and Black(poj 1979 bfs)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 27891 | Accepted: 15142 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
Sample Input
- 6 9
- ....#.
- .....#
- ......
- ......
- ......
- ......
- ......
- #@...#
- .#..#.
- 11 9
- .#.........
- .#.#######.
- .#.#.....#.
- .#.#.###.#.
- .#.#..@#.#.
- .#.#####.#.
- .#.......#.
- .#########.
- ...........
- 11 6
- ..#..#..#..
- ..#..#..#..
- ..#..#..###
- ..#..#..#@.
- ..#..#..#..
- ..#..#..#..
- 7 7
- ..#.#..
- ..#.#..
- ###.###
- ...@...
- ###.###
- ..#.#..
- ..#.#..
- 0 0
Sample Output
- 45
- 59
- 6
- 13
Source
- #include <cstdio>
- #include <algorithm>
- #include <iostream>
- #include <cstring>
- #include <queue>
- using namespace std;
- char map[][];
- bool vis[][];
- int dx[]={,,,-},dy[]={,-,,};
- int w,h;
- int sx,sy;
- struct node
- {
- int x,y;
- };
- int bfs()
- {
- queue<node> q;
- int ans=;
- memset(vis,,sizeof(vis));
- node s,temp;
- s.x=sx,s.y=sy;
- q.push(s);
- while(!q.empty())
- {
- s=q.front();
- q.pop();
- ans++;
- for(int i=;i<;i++)
- {
- temp.x=s.x+dx[i];
- temp.y=s.y+dy[i];
- if(temp.x>=&&temp.x<h&&temp.y>=&&temp.y<w&&vis[temp.x][temp.y]==&&map[temp.x][temp.y]=='.')
- {
- vis[temp.x][temp.y]=;
- q.push(temp);
- }
- }
- }
- return ans;
- }
- int main()
- {
- int i,j;
- freopen("in.txt","r",stdin);
- while(scanf("%d%d",&w,&h))
- {
- if(w==&&h==)
- break;
- for(i=;i<h;i++)
- scanf("%s",map[i]);
- for(i=;i<h;i++)
- for(j=;j<w;j++)
- if(map[i][j]=='@')
- {
- sx=i,sy=j;
- break;
- }
- cout<<bfs()<<endl;
- }
- return ;
- }
Red and Black(poj 1979 bfs)的更多相关文章
- DFS:Red and Black(POJ 1979)
红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- POJ 1979 Red and Black (BFS)
链接 : Here! 思路 : 简单的搜索, 直接广搜就ok了. /****************************************************************** ...
- POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
随机推荐
- Go and JSON
Go and JSON 在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- CoreData (三)备
NSFetchedResultsController 什么是NSFetchedResultsController NSFetchedResultsController是一个让人爱恨交加的一个类.如果使 ...
- 关于NGINX变量的一些测试结果
作为NGINX变量,不像正规语言那么正式. 但处理自定义和内部变量时,这些操作,也是少不了的. geo $dollar { default "$"; } server { list ...
- 下一代云计算模式:Docker正掀起个性化商业革命
作者: 吴宁川 来源: ITValue 发布时间: 2015-09-20 10:41 阅读: 10008 次 推荐: 16 原文链接 [收藏] 文/ITValue 记者吴宁川 从 20 ...
- spring 4 泛型注入
最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...
- 比较有用的log4j.properties
转自 http://walsh.iteye.com/blog/314941 log4j.properties log4j.rootLogger=DEBUG,CONSOLE,DATABASE,FIL ...
- SQL生成随机字符串
1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')
- Android使用bindService启动服务
1.Service package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import andr ...
- Linux usb子系统(三):通过usbfs操作设备的用户空间驱动
内核中提供了USB设备文件系统(usbdevfs,Linux 2.6改为usbfs,即USB文件系统),它和/proc类似,都是动态产生的.通过在/etc/fstab文件中添加如下一行:none /p ...