HDOJ 1241 Oil Deposits【最大连通块 dfs】

来源:互联网 发布:6603棋牌 数据库 编辑:程序博客网 时间:2024/05/29 16:44

HDOJ 1241 Oil Deposits【最大连通块 dfs】

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1241


输入那里很烦。。。


#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int MAXL = 105; // 矩阵最大宽度char maze[MAXL][MAXL]; // 迷宫int vis[MAXL][MAXL]; // 访问标记(坐标点所在连通块编号)int n, m; // 迷宫的长和宽int dirx[8] = {0, 1, 0, -1, 1, 1, -1, -1};int diry[8] = {1, 0, -1, 0, 1, -1, -1, 1}; // 移动方向typedef struct point{    int x, y;}P; // 坐标void Output(){    for(int i = 0; i < n; i++){        for(int j = 0; j < m; j++){            printf("%c", maze[i][j]);        }        printf("\n");    }}void Input(){    memset(maze, 0, sizeof(maze));    memset(vis, 0, sizeof(vis));    for(int i = 0; i < n; i++){            scanf("%s", &maze[i]);        }    //Output();}void dfs(int x, int y, int cnt){    if(x < 0 || x >= n || y < 0 || y >= m) return; // 越界 返回    if(maze[x][y] == '*' || vis[x][y] != 0) return; // 没有油或已经访问过 返回    vis[x][y] = cnt;    for(int i = 0; i < 8; i++){        dfs(x+dirx[i], y+diry[i], cnt); // 遍历邻接点    }}int Sum(){    int cnt = 0; // 连通块的数量    for(int i = 0; i < n; i++){        for(int j = 0; j < m; j++){            if(maze[i][j] == '@' && vis[i][j] == 0){ // 有油并且没有标记过                dfs(i, j, ++cnt);            }        }    }    return cnt;}int main(){    while(~scanf("%d%d", &n, &m) && n && m){        Input();        printf("%d\n", Sum());    }    return 0;}
0 0
原创粉丝点击