Faulty Robot (dfs)两种不同的写法

本文介绍了一种计算机课程项目中,学生Alice编程控制机器人探索带强制移动规则的图的问题。机器人可能因错误而偏离预设路径,文章提供了两种解决该问题的代码实现方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Faulty Robot

As part of a CS course, Alice just finished programming her robot to explore a graph having nn nodes, labeled 1,2,…,n1,2,…,n, and mm directed edges. Initially the robot starts at node 11.

While nodes may have several outgoing edges, Alice programmed the robot so that any node may have a forced move to a specific one of its neighbors. For example, it may be that node 55 has outgoing edges to neighbors 11, 44, and 66 but that Alice programs the robot so that if it leaves 55 it must go to neighbor 44.

If operating correctly, the robot will always follow forced moves away from a node, and if reaching a node that does not have a forced move, the robot stops. Unfortunately, the robot is a bit buggy, and it might violate those rules and move to a randomly chosen neighbor of a node (whether or not there had been a designated forced move from that node). However, such a bug will occur at most once (and might never happen).

Alice is having trouble debugging the robot, and would like your help to determine what are the possible nodes where the robot could stop and not move again.

We consider two sample graphs, as given in Figures 1 and 2. In these figures, a red arrow indicate an edge corresponding to a forced move, while black arrows indicate edges to other neighbors. The circle around a node is red if it is a possible stopping node.

\includegraphics[width=0.6\textwidth ]{faulty}

Figure 1: First sample graph.

\includegraphics[width=0.3\textwidth ]{faulty2}

Figure 2: Second sample graph.

In the first example, the robot will cycle forever through nodes 11, 55, and 44 if it does not make a buggy move. A bug could cause it to jump from 11 to 22, but that would be the only buggy move, and so it would never move on from there. It might also jump from 55 to 66 and then have a forced move to end at 77.

In the second example, there are no forced moves, so the robot would stay at 11without any buggy moves. It might also make a buggy move from 11 to either 22 or 33, after which it would stop.

Input

The first line contains two integers nn and mm, designating the number of nodes and number of edges such that 1≤n≤1031≤n≤103, 0≤m≤1040≤m≤104. The next mm lines will each have two integers aa and bb, 1≤|a|,b≤n1≤|a|,b≤n and |a|≠b|a|≠b. If a>0a>0, there is a directed edge between nodes aa and bb that is not forced. If aa < 0, then there is a forced directed edge from −a−a to bb. There will be at most 900900 such forced moves. No two directed edges will be the same. No two starting nodes for forced moves will be the same.

Output

Display the number of nodes at which the robot might come to a rest.

Sample Input 1Sample Output 1
7 9
1 2
2 3
-1 5
2 6
5 1
-4 1
5 6
-6 7
-5 4
2 
Sample Input 2Sample Output 2
3 2
1 2
1 3
3

 

读懂题意很重要(英语很重要)

第一种:把边和点都封装好,再来按正规的dfs操作

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;

struct edge
{
    int head,tail;
    int color;
};
vector <edge> node[10005];//edge a[1000][1000];相当于这个
int vis[10005];
int cnt;
int bug;
int ans[10005];
void dfs(int n)
{
    int flag=0;
    for(int i=0;i<node[n].size();i++)//枚举顶点n的每一条边
    {

        if(node[n][i].color && bug)//black
        {
            if(vis[node[n][i].tail]) continue;//访问过了,不要放外面,放外面会把红边给pass掉
          bug=0;
          vis[node[n][i].tail]=1;
          dfs(node[n][i].tail);
          vis[node[n][i].tail]=0;
          bug=1;
        }
        else if(node[n][i].color==0)//red
        {
            flag=1;
            if(vis[node[n][i].tail]) continue;//访问过了,不要放外面,放外面会把红边给pass掉
            vis[node[n][i].tail]=1;
            dfs(node[n][i].tail);
            vis[node[n][i].tail]=0;
        }
    }
    if(flag==0)//这个点没有红边,可以stop
        {cnt++;ans[n]=1;}

}
int main()
{
    int n,m,x;
    edge t;
    cnt=0;
    bug=1;
    memset(vis,0,sizeof(vis));
    memset(ans,0,sizeof(ans));
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&t.tail);
        if(x>0)
            t.color=1;//black
        else
            t.color=0;//red
        t.head=abs(x);
        node[t.head].push_back(t);//把输入的边加入到节点中
    }
    vis[1]=1;
    dfs(1);
    //printf("%d\n",cnt);
    //cnt里面会有重复的点
    int tt=0;
    for(int i=1;i<=n;i++)
        if(ans[i]) tt++;
        printf("%d\n",tt);
    return 0;
}

第二种:充分理解题意,利用题目的各种条件剪枝,代码简洁一些

#include<bits/stdc++.h>
using namespace std;

int all[1005][1005];
int visit[1005];
int cannot[1005];

int n;
void dfs(int i,int flag,int time)
{
    int j;
    if(flag==2) return;
    if(time>visit[i]) return;
    if(time>900) return;
    visit[i]=time;
    for(j=1; j<=n; j++)
    {
        if(all[i][j]==2)
            dfs(j,flag,time+1);
        if(all[i][j]==1)
            dfs(j,flag+1,time+1);
    }
}
int main ()
{
    int m;
    int i,j,ans;
    scanf("%d %d",&n,&m);
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            all[i][j]=0;
        }
        visit[i]=99999;
        cannot[i]=0;
    }
    int a,b;
    while(m--)
    {
        scanf("%d%d",&a,&b);
        if(a>0)
        {
            all[a][b]=1;//black
        }
        if(a<0)
        {
            all[-a][b]=2;//red
            cannot[-a]=1;//首先把red边删除
        }
    }
    dfs(1,0,0);
    ans=0;
    for(i=1; i<=n; i++)
        if(visit[i]<=900&&cannot[i]==0)
            ans++;
    printf("%d\n",ans);
    return 0;
}

然而这两份代码我都不是我写的。。。。先记着吧。。。感觉写的不错。。。OvO.....

<think>好的,我现在要处理用户关于H3C华三交换机电源模块故障的问题。首先,我需要回顾用户提供的系统级指令,确保回答符合要求。用户需要结构清晰的解答,并且引用了站内内容,所以我要注意在适当的位置添加引用标识。 首先,用户的问题是关于电源模块显示faulty状态的排查和修复。我需要分步骤来引导用户解决问题,同时确保每一步都详细且易懂。根据之前提供的引用,用户可能已经了解了一些关于IRF设备ID修改的方法,但当前问题与电源模块有关,所以需要专注于电源部分。 接下来,我需要考虑常见的电源故障原因。通常,电源模块故障可能由电源线松动、模块接触不良、设备供电问题、模块过热或模块本身损坏引起。因此,我应该将这些可能的原因分步骤列出,并提供相应的解决方法。例如,检查电源线连接、重新安装模块、验证供电电压等。 同时,要确保回答中包含正确的命令,如display power,以便用户检查电源状态。另外,可能需要指导用户如何更换备用模块,并提醒他们在操作时注意安全,比如断电处理。 还要注意引用之前提到的display irf命令,虽然那是在修改IRF设备ID时的步骤,但可能在电源模块状态确认时,相关的设备状态检查命令也有帮助。不过,当前问题更直接相关的应该是电源相关的命令,如display power,所以重点应放在那里。 在生成相关问题部分,需要涵盖故障诊断、更换步骤、冗余电源配置、日志查看以及联系技术支持等方面,以帮助用户全面了解可能涉及的后续步骤。 最后,确保整个回答符合中文语法,使用正确的LaTeX格式(如果有数学表达式的话,但这个问题可能不需要),并且结构清晰,分步骤说明,引用正确的位置。需要检查是否有遗漏的步骤,比如固件升级或环境检查,这些也是常见的解决手段。 总结下来,回答的结构应该是:确认故障状态→检查物理连接→验证供电→检查温度→更换模块→固件检查→联系支持。每个步骤简明扼要,并附上相关命令和注意事项。最后的问题部分要覆盖用户可能进一步想了解的内容。</think>### H3C华三交换机电源模块故障排查与修复方法 当H3C交换机电源模块出现"faulty"状态时,可按照以下步骤进行排查: **1. 确认故障状态** 使用命令查看具体告警信息: ```shell display power ``` 输出结果可显示各电源模块状态(Normal/Faulty)、输入电压、输出功率等信息[^1]。 **2. 检查物理连接** - 确认电源线两端(模块端和供电端)连接牢固 - 检查电源模块与交换机背板的金手指接触是否良好 - 尝试将电源模块重新插拔安装(需在安全下电条件下操作) **3. 验证供电环境** - 使用万用表测量供电电压是否符合规格(交流模块:100-240V;直流模块:-48V/-60V) - 检查PDU/配电柜对应断路器是否跳闸 - 测试更换供电线路(重要设备建议采用双路供电) **4. 检查运行环境** - 测量机柜温度是否超过40℃ - 确认模块散热风扇运转正常 - 检查电源模块进风口是否有异物堵塞 **5. 模块替换测试** - 如有冗余电源配置,可尝试主备模块互换 - 使用同型号备用模块进行替换测试 - 注意更换时需佩戴防静电手环 **6. 固件版本检查** ```shell display version ``` 对比官网最新版本,必要时升级电源模块固件。 **7. 收集诊断信息** ```shell display diagnostic-information ``` 该命令会生成包含电源状态详细日志的打包文件,可用于技术分析。 若上述步骤无法解决问题,建议联系H3C技术支持,提供设备序列号和诊断文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值