从入门到入坑 2020-01-31 12:09 采纳率: 100%
浏览 506
已采纳

hdu1312 深搜水题,大佬帮忙看看为什么A不了?

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

#include <cstdio>
#include <iostream>
using namespace std;
char a[22][22];
int total = 0;
void dfs(int x, int y);
int main()
{
    int w, h;
    while(1)
    {
        int x=0,y=0;
        scanf("%d %d",&w,&h);
        if(w==0 && h==0)
            break;
        for(int i=0; i<h; i++)
        {
            scanf("%s",&a[i]);
            getchar();
        }
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                if(a[i][j]=='@')
                {
                    x=i;
                    y=j;
                }
            }
        }
        total = 1;
        dfs(x,y);
        printf("%d\n",total);
    } 
    return 0;
}
void dfs(int x, int y)
{
    a[x][y] = '#';
    if(a[x-1][y]=='.')
    {
        total++;
        dfs(x-1,y);
    }
    if(a[x+1][y]=='.')
    {
        total++;
        dfs(x+1,y);
    }
    if(a[x][y-1]=='.')
    {
        total++;
        dfs(x,y-1);
    }
    if(a[x][y+1]=='.')
    {
        total++;
        dfs(x,y+1);
    }
    return;
}
  • 写回答

1条回答 默认 最新

  • 橘猫九只胖 2020-02-01 11:54
    关注

    上下左右移动判断一下是否越界就可以啦~

    #include<iostream>
    #include<cstring>
    #include<sstream>
    #include<string>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    char a[30][30];
    int total = 0;
    int w, h;
    void dfs(int x, int y)
    {
        a[x][y]='#';
        total++;
        if(a[x-1][y]=='.'&&(x-1)>=0&&(x-1)<h&&y<w&&y>=0)
        {
            dfs(x-1,y);
        }
        if(a[x+1][y]=='.'&&(x+1)<h&&(x+1)>=0&&y<w&&y>=0)
        {
            dfs(x+1,y);
        }
        if(a[x][y-1]=='.'&&(y-1)>=0&&(y-1)<w&&x<h&&x>=0)
        {
            dfs(x,y-1);
        }
        if(a[x][y+1]=='.'&&(y+1)<w&&(y+1)>=0&&x<h&&x>=0)
        {
            dfs(x,y+1);
        }
    }
    
    int main()
    {
        while(1)
        {
            int x=0,y=0;
            scanf("%d %d",&w,&h);
            memset(a,0,sizeof(a));
            if(w==0 && h==0)
                break;
            total=0;
            for(int i=0; i<h; i++)
            {
                scanf("%s",&a[i]);
                getchar();
            }
            int flag=0;
            for(int i=0; i<h; i++)
            {
                for(int j=0; j<w; j++)
                {
                    if(a[i][j]=='@')//i是第几行,j是第几列
                    {
                        x=i;
                        y=j;
                        dfs(x,y);
                        flag=1;
                        break;
                    }
                }
                if(flag){//如果找到了就可以停止搜索了
                    break;
                }
            }
            printf("%d\n",total);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改