HDU - 1312 - Red and Black
2015-07-22 12:47
483 查看
题意:给你一个二维数组,@是人的起始位置,‘#’代表红色,不能走,’.’代表黑色可以走,输出能走多的方格的个数,@起始位置也算一个。题意说明只有上下左右四个方向,就深搜这四个方向。
[code]#include<stdio.h> #include<string.h> #define MAXN 22 char s[MAXN][MAXN]; int vis[MAXN][MAXN]; int w, h, ans; int move[4][2]={{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; void DFS(int x, int y){ for(int i = 0; i < 4; i++){ int tx = x + move[i][0]; int ty = y + move[i][1]; if(tx >= 0 && tx < h && ty >= 0 && ty < w && s[tx][ty]=='.' && !vis[tx][ty]){ vis[tx][ty] = 1; ans++; DFS(tx, ty); } } } int main() { while(scanf("%d%d", &w, &h) && w+h){ ans = 0; for(int i = 0; i < h; i++) scanf("%s", s[i]); memset(vis, 0, sizeof(vis)); for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++){ if(s[i][j]=='@'){ vis[i][j] = 1; ans++; DFS(i, j); } } } printf("%d\n", ans); } return 0; }
相关文章推荐
- eclipse 自动包含头文件
- 极客学院java 线程代码笔记1
- WebKit的一些笔记3(基础篇)
- NOJ 1064 快速排序
- shell几种字符串加密解密的方法
- 跨工厂物料状态/特定工厂的物料状态
- BZOJ 1037 [ZJOI2008]生日聚会Party
- android dialog 有关token的问题
- 整套spark视频教程免费下载,还有Hadoop
- mybatis的mapper.xml 小结
- Github上的几个C++开源项目
- ROS images和OpenCV images之间的转化
- Linux的top命令详细解析
- JavaScript检测上传文件大小的方法
- windows在ISR中不能睡眠的原因
- 在 Eclipse 上开发 Python – PyDev 插件安装教程
- android 实现代码混淆
- extjs4 treepanel checkbox 复选框 全选完美解决
- 硬盘安装Ubuntu
- 关于安卓输入法的设置 使输入法不会遮挡到主界面