DFS:Red and Black(POJ 1979)

题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间?
不多说,DFS深搜即可,水题
注意一下不要把行和列搞错就好了,我就是那样弄错过一次哈哈哈哈
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 20 static int dp[MAX_N][MAX_N];
static int startx;
static int starty;
static int ans; void DFS_Search(const int, const int, const int, const int); int main(void)
{
int W, H, i, j;
while (~scanf("%d%d", &W, &H))
{
if (W == && H == )
break;
ans = ;//先算起点的
for (i = ; i < H; i++)//Read Map
{
getchar();
for (j = ; j < W; j++)
{
scanf("%c", &dp[i][j]);
if (dp[i][j] == '@'){
startx = i; starty = j;
}
}
}
DFS_Search(startx, starty, W, H);
printf("%d\n", ans);
} return ;
} void DFS_Search(const int x, const int y, const int W, const int H)
{
dp[x][y] = '#';
ans++;
if (x - >= && dp[x - ][y] != '#')
DFS_Search(x - , y, W, H);
if (x + < H && dp[x + ][y] != '#')
DFS_Search(x + , y, W, H);
if (y - >= && dp[x][y - ] != '#')
DFS_Search(x, y - , W, H);
if (y + < W && dp[x][y + ] != '#')
DFS_Search(x, y + , W, H);
}
DFS:Red and Black(POJ 1979)的更多相关文章
- Red and Black(poj 1979 bfs)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27891 Accepted: 15142 D ...
- 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 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+染色)
-->Red and Black Descriptions: 有个铺满方形瓷砖的矩形房间,每块瓷砖的颜色非红即黑.某人在一块砖上,他可以移动到相邻的四块砖上.但他只能走黑砖,不能走红砖. 敲个程 ...
- POJ 1979 Red and Black【DFS】
标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
随机推荐
- 【BZOJ-2295】我爱你啊 暴力
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 202 Solved: 141[Submit][St ...
- BZOJ2186 欧拉函数
欧拉函数:一般记作φ(n),表示1-n中与n互质的数的数量. 欧拉函数是积性函数,即φ(m*n)=φ(m)*φ(n) //这条定理基友面试时还遇到了= = 欧拉函数的值φ(n)=n*(1-p[1])* ...
- [IOS Delegate和协议]
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/25655443 作者:小马 代理和协议的语法这里不赘述,自己查资料. 这个demo的 ...
- Eclipse学习总结(02)-动态项目部署到到本地Tomcat
一.发现问题 在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并 ...
- Bootstrap教程:[4]栅格系统详解
http://jingyan.baidu.com/article/6f2f55a1852aa1b5b83e6c5a.html 们都知道bootstrap3.0使用了四种栅格选项来形成栅格系统,这四种选 ...
- linux3
第一课:date +%Y-%m-%d 显示日期date +%H:%M 显示小时分钟date 显示日期 vi /etc/sysconfig/network-scripts/ifcfg-eth0 网卡配置 ...
- 初学JDBC,最简单示例
一.下载相应数据库驱动jar包,添加到项目中 二.注册驱动,数据库驱动只加入到classpath中是还不行的,还要在使用的时候注册一下,就像安装驱动软件,只拷贝到硬盘还不行,需要安装一下 Driver ...
- KxMenu下拉菜单
+ (void)createMenu:(id)sender target:(UIViewController *)t { NSArray *menuItems = @[ [KxMenuItem men ...
- Python2.x与Python3.x的区别
这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...
- apache-tomcat-5.5.35.搭建实战
tar xf apache-tomcat-5.5.35.tar -C /usr/local/ 需要安装JDK支持,可借鉴:http://www.cnblogs.com/zhoulf/archive/2 ...