cf Ping-Pong (Easy Version)

本文解析了CodeForces上的一道B级题目,通过深度优先搜索(DFS)算法解决区间覆盖问题。代码示例清晰地展示了如何判断两个区间是否相交以及如何遍历所有可能的路径来寻找解决方案。

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

http://codeforces.com/contest/320/problem/B

这道题看了很长时间没看懂, 就是个dfs;

 1 #include <cstdio>
 2 #include <cstring>
 3 #define maxn 2000
 4 using namespace std;
 5 
 6 int a[maxn],b[maxn];
 7 bool vis[maxn];
 8 int cnt=1;
 9 
10 bool dfs(int x,int y)
11 {
12     vis[x]=true;
13     if(x==y) return 1;
14     for(int i=1; i<=cnt; i++)
15     {
16         if(!vis[i]&&((a[i]<a[x]&&a[x]<b[i])||(a[i]<b[x]&&b[x]<b[i])))
17         {
18             if(dfs(i,y)) return true;
19         }
20     }
21     return false;
22 }
23 
24 int main()
25 {
26     int n,x,y,c;
27     scanf("%d",&n);
28     memset(vis,false,sizeof(vis));
29     for(int i=1; i<=n; i++)
30     {
31         scanf("%d%d%d",&c,&x,&y);
32         if(c==1)
33         {
34             a[cnt]=x; b[cnt++]=y;
35         }
36         else
37         {
38             memset(vis,false,sizeof(vis));
39             if(!dfs(x,y)) printf("NO\n");
40             else printf("YES\n");
41         }
42     }
43     return 0;
44 }
View Code

 

转载于:https://www.cnblogs.com/fanminghui/p/3668920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值