博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】
阅读量:4456 次
发布时间:2019-06-08

本文共 4348 字,大约阅读时间需要 14 分钟。

C    Oil Deposits

Problem Description
The 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
#include
#define N 110char str[N][N];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 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. 
 

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
 
题意:从'@'开始,最多能够走过多少个'.',输入0,0结束输入。水DFS。

 
#include
#include
#define N 30char str[N][N];int book[N][N];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;}

转载于:https://www.cnblogs.com/hellocheng/p/7350099.html

你可能感兴趣的文章
数字及字符串
查看>>
【转载】OmniGraffle (二)基础绘图和模具
查看>>
一些提高开发效率的 Category
查看>>
拓扑排序基础题——排序
查看>>
转:iphone 申请证书
查看>>
Python就业方向
查看>>
一步步学习SPD2010--第二章节--处理SP网站(3)--创建网站层次架构
查看>>
TCP
查看>>
Excel常用函数大全
查看>>
团队-团队编程项目中国象棋-模块测试过程
查看>>
10个经典的C语言面试基础算法及代码
查看>>
[概念] js的函数节流和throttle和debounce详解
查看>>
普通的java Ftp客户端的文件上传
查看>>
视图系统
查看>>
Palindromes _easy version
查看>>
vue 小记
查看>>
CURRICULUM VITAE
查看>>
菱形缓冲器电路
查看>>
08多态
查看>>
Groovy 程序结构
查看>>