HDU 1312 -- Red and Black(dfs)
来源:互联网 发布:闹钟提醒软件 编辑:程序博客网 时间:2024/04/29 20:21
题目大意:一个房间,有红色的格子,黑色的格子,黑色的格子’.‘可以走,红色的格子不可以走,问一个人站在一个点’@‘最多能走多少个格子。
思路分析:从这个人开始,用dfs,搜索以这个点为根节点的整棵树,找到一个子节点,用来计数的cnt就加1;其实可以设立一个visit数组,初始化为0,用来判断以这个节点为根节点的那些节点被访问过,被访问过的结点就说明从这个根节点能到达这个点,该点的visit设为1,计算所有visit里为1的点的个数就好,这样就不用再在dfs递归的过程中记录结点的个数,会节省时间。
#include<stdio.h>#include<string.h>const int maxn=25;char room[25][25];int dir[4][2]={1,0,-1,0,0,1,0,-1};int cnt,h,w;void dfs(int row,int col){ ++cnt; room[row][col]='#'; int x,y; for(int i=0;i<4;i++){ x=row+dir[i][0]; y=col+dir[i][1]; if(room[x][y]!='#'&&x>=0&&x<h&&y>=0&&y<w) dfs(x,y); }}int main(){ while(~scanf("%d%d",&w,&h)&&(w||h)){ int a,b; cnt=0; for(int i=0;i<h;i++){ getchar(); for(int j=0;j<w;j++){ scanf("%c",&room[i][j]); if(room[i][j]=='@'){ a=i; b=j; } } } dfs(a,b); printf("%d\n",cnt); } return 0;}
0 0
- HDU 1312 -- Red and Black(dfs)
- HDU - 1312 Red and Black (dfs)
- hdu 1312Red and Black(DFS)
- HDU 1312 Red and Black(DFS)
- hdu 1312 Red and Black(dfs)
- HDU 1312 Red and Black(DFS)
- hdu 1312 Red and Black (dfs)
- 【HDU】1312--Red and Black(DFS)
- Red and Black (dfs)【HDU】-1312
- HDU 1312Red and Black(dfs)
- hdu 1312 Red and Black(dfs入门)
- hdu 1312 Red and Black dfs
- hdu 1312 Red and Black (简单dfs)
- hdu 题目1312 Red and Black ( DFS )
- HDU--1312 -- Red and Black [简单DFS]
- HDU 1312 Red and Black(BFS,DFS)
- HDU 1312 Red and Black (DFS入门)
- hdu 1312 Red and Black (bf、dfs)
- CSS 第一课
- linux学习笔记一(linux发展史及基本命令)
- shell编程入门之shell介绍
- 接口--interface
- JAVA菜鸟入门篇 - 继承VS组合、final关键字 (十六)
- HDU 1312 -- Red and Black(dfs)
- maven安装问题
- 普通平衡树 离散化
- 【J2SE快速进阶】——向上转型及其内存分析
- Ubuntu通过有线路由器上网
- Qt元对象表机制与MFC消息映射机制
- Vs2013在Linux开发中的应用(36):Milestone-0.1.0
- [数学][第二阶段-简单数学题][HDOJ-2057]进制转换
- NSURLSession 教程