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-1406】密码箱 约数 + 乱搞 + set?
1406: [AHOI2007]密码箱 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1143 Solved: 677[Submit][Status][ ...
- HDU2096 小明A+B
入门级都没到的水题!看到顺便就做了,AC记录喜+1 Description 小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能够进行100以内的非负整数的加法计算. 对于大于等于100 ...
- java时间库Joda-Time
虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...
- hihocoder1187 Divisors
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...
- c++实现gray code(格雷码)
今天别人问的一道题,强调用分治法实现 =.= 百度了一下格雷码,然后写了一下. 关于格雷码大家看百度的吧,特别详细,贴个图: 代码如下(header_file.h是我自己写的一个头文件,包括常见的ve ...
- auto,register,static实例
#include <stdio.h>int main() { auto int i = 0; register int j = 0; static int k = 0; ...
- TFS 配置 报表权限问题
通过[项目门户网站]访问,却被提示ReportService权限不足时, 提示:处理报表时出错. (rsProcessingAborted) 对数据集 “dsiteration” 执行查询失败 同样 ...
- ucenter实现原理
其实Ucenter实现同步登陆的原理就是cookie,一个应用登陆成功之后,向Ucenter传递数据(post方式),让Ucenter通知其他的应用也设置 cookie(get方式),这样用户在访问其 ...
- Bootstrap新手学习笔记——css
Css模块: 1.网格系统: class前缀:.col-xs-*,.col-sm-*,.col-md-*,.col-lg-* <div class="container"&g ...
- SQL 基本语句
1.修改sa账户密码 在查询分析器中执行如下语句: sp_password Null,'teracypwd','sa' 把SA的密码设为"teracypwd" 执行成功后有&quo ...