您的位置:首页 > 其它

dfs算法练习

2017-07-30 20:43 274 查看

1. Find The Multiple ( POJ 1426)

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.


Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.


Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.


Sample Input

2
6
19
0


Sample Output

10
100100100100100100
111111111111111111


主要题意:

给定一个正整数n,写一个程序以找出一个非零的m,其十进制表示只包含数字0和1.你可以假设n不大于200,并且有一个对应的不超过100位的十进制数m。


#include <iostream>
using namespace std;
int flag;  //标记是否已经找的,没有的话程序会在找到后持续执行
void dfs(long long y, int k, int num){
if(flag || num >= 19)  //递归不超过19层,即long long型所存数字最大位数,没有该条件会数字溢出
return ;
if(y % k == 0){  //判断是否整除
flag = 1;
cout << y << endl;
return ;
}

dfs(y * 10, k, num+1);
dfs(y * 10 +1, k, num+1);
}

int main(void)
{
int n;
while(cin >> n && n){
flag = 0;
dfs(1, n, 0);
}

return 0;
}


2. Oil Deposits (HDU 1241)

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
*
35
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0


Sample Output

0
1
2
2


主要题意:

相邻(包括横、竖、斜向等八个方向)油田被视作一个整体,需要遍历整个地图来寻找整体都不相邻的油田个数。


#include <iostream>
#include <string.h>
#include <memory.h>

char a[101][101];
int book[101][101];
int count; //记录油田个数
int n, m;
int flag;

void dfs(int x, int y, int num){  //num用来判断是否为同一油田,值为0时表示新油田刚找到,此时 count 加 1
int next[8][2]={{0,1}, {1,1},
{1,0}, {1,-1},
{0,-1}, {-1,-1},
{-1,0}, {-1,1} };  //八个方向

int tx, ty;
if(x < 0 || x >= n || y < 0 || y >= m || book[x][y])  //判断是否已经走过或超出地图范围
return;

if(!num && a[x][y] == '@')
count++;

book[x][y] = 1;//标记已经走过
for(int i = 0; i < 8; i++){
tx = x + next[i][0]; //下一步位置
ty = y + next[i][1]; //下一步位置

if(a[tx][ty] == '@' && !book[tx][ty]){
dfs(tx, ty, num+1);
}
}
}

int main(int argc, char const *argv[])
{
while(cin >> n >> m && n && m){
memset(a, 0, sizeof(a));  //初始化为0
memset(book, 0, sizeof(book));
count = 0;

for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> a[i][j];

for(int i = 0; i < n; i++)  //遍历地图
for(int j = 0; j < m; j++)
if(a[i][j] == '@')
dfs(i, j, 0);

cout << count << endl;
}

return 0;
}


3. Prime Ring Problem(HDU 1016)

Problem Description

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~n构成一个环,要求相邻两数字之和为质数,且每组数第一个数都是1。


#include <iostream>
#include <math.h>
using namespace std;

int n;
int a[20] = {1}; //第一个数字为1,直接存入数组
int book[20];

/* 判断是否为质数 */
int isPrime(int a){
int i;
for(i = 2; i <= sqrt(a) && a % i; i++)
;
if(i > sqrt(a))
return 1;
return 0;
}

void dfs(int num){
if(num == n && isPrime(a[num-1] + 1) && isPrime(a[num-1] + a[num-2])){  //额外判断最后一个数与第一个数之和是否为质数
for( int i = 0; i < n; i++){
if(i == n-1)
cout << a[i];
else
cout << a[i] << ' ';
}
cout << endl;
return ;
}

for(int i = 2 ; i <= n; i++){
if(!book[i] && isPrime(a[num-1] + a[num - 2]) ){
book[i] = 1;
a[num] = i;
dfs(num + 1);
book[i] = 0;
}
}
}

int main(void)
{
int cas = 0;
while(cin >> n){
cout << "Case " << cas + 1 << ":" << endl;
dfs(1);
cout << endl;
cas++;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 dfs
相关文章推荐