dfs培训列题题解

寻宝

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

描述

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

输入

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

输出

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

扫描二维码关注公众号,回复: 2620972 查看本文章

样例输入

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;
}


猜你喜欢

转载自blog.csdn.net/zero_zp/article/details/80207655
今日推荐