1504061021-hd-Red and Black

Red and Black

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

Total Submission(s): 11542    Accepted Submission(s): 7185

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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

解题思路

这是一道最基础的深搜,也是朋友教给我的,以前都是不懂。

深搜就是从一个点向上下左右四个方向拓展,然后再以拓展的点为基础向上下左右拓展判断。

所以需要两个数组,bianx[4]和biany[4]来存储x和y的对应变化,一定要注意x、y的变话要对照。

然后还需要判断地点有意义,即在范围之内1<=x<=n&&1<=y<=m

解题代码

#include<cstdio>
#include<cstring>
#include<iostream>//
using namespace std;//C++要加这两个头文件
char map[22][22];
int bx[5]={0,1,0,-1};
int by[5]={1,0,-1,0};
int sum;
int n,m;
bool judge(int a,int b)
{
	if(a<1||a>m||b<1||b>n)
	    return false;
	else
	{
		if(map[a][b]!='#')
		    return true;
		else
		    return false;
	}
}
void dfs(int a,int b)
{
	int i;
	int nowa,nowb;
	sum++;
	map[a][b]='#';
	for(i=0;i<4;i++)
	{//对定点进行上下左右四种变化
		nowa=a+bx[i];
		nowb=b+by[i];
		if(judge(nowa,nowb))
		    dfs(nowa,nowb);
		//直接递归调用就好
		else
		    continue;
	}
}
int main()
{
	//int n,m;
	int i,j,k;
	int stax,stay;
	while(scanf("%d%d",&n,&m),n+m)
	{
		memset(map,0,sizeof(map));
		for(i=1;i<=m;i++)
		    for(j=1;j<=n;j++)
		    {
		    	cin>>map[i][j];
		    	if(map[i][j]=='@')
		    	{
		    		stax=i;
		    		stay=j;
		    	}
		    }
		sum=0;
		dfs(stax,stay);
		cout<<sum<<endl;
		//若输出需要换行,则加上<<endl
	}
	return 0;
}
时间: 2024-12-23 05:37:28

1504061021-hd-Red and Black的相关文章

Android IntentService使用

因为多数启动服务不必同时处理多个请求(在多线程情景下会很危险),所以使用IntentService类实现服务是很好的选择.本经验将通过继承IntentService输出当前时间教大家如何使用IntentService. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

自动安装red hat enterprise linux

第一单元 一 kickstart 概述 1.使用kickstart ,系统管理员可以创建一个包含安装期间所有常见问题的答案的文件,以自动安装red hat enterprise linux 2.kickstart 类似于 oracle solaris 中的jumpstart 或 microsoft windows 的无人值守安装 二 kickstart 制作工具安装 1 软件包 :system-config-kickstart 2 安装 : yum install system-config-k

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

Linux red hat 安装ansible

今日对Linux 系统是Red Hat Enterprise Linux Server release 6.5 (Santiago)对ansible进行安装. 由于系统的源为yum源,所以使用yum install ansible 进行安装,但是报错.如图.(这个错误是yum源没有注册到red hat 系统). yum源不能安装,所以换了一个思路.使用pip安装.pip是依赖python安装的. 1.检查Python版本 Python -v 检查出来为Python 2.6.6 2.检查pip 版

red hat7 系统可以ping通ip地址但是不能ping通域名

在red hat7中ifconfig后出现这样的情况,ens33是物理网卡,与eth0一样只是不同的名字.但是只能ping通ip地址不能ping通域名. 解决方法: 在文件 /etc/resolv.conf文件下更改nameserver的值 加上 nameserver 8.8.8.8  域名解析服务  nameserver 8.8.4.4为备用的 在文件 etc/sysconfig/network-scripts下修改 ifcfg-eth0 或者 ifcfg-ens33文件,设置静态ip地址,

red halt 7.3使用Cent-os7 yum 源

困惑:在使用red halt学习时,未注册的用户,有些软件是无法通过yum网络源和本地光盘下载,此时使用Centos的yum源就显得很方便,因为红帽收购了Centos 所以软件的兼容性基本不会有什么问题 1.检查是否安装yum包 查看RHEL是否安装了yum,若是安装了,那么又有哪些yum包: [[email protected] yum.repos.d]# rpm -qa |grep yumyum-3.4.3-118.el7.noarchyum-utils-1.1.31-24.el7.noar

BZOJ1419: Red is good

1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 639  Solved: 247[Submit][Status][Discuss] Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. Input 一行输入两个数R,B,其值在0到5000之间 Output 在最优策略下平均能得到多少钱

POJ1979 Red and Black

问题链接:POJ1979 Red and Black. 题意简述:输入正整数w和h,w为列数,h为行数.输入h×w矩阵 (1 <= h <= 20; 1 <= w <= w),其中'.'代表可到达,'#'代表不可到达,'@'代表开始点.问从'@'开始可以到达最多多少个点. 问题分析:本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大.需要注意的是'@'也算一个可以到达的点. 程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即

Red style books store OpenCart 主题模板 ABC-0117

Red style books store OpenCart 主题模板 ABC-0117 RED STYLE BOOKS STORE OPENCART 主题模板 ABC-0117 Designed with luxurious colorsSuitable for shop selling: Books , templates, Card, picture, Art, Photography..Designed by CSS, HTMLSupports multiple languages, m

HDU1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9732    Accepted Submission(s): 6060 Problem Description There is a rectangular room, covered with square tiles. Each tile is color