网络寻路(dfs或者枚举)

这个题一开始用了dfs,因为蓝桥杯的数据太水了,然后就过了。但是之前做过类似的题,用枚举来做的,然后就是可以避免超时
//dfs

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map> 
#include <sstream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct node
{
    int to,pre;
};
int h=0,ans=0;
node e[200010];
int head[10005];
void add(int a,int b)
{
    e[h].to=b;e[h].pre=head[a];head[a]=h;
    h++;
}
int vis[10005];
int start=0;
void dfs(int bg,int time1)
{
    if(time1==3){
        ans++;
        return ;
    }
    for(int i=head[bg];i>-1;i=e[i].pre)
    {
        node t=e[i];
        if(time1==2&&t.to==start)
         {
          ans++;
          continue;}
        if(!vis[t.to])
        {
            vis[t.to]=1;
            dfs(t.to,time1+1);
            vis[t.to]=0;
        }
    }
}
int main(int argc, char *argv[]) {
    int n,m;
    scanf("%d %d",&n,&m);
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        add(a,b);
        add(b,a);
    }
    for(int i=1;i<=n;i++)
    {
        start=i;
        memset(vis,0,sizeof(vis));
        vis[i]=1;
        dfs(i,0);
    }
    printf("%d\n",ans);
    return 0;
}

//枚举

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map> 
#include <sstream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct node
{
    int to,pre;
};
set<pair<int ,int > > t;
int h=0,ans=0;
node e[200010];
int head[10005];
void add(int a,int b)
{
    e[h].to=b;e[h].pre=head[a];head[a]=h;
    h++;
}
int start=0;
int main(int argc, char *argv[]) {
    int n,m;
    scanf("%d %d",&n,&m);
    memset(head,-1,sizeof(head));
    for(int i=0;i<m;i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        add(a,b);
        add(b,a);
        t.insert(pair<int ,int > (a,b));
    }
    int sum=0;
    set<pair<int ,int > > ::iterator it;
    for(it=t.begin();it!=t.end();it++)
    {   
        pair< int,int > temp=*it;
        int a=temp.first,b=temp.second;//这个.first不要记错啦啦啦
        int ans1=0,ans2=0;
        for(int i=head[a];i>-1;i=e[i].pre)
             if(e[i].to!=b) ans1++;
        for(int i=head[b];i>-1;i=e[i].pre)
             if(e[i].to!=a) ans2++;
        sum+=ans1*ans2;
    }
    printf("%d\n",sum*2);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用深度优先搜索(DFS)实现机器人寻路程序的示例代码: ```python # 定义迷宫地图和机器人起点 maze = [ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0] ] start = (1, 1) # 定义 DFS 函数 def dfs(maze, start, visited): if start in visited: return False if maze[start[0]][start[1]] == 1: return False if maze[start[0]][start[1]] == 2: print("找到终点:", start) return True visited.append(start) directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] for d in directions: next_pos = (start[0] + d[0], start[1] + d[1]) if dfs(maze, next_pos, visited): return True return False # 调用 DFS 函数 dfs(maze, start, []) ``` 在上述代码中,我们首先定义了一个迷宫地图和机器人的起点。然后,我们定义了一个 `dfs` 函数来实现 DFS 算法。该函数接收三个参数:迷宫地图、机器人起点和已访问过的位置列表。在每个位置,我们检查该位置是否已被访问过、是否为墙壁或终点。如果是终点,我们就输出该位置并返回 True。否则,我们将该位置添加到已访问过的位置列表中,并继续遍历该位置的上下左右四个方向。如果没有找到终点,我们就返回 False。 最后,我们调用 `dfs` 函数,并将已访问过的位置列表初始化为空列表 `[]`。如果机器人能够找到终点, `dfs` 函数将返回 True,否则返回 False。在本例中,机器人能够从起点 (1, 1) 找到终点 (3, 3)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值