HDOJ 1312 (POJ 1979) Red and Black
Problem 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
45
59
6
13
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char d[30][30];
bool vis[30][30];
int str[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,s;
using namespace std;
void dfs(int x,int y){
for(int i=0;i<4;i++){
int xx=x+str[i][0];
int yy=y+str[i][1];
if(xx>=0&&yy>=0&&xx<m&&yy<n&&d[xx][yy]=='.'&&vis[xx][yy]==0){
s++;
// printf("%d\n",s);
vis[xx][yy]=1;
dfs(xx,yy);
}
if(i==3)
return ;
}
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m)&&(n||m)){
for(int i=0;i<m;i++){
scanf("%s",&d[i]);
for(int j=0;j<n;j++){
if(d[i][j]=='@'){
a=i;
b=j;
}
}
}
memset(vis,0,sizeof(vis));
s=1;
d[a][b]='#';
dfs(a,b);
printf("%d\n",s);
}
return 0;
}
HDOJ 1312 (POJ 1979) Red and Black的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- 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解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- 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 ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black 四方向棋盘搜索
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 50913 Accepted: 27001 D ...
随机推荐
- 学习java随笔第九篇:java异常处理
在java中的异常处理和c#中的异常处理是一样的都是用try-catch语句. 基本语法如下 try { //此处是可能出现异常的代码 } catch(Exception e) { //此处是如果发生 ...
- 网页Gzip
网页Gzip压缩检测工具 网站Gzip压缩可以减小服务器带宽占用,提高用户打开网页速度,最多可以提升网站80%的性能,是每个网站必须开启的功能, 站长工具网页 Gzip压缩检测工具方便站长朋友们检测特 ...
- VMWare ESX Server
VMWare ESX Server 4.1 全套下载 [转自:http://www.awolf.net/content/hack/vmware-esx-server-4-1-all-download. ...
- CI 笔记 datagrid的调用,不支持多页面多次调用js
在导航列表中,调用datagrid时,如果用js加载datagrid时,不知为何,报“404错误”找不到网页, 用datagrid时,用网页的形式调用,则没有问题. ----------------- ...
- Maven3(笔记二)
笔记本二 在Eclipse 中使用Maven 第一节:m2eclipse 插件安装 打开Eclipse,点击菜单Help - > Install New Software 点击Add 按钮N ...
- Oracle 的merge into 用法
1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...
- The version of CocoaPods used to generate the lockfile (*) is higher than the version of the current executable (*). Incompatibility issues may arise.
解决方法: sudo gem update cocoapod
- 菜鸟日记之 java中的集合框架
java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...
- npm 好用工具 for 前端
1. caniuse npm install -g caniuse-cmd
- mysql备份和恢复
摘自:http://safe.it168.com/a2009/1108/805/000000805490.shtml 要备份数据库" phpbb_db_backup " #mysq ...