hud 1312 Red and Black

题目:

链接:点击打开链接

题意:

DFS搜索

算法:

dfs

思路:

简单题

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int w,h;
char s[30][30];
int vis[30][30];
int cnt;

void dfs(int x,int y)
{
    if(s[x][y] == '#' || vis[x][y])
        return ;
    if(x>=h || y>=w || x<0 || y<0)
        return ;
    if(s[x][y] == '@' && vis[x][y] == 0)
    {
        ++cnt;
    }
    if(s[x][y] == '.' && vis[x][y] == 0)
    {
        ++cnt;
        s[x][y] = '@';
    }
    vis[x][y] = 1;
    dfs(x-1,y);
    dfs(x,y-1);
    dfs(x,y+1);
    dfs(x+1,y);
}

int main()
{
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d",&w,&h) != EOF && (w || h))
    {
        memset(vis,0,sizeof(vis));
        memset(s,0,sizeof(s));
        getchar();
        for(int i=0; i<h; i++)
            gets(s[i]);
        cnt = 0;
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                if(s[i][j] == '@' && vis[i][j] == 0)
                {
                    dfs(i,j);
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

hud 1312 Red and Black,布布扣,bubuko.com

时间: 2024-10-20 22:15:06

hud 1312 Red and Black的相关文章

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

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

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

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水题)

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(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

HDOJ 1312 Red and Black

题目链接: 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): 10724    Accepted Submission(s): 6716 Problem Description There is a rectangul

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

杭电 1312 red and black

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