dfs 2016.3.9

1、HDU 1241 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

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

void dfs(int x, int y);

char mat[110][110];
int vis[110][110];

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int m, n;

    while (cin>>m>>n && m!=0) {
        int i, j;
        memset(mat, '*', sizeof(mat));
        memset(vis, 0, sizeof(vis));
        for (i=1; i<=m; ++i) {
            for (j=1; j<=n; ++j) {
                cin>>mat[i][j];
            }
        }
        int count = 0;
        for (i=1; i<=m; ++i) {
            for (j=1; j<=n; ++j) {
                if (!vis[i][j] && mat[i][j]=='@') {
                    ++count;
                    dfs(i, j);
                }
            }
        }
        cout<<count<<endl;
    }
    return 0;
}

void dfs(int x, int y)
{
    if (mat[x][y]=='*' || vis[x][y]) {
        return;
    }
    vis[x][y] = 1;
    dfs(x-1, y-1); dfs(x-1, y); dfs(x-1, y+1);
    dfs(x, y-1);                  dfs(x, y+1);
    dfs(x+1, y-1); dfs(x+1, y); dfs(x+1, y+1);
}

2、HDU 1312 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

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char mat[25][25];
int vis[25][25];
int count = 0;

void dfs(int x, int y);

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int W, H;

    while (cin>>W>>H && !(W==0&&H==0)) {
        memset(mat, '#', sizeof(mat));
        memset(vis, 0, sizeof(vis));
        int i, j;
        int x, y;
        count = 0;
        for (i=1; i<=H; ++i) {
            for (j=1; j<=W; ++j) {
                cin>>mat[i][j];
                if (mat[i][j] == '@') {
                    x = i; y = j;
                }
            }
        }
        dfs(x, y);
        cout<<count<<endl;
    }
    return 0;
}

void dfs(int x, int y)
{
    if (vis[x][y] || mat[x][y]=='#') {
        return;
    }
    vis[x][y] = 1;
    ++count;
    dfs(x-1, y); dfs(x+1, y);
    dfs(x, y-1); dfs(x, y+1);
}

3、UVa 784 Maze Exploration(迷宫探索)

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

char maze[35][85];
int depth;
int len[85];

void dfs(int x, int y);

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int n;

    while (cin>>n) {
        getchar();
        while (n--) {
            memset(maze, '\0', sizeof(maze));
            memset(len, 0, sizeof(len));
            depth = 0;
            int i, j;
            char s[85];
            int x, y;
            while (gets(s)) {
                len[depth] = strlen(s);
                for (i=0; i<len[depth]; ++i) {
                    maze[depth][i] = s[i];
                    if (maze[depth][i] == '*') {
                        x = depth; y = i;
                    }
                }
                if (s[0] == '_') {
                    break;
                }
                ++depth;
            }
            dfs(x, y);
            for (i=0; i<=depth; ++i) {
                for (j=0; j<len[i]; ++j) {
                    cout<<maze[i][j];
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

void dfs(int x, int y)
{
    if (maze[x][y]==' ' || maze[x][y]=='*') {
        maze[x][y] = '#';
        if (x-1 >= 0) {
            dfs(x-1, y);
        }
        if (x+1 < depth) {
            dfs(x+1, y);
        }
        if (y-1 >= 0) {
            dfs(x, y-1);
        }
        if (y+1 < len[x]) {
            dfs(x, y+1);
        }
    }
}

4、POJ 2386 Lake Counting
Description
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (‘.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John’s field, determine how many ponds he has.

Input

Line 1: Two space-separated integers: N and M

Lines 2..N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.

Output
Line 1: The number of ponds in Farmer John’s field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 100 + 5;
int N, M;
char grid[maxn][maxn];
int dir[][2] = {
    {-1, -1}, {-1, 0}, {-1, 1},
    {0, -1},           {0, 1},
    {1, -1},  {1, 0},  {1, 1}
};

void dfs(int x, int y);

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    while (cin>>N>>M) {
        int i, j;
        for (i=0; i<N; ++i) {
            for (j=0; j<M; ++j) {
                cin>>grid[i][j];
            }
        }
        int Count = 0;
        for (i=0; i<N; ++i) {
            for (j=0; j<M; ++j) {
                if (grid[i][j] == 'W') {
                    ++Count;
                    dfs(i, j);
                }
            }
        }
        cout<<Count<<endl;
    }
    return 0;
}

void dfs(int x, int y)
{
    grid[x][y] = '.';
    for (int i=0; i<8; ++i) {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if (0<=nx && nx<N && 0<=ny && ny<M && grid[nx][ny]=='W') {
            dfs(nx, ny);
        }
    }
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;

char s[110][110];
int vis[110][110];

void dfs(int x, int y);

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int N, M;

    while (cin>>N>>M) {
        memset(vis, 0, sizeof(vis));
        memset(s, '.', sizeof(s));
        int i, j;
        for (i=1; i<=N; ++i) {
            for (j=1; j<=M; ++j) {
                cin>>s[i][j];
            }
        }
        int Count = 0;
        for (i=1; i<=N; ++i) {
            for (j=1; j<=M; ++j) {
                if (s[i][j] == 'W' && (!vis[i][j])) {
                    ++Count;
                    dfs(i, j);
                }
            }
        }
        cout<<Count<<endl;
    }

    return 0;
}

void dfs(int x, int y)
{
    if (s[x][y]=='.' || vis[x][y]) {
        return;
    }
    vis[x][y] = 1;
    dfs(x-1, y-1); dfs(x-1, y); dfs(x-1, y+1);
    dfs(x, y-1);                dfs(x, y+1);
    dfs(x+1, y-1); dfs(x+1, y); dfs(x+1, y+1);
}

5、POJ 2245 Lotto
Description
In the German Lotto you have to select 6 numbers from the set {1,2,…,49}. A popular strategy to play Lotto - although it doesn’t increase your chance of winning - is to select a subset S containing k (k > 6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], … [3,5,8,13,21,34].

Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.

Input
The input will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.

Output
For each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.

Sample Input

7 1 2 3 4 5 6 7
8 1 2 3 5 8 13 21 34
0

Sample Output

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 4 5 6 7
1 3 4 5 6 7
2 3 4 5 6 7

1 2 3 5 8 13
1 2 3 5 8 21
1 2 3 5 8 34
1 2 3 5 13 21
1 2 3 5 13 34
1 2 3 5 21 34
1 2 3 8 13 21
1 2 3 8 13 34
1 2 3 8 21 34
1 2 3 13 21 34
1 2 5 8 13 21
1 2 5 8 13 34
1 2 5 8 21 34
1 2 5 13 21 34
1 2 8 13 21 34
1 3 5 8 13 21
1 3 5 8 13 34
1 3 5 8 21 34
1 3 5 13 21 34
1 3 8 13 21 34
1 5 8 13 21 34
2 3 5 8 13 21
2 3 5 8 13 34
2 3 5 8 21 34
2 3 5 13 21 34
2 3 8 13 21 34
2 5 8 13 21 34
3 5 8 13 21 34

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int k;
int S[15];
int vis[20];

void dfs(int pos, int count);

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    while (cin>>k && k!=0) {
        memset(vis, 0, sizeof(vis));
        int i;
        for (i=0; i<k; ++i) {
            cin>>S[i];
        }
        dfs(0, 0);
        cout<<endl;
    }
    return 0;
}

void dfs(int pos, int count)
{
    if (count == 6) {
        int i, j;
        for(i=0; i<k; i++) {
            if(vis[i]) {
                cout<<S[i];
                break;
            }
        }
        for(j=i+1; j<k; j++) {
            if(vis[j]) {
                cout<<" "<<S[j];
            }
        }
        cout<<endl;
        return;
    } else if (count < k) {
        int i;
        for (i=pos; i<k; ++i) {
            if (!vis[i]) {
                vis[i] = 1;
                dfs(i+1, count+1);
                vis[i] = 0;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值