POJ 1979 Red and Black(DFS)
来源:互联网 发布:知乎关注最多的话题 编辑:程序博客网 时间:2024/04/29 21:44
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
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.
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)
The end of the input is indicated by a line consisting of two zeros.
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)
The end of the input is indicated by a line consisting of two zeros.
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.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0
Sample Output
4559613
一个铺满黑砖和红砖的房间,一个刚开始站在黑砖上的人,在房间内移动,人只能站在黑砖上,并且每一次只能上下左右走一块,编写程序计算出人可以在这个房间走过多少黑砖。
'.' - a black tile (代表黑砖的位置)
'#' - a red tile (代表红砖的位置)
'@' - a man on a black tile(appears exactly once in a data set)(代表人的位置)
代码如下:
#include <stdio.h>#include <string.h>//n,m:地图的长宽;二维数组(move[ ]) :人每次移动的方向int n,m,num,move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//二维字符数组储存地图char map[25][25];//DFS深度搜索,找到人可以到达的黑砖数void DFS(int i,int j){ int k,x,y; map[i][j]='#'; //循环四次,代表每一点的上下左右 for(k=0;k<4;k++) { x=i+move[k][0]; y=j+move[k][1]; if(x<n&&y<m&&x>=0&&y>=0&&map[x][y]=='.') { DFS(x,y);//沿一条路深搜下去 num++; } }}int main(){ int i,j,x,y; while(scanf("%d%d",&m,&n)) { getchar();//吃掉n后面的回车 if(m==0&&n==0) return 0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='@') { x=i; y=j; } } getchar();//吃掉每行最后的回车 } num=0; DFS(x,y); printf("%d\n",num+1); } return 0;}
0 0
- POJ 1979 Red and Black(DFS)
- poj 1979 Red and Black (DFS)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black (dfs)
- POJ 1979 Red and Black (DFS)
- POJ 1979 Red and Black(dfs)
- POJ 1979-Red and Black(DFS)
- POJ 1979:Red and Black(dfs)
- poj 1979 Red and Black(DFS)
- POJ 1979 Red and Black(DFS)
- 【POJ】-1979-Red and Black(DFS)
- POJ-1979-Red and Black(DFS)
- poj 1979Red and Black(BFS DFS)
- poj 1979 Red and Black ---DFS
- POJ 1979 Red and Black (DFS)
- poj 1979 Red and Black (DFS)
- poj 1979 Red and Black(DFS)
- IntentFilter
- 蓝光的CIGS,混合起来,省钱
- 苹果和甲骨文已经宣布计划OpenJDK的Java实现
- JavaFX战旗类游戏开发 第五课 定时器的应用
- Longest Consecutive Sequence
- POJ 1979 Red and Black(DFS)
- linux 有多个shell
- Eclipse上GIT插件EGIT使用手册
- 苹果开发者账号注册流程
- 获取GIAC GSEC,GSLC,GSSP-Java认证测试
- linux 下 apache2和subverion的通用安装和配置(无权限)
- 获取最新职位通过在线作业分类
- 并查集PID36 / 数石子
- 学习Weblogic和JAVA的一些笔记-Xms Xmx PermSize MaxPermSize的含义