Red and Black

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13508 Accepted Submission(s): 8375

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)

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

题意:

n*m的方阵有红格或是黑格,只能走黑格

每次只能走上下左右四个紧邻方向的格子,求

这个人最后能走多少个黑格子。

分析:

dfs水题。从第一个黑格子开始递归的搜索,

每次搜索一个黑格子后为了以后不再重复走

这个黑格子,就把当前搜索的这个黑格子换

成红格子,然后继续dfs。。。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

题目大意:一个长方形空间,上面铺红色和黑色瓦片,一个人起初站在黑色瓦片上,每次可以走到相邻的4个黑色瓦片上,输入数据,求其能走过多少瓦片

题意:某人在@处为起点(也包括@点)#为墙,点(.)为通路,问最多能走多远统计能走几个点(加上@这个点)

思路:用dfs;

代码:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char a[30][30];
int ss,n,m;//这3个值需要用全局变量
int b[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
int dfs(int x,int y)
{
int xx,yy;
if(x<0||y<0||x>=m||y>=n)
return 0;
int i;
for(i=0; i<4; i++)
{
xx=x+b[i][0];
yy=y+b[i][1];
if(xx<0||yy<0||xx>=m||yy>=n||a[xx][yy]=='#')
//检查该点上下左右的点是否符合题目要求。
continue;
ss++;
a[xx][yy]='#';
//如果该点已经检查过,就把它变成'#',防止再次被检查。
dfs(xx,yy);
}
}
int main()
{ while(~scanf("%d%d",&n,&m)&&(n||m))//n,m不能同时为0
{
int i,j;
int pi,pj;
getchar();//吸收换行符。
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='@')
{
pi=i;
pj=j;
}
}
getchar();//吸收换行符。
}
a[pi][pj]='#';
ss=1;
dfs(pi,pj);
printf("%d\n",ss);
}
return 0;
}

HDOJ 1312题Red and Black的更多相关文章

  1. HDOJ 1312 (POJ 1979) Red and Black

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

  2. Hdoj 1312.Red and Black 题解

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

  3. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

  4. HDOJ 1312 DFS&BFS

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. HDOJ 1004题 Let the Balloon Rise strcmp()函数

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  6. HDOJ 1237题 简单计算器

    简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. HDOJ 1013题Digital Roots 大数,9余数定理

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  8. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

  9. BFS && DFS

    HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...

随机推荐

  1. Javascript 汉字转拼音

    调用方式: var pinyin = convert("欢迎光临"); alert(pinyin); 新建JS文件:PYConvert.js,内容如下: var PinYin = ...

  2. [Cache] C#操作缓存--CacheHelper缓存帮助类 (转载)

    点击下载 CacheHelper.zip CacheHelper 缓存帮助类 C#怎么操作缓存 怎么设置和取缓存数据,都在这个类里面呢 下面看一下代码吧 /// <summary> /// ...

  3. 给右键 添加dos命令

    reg add "HKEY_CURRENT_USER\Console" /v "ScreenBufferSize" /t REG_DWORD /d 655361 ...

  4. windows服务启动 1053错误

    1.问题描述 今天在启动一个Windows服务时,服务启动不了,且提示:1053错误 那么是什么导致了1053错误呢? 2.他山之石 百度了一下,发现有人作出下面的解释并给出了解决方法: “常常是因为 ...

  5. Lucene索引的初步创建

    从百度上知道的,Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的 ...

  6. 手机定位原理 - GPS/GLONASS/北斗 + WIFI + 基站

    卫星定位系统 - GPS/GLONASS/北斗: 关于GPS.GLONASS.北斗.伽利略系统的科普请自行谷歌. GPS是使用最广泛的全球定位网络,几乎是所有智能手机的标配.进几年,俄罗斯的GLONA ...

  7. (转)Android开发:性能最佳实践-管理应用内存

    翻自:http://developer.android.com/training/articles/memory.html 在任何软件开发环境中,RAM都是宝贵的资源,但在移动操作系统中更加珍贵.尽管 ...

  8. Java实现单向链表

    /* 先定义一个Node类用来存储节点的值域和指针域 * 即当前节点中的值和后面节点的方法 * 在C中就是相当与定义一个结构体类型一个数据域和指针域的方法 */class LNode{//这个写法已经 ...

  9. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller

    ASP.NET MVC中的Model(数据模型)主要包括定义数据结构.数据库读写.数据验证等等和对象处理相关的工作. 在解决方案资源管理器中找到Model文件夹,点击右键,添加一个新类,名为“Mess ...

  10. Android模拟器常用命令收录

    一.Linux命令 1.挂载/systme分区为读写状态 mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system 2.切换为Root用户 ...