深搜广搜专题【DFS】【BFS】

深搜广搜专题

又是一年专题时,这次的专题是BFS和DFS,我们刚加入acm时的噩梦,然而现在已经写起来很舒服了(OS:那你还A不出题?)

BFS和DFS都是通过对所有的点进行遍历来得到结果的,是一种比较暴力的方法,在平时较难的题目中一般不会出现。

 


A - Oil Deposits

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 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

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,将与其相邻的油都归属到同一个油田中,并将其重新标号为*,防止重复。

AC代码

//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <string>
#include <list>
#include <algorithm>
#include <set>
#include <list>
#include <stack>
//#include <map>
#include <bitset>
//#define INF 0x3f3f3f3f
//#define MAX 1e9
//#define N 1000005
#define ll long long
#define G 0
#define R 1
#define P 2
#define MOD 1000000007

using namespace std;

int n, m;
char map[101][101];

void dfs(int x, int y)
{
	map[x][y] = '*';
	int xt, yt;
	int next[8][2] = { {1,0},{0,1},{0,-1},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1} };
	for(int i=0;i<8;i++)
	{
		xt = x + next[i][1];
		yt = y + next[i][0];
		if (xt >= m || xt<0 || yt >= n || yt < 0)continue;
		if (map[xt][yt] == '*')continue;
		dfs(xt, yt);
	}
}

int main()
{
	while(~scanf("%d%d",&m,&n))
	{
		getchar();
		if (!m)break;
		for (int i = 0; i < m; i++)
		{
				scanf("%s", &map[i]);
		}
		int count = 0;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if(map[i][j]=='@')
				{
					count++;
					dfs(i, j);
				}
			}
		}
		printf("%d\n", count);
	}
	//system("PAUSE");
	return 0;
}

 

 


D - Prime Ring Problem 

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. 

Note: the number of first circle should always be 1. 

 

Input

n (0 < n < 20). 

Output

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order. 

You are to write a program that completes above process. 

Print a blank line after each case. 

Sample Input

6
8

Sample Output

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

题意

给我们一个环,从数字1开始,每两个相邻的数之和必须是一个质数,要你将可能的数按字典序升序输出。

思路

质环问题,我们对每个点进行枚举,判断该点和上一个点之间是否满足题目,若不满足则及时剪枝,避免浪费时间,需要注意的是我们当判断到最后一个点时需要将其与第一个点进行求和并求其是否是一个质数。对于本题来说最大数字之间的和不超过40,我们可以通过手动打出质数表来判断一个数是否是质数。

AC代码


                
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值