【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】
2017-07-29 14:54
239 查看
C Oil Deposits
Problem DescriptionThe GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous
square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits
can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <=
m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100
pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题意:输出有多少个连续的'@',单独的'@'算作一个。水DFS。
#include<stdio.h> #include<string.h> #define N 110 char str ; int n, m,count; void dfs(int x,int y) { int k[8][2] = {0,1,-1,1,-1,0,-1,-1,0,-1,1,-1,1,0,1,1}; int i; int tx,ty; for(i = 0; i < 8; i ++) { tx = x+k[i][0]; ty = y+k[i][1]; if(tx<0||ty<0||tx>n-1||ty>m-1) continue; if(str[tx][ty] == '@') { str[tx][ty] = '*'; dfs(tx,ty); } } return; } int main() { int i,j; while(scanf("%d%d",&n,&m),m!=0) { count = 0; memset(str,0,sizeof(str)); for(i = 0;i < n; i ++) scanf("%s",str[i]); for(i = 0; i < n; i ++) { for(j = 0; j < m; j ++) { if(str[i][j] == '@') { count++; dfs(i,j); } } } printf("%d\n",count); } return 0; }
D Red and Black
Problem DescriptionThere 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.
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)
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
45
59
6
13
题意:从'@
da0f
'开始,最多能够走过多少个'.',输入0,0结束输入。水DFS。
#include<stdio.h> #include<string.h> #define N 30 char str ; int book ; int n,m,step; void dfs(int x,int y) { int k[4][2] = {0,1,-1,0,0,-1,1,0}; int i; int tx,ty; for(i = 0; i < 4; i ++) { tx = x + k[i][0]; ty = y + k[i][1]; if(tx < 0||ty < 0||tx > n-1||ty>m-1) continue; if(str[tx][ty]!='#'&&book[tx][ty]!=1) { book[tx][ty] = 1; step++; dfs(tx,ty); } } return; } int main() { int i,j; while(scanf("%d%d",&m,&n),(m+n)!=0) { step = 1; memset(book,0,sizeof(book)); for(i = 0; i < n; i ++) scanf("%s",str[i]); for(i = 0; i < n; i++) { for(j = 0; j < m; j ++) { if(str[i][j] == '@') { book[i][j] = 1; dfs(i,j); } } } printf("%d\n",step); } return 0; }
相关文章推荐
- 专题 简单搜索(bfs+dfs) 个人题解
- NYOJ1058+NYOJ1282 部分和问题(深度优先搜索DFS入门题)
- 【算法入门】广度/宽度优先搜索(DFS)
- 搜索专题(DFS&&BFS&&剪枝)HDU 1728-逃离迷宫
- 搜索入门之dfs--经典的迷宫问题解析
- HDU 1016 Prime Ring Problem(搜索入门题 DFS,多种方式)
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
- DFS专题(入门,经典为主)
- 搜索专题训练【CSUST_Newer_12级入门】
- 【搜索入门专题1】E - Farm Irrigation 【BFS】ZOJ 2412
- [kuangbin]专题一 简单搜索 A - 棋盘问题(dfs 回溯)
- poj2531--Network Saboteur(搜索练习7-dfs或随机算法)
- DFS-BFS搜索专题【经典训练题】【有时间一个个做下来】
- BZOJ 4809: 皇后 (n皇后问题)dfs搜索入门
- 搜索专题训练【CSUST_Newer_12级入门】
- dfs入门练习一
- 搜索入门(DFS)-Lake Counting(积水连通问题)
- 搜索专题(DFS)HDU1181-变形课
- 【搜索入门专题1】 hdu1242 J - Rescue c++ stl容器之优先队列+BFS
- 【算法入门】广度/宽度优先搜索(DFS)