1.链接地址:

http://bailian.openjudge.cn/practice/1979

http://poj.org/problem?id=1979

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
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.

输入
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.

输出
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).
样例输入
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
样例输出
45
59
6
13
来源
Japan 2004 Domestic

3.思路:

4.代码:

 #include <iostream>
#include <cstdio> using namespace std; int f(char **arr,const int i,const int j,int w,int h)
{
int res = ;
if(arr[i][j] == '.')
{
res += ;
arr[i][j] = '#';
if(i > ) res += f(arr,i - ,j,w,h);
if(i < h - ) res += f(arr,i + ,j,w,h);
if(j > ) res += f(arr,i,j - ,w,h);
if(j < w - ) res += f(arr,i,j + ,w,h);
}
return res;
} int main()
{
//freopen("C:\\Users\\wuzhihui\\Desktop\\input.txt","r",stdin); int i,j; int w,h;
//char ch;
while(cin>>w>>h)
{
if(w == && h == ) break;
//cin>>ch; char **arr = new char*[h];
for(i = ; i < h; ++i) arr[i] = new char[w]; for(i = ; i < h; ++i)
{
for(j = ; j < w; ++j)
{
cin>>arr[i][j];
}
//cin>>ch;
} for(i = ; i < h; ++i)
{
for(j = ; j < w; ++j)
{
if(arr[i][j] == '@')
{
arr[i][j] = '.';
cout << f(arr,i,j,w,h) << endl;
break;
}
}
if(j < w) break;
} for(i = ; i < h; ++i) delete [] arr[i];
delete [] arr;
} return ;
}

OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑的更多相关文章

  1. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  2. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  3. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  4. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  5. 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 ...

  6. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. POJ 1979 Red and Black 四方向棋盘搜索

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 50913   Accepted: 27001 D ...

随机推荐

  1. 如何关闭log4j中配置的spring或者hibernate的日志信息

    通常在建立一个web项目的时候,我们通常需要为其配置日志,以便了解启动过程中发生了什么,如果启动过程中发生了错误,则可以很方便的查看错误的信息,但是在项目部署到服务器上时,打印日志信息,需要耗费大量的 ...

  2. SUSE的SSHD配置及设置防火墙

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. 【问题汇总】ListView的FooterView设置可见性的问题

    ListView的FooterView一般用来给用户展示一些提示信息. 正常情况下,是这么使用的.代码例如以下: // footer footerLayout = new PullLoadingLay ...

  4. Java Web services: WS-Security with Metro--referenc

    As you know from "Introducing Metro," the reference implementations of the JAXB 2.x data-b ...

  5. AppScan在项目中的使用流程

    AppScan在项目中的使用流程 http://www.docin.com/p-829022229.html

  6. js解析XML

    //在当前页面内追加换行标签和指定的HTML内容function w( html ){    $(document.body).append("<br/>" + htm ...

  7. Linux系统常见的压缩命令

    *.Z compress 程序压缩的文件: //在当前的Linux主流版本中都已经默认没有支持该压缩命令了,因为gzip命令已经取代了compress命令了 *.gz gzip 程序压缩的文件: *. ...

  8. LogstashL reference 重要章节

    配置文件结构说明 https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html Event相关配 ...

  9. 远程通信Socket

    网络通信高性能的三个主题: 1) 传输:用什么样的通道将数据发送给对方,BIO.NIO或者AIO,IO模型在很大程度上决定了框架的性能: 2) 协议:采用什么样的通信协议,HTTP或者内部私有协议.协 ...

  10. mongodb用mongoose取到的对象不能增加属性

    先定义了一个article的schema var mongoose = require('mongoose'); var Schema = mongoose.Schema; exports.schem ...