POJ 1979 题解
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 31722 | Accepted: 17298 |
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 "cstring"
- #include "cmath"
- #include "iostream"
- #include "string"
- using namespace std ;
- const int maxN = ;
- const int dx[ ] = { , , , - } ;
- const int dy[ ] = { , , - , } ;
- char mp[ maxN ][ maxN ] ;
- int N , M ;
- void DFS ( const int xi , const int yi ) {
- if ( !mp[ xi ][ yi ] )return ;
- mp[ xi ][ yi ] = '%' ;
- for ( int i= ; i< ; ++i ) {
- int xx = xi + dx[ i ] ;
- int yy = yi + dy[ i ] ;
- if ( xx > && xx <= M && yy > && yy <= N && mp[ xx ][ yy ] == '.' )
- DFS( xx , yy ) ;
- }
- }
- int sumerize ( const int n , const int m ) {
- int ret ( ) ;
- for ( int i= ; i<=n ; ++i ) {
- for ( int j= ; j<=m ; ++j ) {
- if ( mp[ i ][ j ] == '%' ) ++ret ;
- }
- }
- return ret ;
- }
- int main ( ) {
- int start_x , start_y ;
- while ( scanf ( "%d%d\n" , &N , &M ) == && N && M ) {
- for ( int i= ; i<=M ; ++i ) {
- scanf ( "%s" , mp[ i ] + ) ;
- }
- for ( int i= ; i<=M ; ++i ) {
- for ( int j= ; j<=N+ ; ++j ) {
- if ( mp[ i ][ j ] == '@' ) {
- mp[ i ][ j ] = '.' ;
- start_x = i ;
- start_y = j ;
- goto Loop ;
- }
- }
- }
- Loop :
- DFS ( start_x , start_y ) ;
- printf ( "%d\n" , sumerize ( M , N ) ) ;
- memset ( mp , , sizeof ( mp ) ) ;
- }
- return ;
- }
2016-10-21 16:28:48
POJ 1979 题解的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 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 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(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- POJ 1979 DFS
题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace ...
- POJ 1979 红与黑
题目地址: http://poj.org/problem?id=1979 或者 https://vjudge.net/problem/OpenJ_Bailian-2816 Red and Blac ...
- POJ 1979 Red and Black (zoj 2165) DFS
传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
随机推荐
- js中Window 对象及其的方法
window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...
- hdu 1429
http://acm.hdu.edu.cn/showproblem.php?pid=1429 一个广搜的简单题吧,不过有意思的事这个题目用到了位运算,还有就是很恶心的MLE #include < ...
- socket泄露的问题
在增加keepalive和libevent 超时后,仍然会几个月后出现lsof统计句柄过多的问题,如下图,红线圈定的是不定期出现的错误关闭流,时间点和server端日志(只看到accpet fd,没有 ...
- ->code vs 2879 堆的判断(堆的学习一)
2879 堆的判断 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父 ...
- JSP复习整理(五)JavaBean使用表单处理数据
一.先建立用户输入的数据 usingGetparameter.html <!DOCTYPE html> <html> <head> <meta charset ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Linux学习笔记之兄弟连
systemctl --user enable pulseaudio说明:安装完成后系统没有声音,用该命令可以打开.ifconfig eth0 192.168.118.1说明:给网卡设置IP地址.ap ...
- html5 websocket 实时日志推送
http://blog.csdn.net/neutrojan/article/details/46119645
- UE3名称结构(Name)
解释说明: (1) 直接通过FName的index进行比较来判断两个FName是否相等 (2) 通过FName的index从全局Names数组中取出对应的FNameEntry,可以获得FName的字符 ...
- SSH ProxyCommand
简单的说就是SSH层面的代理服务器,实现通过跳板机执行SSH相关命令到目标机器的代理服务.