dfs算法练习

来源:互联网 发布:夏普网络电视机 编辑:程序博客网 时间:2024/06/05 23:04

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

26190

Sample Output

10100100100100100100111111111111111111

主要题意:

    给定一个正整数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

0122

主要题意:

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

68

Sample Output

Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 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;}
原创粉丝点击