dfs培训列题题解

寻宝

时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
总提交:2            测试通过:2

描述

怀化学院探险小队听说怀化深山区某地依山环水,而聚天地之灵气,吸日月之精华,此地必有大墓,并藏有惊天的保藏。
小队队长费尽心机得到了一份宝藏的地图,打算择良时吉日率领探险小队开始寻宝。
由于地图有些模糊,并且进入之后只能上下左右四个方向移动,其中的巨石不能逾越只能绕道行之。
请你写一程序帮探险小队预测一下能否找到保藏。

输入

第一行两个数n,m 以空格隔开,其中n为行数,m为列数,0<=n<100,0<=m<100。
接下来为n*m大小的二维地图,'.'代表可以行走的空地,'*'代表巨石不能行走,'S'代表入口,'G'代表宝藏。

输出

若能找到宝藏输出YES,否则输出NO。

样例输入

5 5
S....
**.**
...*G
*..*.
.....

样例输出

YES

题解:模板dfs

#include <stdio.h>
#include <iostream>
using namespace std;
int n,m;
int to[4][2]= {-1,0,0,1,1,0,0,-1};
int vis[105][105];
char mapp[105][105];
int flag;
void dfs(int x,int y)
{
    if(mapp[x][y]=='G')
    {
        flag=1;
        return;
    }
    if(flag)
        return;
    vis[x][y]=1;
    for (int i=0;i<4;i++)
    {
        int nx=x+to[i][0];
        int ny=y+to[i][1];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&mapp[nx][ny]=='.'||mapp[nx][ny]=='G')
            dfs(nx,ny);

    }
    vis[x][y]=0;
}
int main ()
{
    cin>>n>>m;
    int sx=0,sy=0;
    for (int i=0; i<n; i++)
    {
        cin>>mapp[i];
        for (int j=0; j<m; j++)
            if(mapp[i][j]=='S')
            {
                sx=i;
                sy=j;
            }
    }
    dfs(sx,sy);
    if(flag)
        printf("YES");
    else
        printf("NO");
    return 0;
}
Lake Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 41967 Accepted: 20819

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

题目大意:有一个n*m的园子,雨后积了水。八连通的积水被认为是连续在一起。求园子里总共有多少水洼?

#include <stdio.h>
#include <iostream>
using namespace std;
int n,m;
char mapp[101][101];
int vis[101][101];
int to[8][2]={-1,0,0,1,1,0,0,-1,-1,-1,-1,1,1,1,1,-1};

void dfs(int x,int y)
{
    vis[x][y]=1;
    for (int i=0;i<8;i++)
    {
        int nx=x+to[i][0];
        int ny=y+to[i][1];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[nx][ny]==0&&mapp[nx][ny]=='W')
            dfs(nx,ny);
    }
}

int main ()
{
    cin>>n>>m;
    for (int i=0;i<n;i++)
        cin>>mapp[i];
    int cnt=0;
    for (int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mapp[i][j]=='W'&&vis[i][j]==0)
            {
                 dfs(i,j);
                 cnt++;
            }
        }
    }

    cout<<cnt;
    return 0;
}

桐桐的全排列

Description 
今天,桐桐的老师布置了一道数学作业,要求列出所有从数字 1 到数字 n 的连续自然数的排列,要求所产生的任一数字 
序列中不允许出现重复的数字。因为排列数很多,桐桐害怕写漏了,所以她决定用计算机编程来解决。 
Input
只有一个整数 n(1≤n≤9)
Output
按字典序输出由 1 n 组成的所有不重复的数字序列,每行一个序列,每个数字之间有一个空格。
SampleInput

Sample Output
12 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1

#include <stdio.h>
#include <iostream>
using namespace std;
int n;
int vis[10],num[10];
void dfs(int id,int step)
{
    if(step==n)
    {
        num[step]=id;
        for (int i=1; i<=n; i++)
            cout<<num[i];
        cout<<endl;
        return;
    }
    vis[id]=1;
    num[step]=id;
    for (int i=1; i<=n; i++)
    {
        if(vis[i]==0)
            dfs(i,step+1);
    }
    vis[id]=0;
}

int main ()
{
    cin>>n;
    dfs(0,0);
    return 0;
}

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 140647    Accepted Submission(s): 37564


Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.
 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

Sample Input
 
 
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
 
 
NOYES

题目大意:在规定的步数从S到达D

题解:dfs+奇偶剪枝

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
char map[9][9];
int n,m,t,x,dx,y,dy,ok;
int a[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y,int time)
{
    int i,ans;
    if(x==dx&&y==dy&&time==t)
        ok=1;
    if(ok)
        return;
    if(x<1||y<1||x>n||y>m)
        return;  //搜索到边界 。
    ans=t-time-abs(dx-x)-abs(dy-y); //奇偶剪枝。
    if(ans<0||ans&1)
        return;
    for(i=0; i<4; i++)
    {
        if(map[x+a[i][0]][y+a[i][1]]!='X')
        {
            map[x+a[i][0]][y+a[i][1]]='X';
            dfs(x+a[i][0],y+a[i][1],time+1);
            map[x+a[i][0]][y+a[i][1]]='.';
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        getchar();
        if(n==0&&m==0&&t==0)
            break;
        for(i=1,k=0; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='S')
                {
                    x=i;
                    y=j;
                }
                if(map[i][j]=='D')
                {
                    dx=i;
                    dy=j;
                }
            }
            getchar();
        }
        ok=0;
        map[x][y]='X';  //标记已经访问过了。
        dfs(x,y,0);
        if(ok==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值