PO3083 DFS+BFS

迷宫寻路策略
本文介绍了一种迷宫寻路算法,包括通过始终沿左侧或右侧墙壁行走的策略,以及使用BFS寻找最短路径的方法。
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12992 Accepted: 5612

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########


题意:给定一个迷宫,要求从S走到E,分别输出“只能向前进方向左拐”“只能向前进方向右拐”“最短路径”三种路径长度。

思路:前两种是DFS的加强改版,后一种是裸BFS。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct ss
{
    int x,y,sn;
};
bool vis[50][50];
char a[50][50];
int n,m,xs,xe,ys,ye;
int bfsp[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int dfspl[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int dfspr[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int bfs()
{
    bool vis[50][50];
    memset(vis,false,sizeof(vis));
    queue<ss>q;
    ss t0;
    t0.x=xs;
    t0.y=ys;
    t0.sn=1;
    q.push(t0);
    vis[xs][ys]=true;
    while(!q.empty())
    {
        ss t=q.front();
        q.pop();
        if(t.x==xe&&t.y==ye)
            return t.sn;
        for(int i=0;i<4;i++)
        {
            ss tt;
            tt.x=t.x+bfsp[i][0];
            tt.y=t.y+bfsp[i][1];
            tt.sn=t.sn+1;
            if(tt.x<=n&&tt.x>=1&&tt.y<=m&&tt.y>=1&&!vis[tt.x][tt.y]&&a[tt.x][tt.y]!='#')
            {
                vis[tt.x][tt.y]=true;
                q.push(tt);
            }
        }
    }
    return 0;
}
int dfs(int x,int y,int d,int step,int path[][2])
{
    for(int i=0;i<4;i++)
    {
        int j=((d-1+4)%4+i)%4;
        int xx=x+path[j][0];
        int yy=y+path[j][1];
        if(xx==xe&&yy==ye)
            return step+1;
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]!='#')
            return dfs(xx,yy,j,step+1,path);
    }
}
int main()
{
    //freopen("D://in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&m,&n);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
                if(a[i][j]=='S')
                {
                    xs=i;
                    ys=j;
                }
                if(a[i][j]=='E')
                {
                    xe=i;
                    ye=j;
                }
            }
        }
        int d1,d2;
        if(xs==0)
            d1=3,d2=3;
        else if(xs==n-1)
            d1=1,d2=1;
        else if(ys==0)
            d1=2,d2=0;
        else if(ys==m-1)
            d1=0,d2=2;
        printf("%d %d %d\n",dfs(xs,ys,d1,1,dfspl),dfs(xs,ys,d2,1,dfspr),bfs());
    }
    return 0;
}


一门武功能否传承久远并被发扬光大,是要看缘分的。一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱…… 直到某一支的某一代突然出现一个天分特别高的弟子(或者是吃到了灵丹、挖到了特别的秘笈),会将功夫的威力一下子放大N倍 —— 我们称这种弟子为“得道者”。 这里我们来考察某一位祖师爷门下的徒子徒孙家谱:假设家谱中的每个人只有1位师傅(除了祖师爷没有师傅);每位师傅可以带很多徒弟;并且假设辈分严格有序,即祖师爷这门武功的每个第i代传人只能在第i-1代传人中拜1个师傅。我们假设已知祖师爷的功力值为Z,每向下传承一代,就会减弱r%,除非某一代弟子得道。现给出师门谱系关系,要求你算出所有得道者的功力总值。 输入格式: 输入在第一行给出3个正整数,分别是:N(≤10 5 )——整个师门的总人数(于是每个人从0到N−1编号,祖师爷的编号为0);Z——祖师爷的功力值(不一定是整数,但起码是正数);r ——每传一代功夫所打的折扣百分比值(不超过100的正数)。接下来有N行,第i行(i=0,⋯,N−1)描述编号为i的人所传的徒弟,格式为: K i ​ ID[1] ID[2] ⋯ ID[K i ​ ] 其中K i ​ 是徒弟的个数,后面跟的是各位徒弟的编号,数字间以空格间隔。K i ​ 为零表示这是一位得道者,这时后面跟的一个数字表示其武功被放大的倍数。 输出格式: 在一行中输出所有得道者的功力总值,只保留其整数部分。题目保证输入和正确的输出都不超过10 10 。 输入样例: 10 18.0 1.00 3 2 3 5 1 9 1 4 1 7 0 7 2 6 1 1 8 0 9 0 4 0 3 输出样例: 404 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB,#include<bits/stdc++.h> using namespace std; using pis = pair<int,string>; int main(){ int n,k; double z,r; cin>>n>>z>>r; vector<vector<int>> g(n); vector<int> fa(n,-1); unordered_map<int,int> strongers; for(int i=0;i<n;i++){ cin>>k; if(k==0){ int po; cin>>po; strongers[i] = po; }else{ int k; cin>>k; for(int j=0;j<k;j++){ int t; cin>>t; g[i].push_back(t); fa[t] = i; } } } int root = -1; for(int i=0;i<n;i++){ int f = fa[i]; if(f==-1){ root = i; } } double ans = 0; auto dfs = [&](auto&& dfs,int i,int fa,double curPower)->void{ if(strongers[i]!=0){ ans += curPower * strongers[i]; } for(auto& j:g[i]){ if(j!=fa){ dfs(dfs,j,i,curPower * (1-r)); } } }; dfs(dfs,root,-1,z); cout<<ans<<endl; return 0; }对于这题我的代码有什么问题
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值