POJ 1979 Red and Black
2014-07-27 17:23
183 查看
题目大意:
有一块地由H行×W列砖块组成(H, W ≤ 20),砖块有红黑两种颜色,红用'#'表示,黑用'.'表示,人起始时再一块黑砖上(起始黑砖用'@'表示),砖块的布局输入时会以H×W的矩阵形式给出,只包含'#'、'.'、'@'三种符号,其中'@'只有一个,人只能沿上下左右四个方向走,并且只能走黑砖,问该人能到达的所有黑砖总数(也包括起始位置)。
现有多个测例,测例以H, W = 0结束,对于每个测例求出人能到达的所有黑砖的个数。
题目链接
注释代码:
有一块地由H行×W列砖块组成(H, W ≤ 20),砖块有红黑两种颜色,红用'#'表示,黑用'.'表示,人起始时再一块黑砖上(起始黑砖用'@'表示),砖块的布局输入时会以H×W的矩阵形式给出,只包含'#'、'.'、'@'三种符号,其中'@'只有一个,人只能沿上下左右四个方向走,并且只能走黑砖,问该人能到达的所有黑砖总数(也包括起始位置)。
现有多个测例,测例以H, W = 0结束,对于每个测例求出人能到达的所有黑砖的个数。
题目链接
注释代码:
/* * Problem ID : POJ 1979 Red and Black * Author : Lirx.t.Una * Language : C * Run Time : 0 ms * Run Memory : 148 KB */ #include <stdio.h> //最大宽度 #define MAXW 20 //存放矩阵,用字符串存放,要留以为给'\0' char m[MAXW][MAXW + 1]; int stp;//step,步数,即可以到达的黑砖的数量 int w, h;//列数和行数 void dfs( int y, int x ) {//朝四个方向深搜 stp++; m[y][x] = '#';//找到就标记成已经搜索过,之后就不要重复计算了 if ( x - 1 >= 0 && '.' == m[y][x - 1] ) dfs( y, x - 1 ); if ( x + 1 < w && '.' == m[y][x + 1] ) dfs( y, x + 1 ); if ( y - 1 >= 0 && '.' == m[y - 1][x] ) dfs( y - 1, x ); if ( y + 1 < h && '.' == m[y + 1][x] ) dfs( y + 1, x ); } int main() { int i, j;//计数变量 int find;//用于找'@'的位置 while ( scanf("%d%d", &w, &h), w ) { stp = 0; for ( i = 0; i < h; i++ ) scanf("%s", m[i]); find = 0; for ( i = 0; i < h; i++ ) { for ( j = 0; j < w; j++ ) if ( '@' == m[i][j] ) { find = 1; break; } if ( find ) break; } dfs( i, j ); printf("%d\n", stp); } return 0; }无注释代码:
#include <stdio.h> #define MAXW 20 char m[MAXW][MAXW + 1]; int stp; int w, h; void dfs( int y, int x ) { stp++; m[y][x] = '#'; if ( x - 1 >= 0 && '.' == m[y][x - 1] ) dfs( y, x - 1 ); if ( x + 1 < w && '.' == m[y][x + 1] ) dfs( y, x + 1 ); if ( y - 1 >= 0 && '.' == m[y - 1][x] ) dfs( y - 1, x ); if ( y + 1 < h && '.' == m[y + 1][x] ) dfs( y + 1, x ); } int main() { int i, j; int find; while ( scanf("%d%d", &w, &h), w ) { stp = 0; for ( i = 0; i < h; i++ ) scanf("%s", m[i]); find = 0; for ( i = 0; i < h; i++ ) { for ( j = 0; j < w; j++ ) if ( '@' == m[i][j] ) { find = 1; break; } if ( find ) break; } dfs( i, j ); printf("%d\n", stp); } return 0; }
相关文章推荐
- zoj 2165||poj 1979 red and black (dfs)
- POJ 1979 Red and Black
- poj 1979 , zoj 2165 Red and Black【dfs,入门题】
- poj 1979 red and black
- POJ-1979-Red and Black
- poj 1979 Red and Black
- poj 1979 zoj 2165 Red and Black 深搜做法
- zoj 2165 Red and Black (DFs)poj 1979
- POJ 1979 Red and Black(C语言堆栈实现)
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 Red and Black 解题报告
- POJ_1979 Red and Black 迷宫类
- POJ 1979 Red and Black
- POJ1979 Red and Black dfs基础题
- POJ 1979 Red and Black(BFS广度优先搜索)
- poj1979——Red and Black
- poj 1979 Red and Black
- poj 1979 Red and Black
- poj 1979 zoj 2165 Red and Black