HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312

/*HDU 1312 ----- Red and Black  入门搜索 */
#include <cstdio>

int n, m; //n行m列
int cnt, startx, starty;
char mapp[25][25];

/*找一个连通的区域里可以走的块*/
void dfs(int x, int y){
    if (x < 0 || x >= n || y < 0 || y >= m || mapp[x][y] == ‘#‘)
        return;
    ++cnt;
    mapp[x][y] = ‘#‘; //访问过后直接设置为#可省下一个标记数组visit
    /*递归访问4个方向*/
    dfs(x - 1, y);
    dfs(x + 1, y);
    dfs(x, y - 1);
    dfs(x, y + 1);
}

int main()
{
    //注意题目先给的是列 n行m列
    while (scanf("%d%d", &m, &n) == 2 && (m + n)){
        cnt = 0;
        for (int i = 0; i < n; ++i){
            scanf("%s", mapp[i]);
            for (int j = 0; j < m; ++j){
                if (‘@‘ == mapp[i][j]){
                    startx = i; //记录初始位置
                    starty = j;
                }
            }//for(j)
        }//for(i)
        dfs(startx, starty);
        printf("%d\n", cnt);
    }

    return 0;
}

时间: 2024-12-16 14:55:53

HDU 1312 ----- Red and Black 入门搜索 DFS解法的相关文章

HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u 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

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20820    Accepted Submission(s): 12673 Problem Description There i

HDU 1312 Red and Black 第一题搜索!

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12935    Accepted Submission(s): 8006 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU 1312 Red and Black(基础bfs或者dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11843    Accepted Submission(s): 7380 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

杭电 HDU 1312 Red and Black(超级简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12907    Accepted Submission(s): 7983 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo

hdu 1312 Red and Black(dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char mat[50][50]; int n,m; int ans; int op[4][2]={0,1,0,-1,1,0,-1,0}; bool ok(int x,int y) { if(0<=x&&x<n&&0<=y