AtCoder Beginner Contest 132 E - Hopscotch Addict

bfs

位置+状态

just need to calculate min value(only it is useful), so O(1*x)

挺有趣的一道题。。。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 #define ll long long
10
11 const int maxn=1e6+10;
12
13 struct node
14 {
15     int d;
16     node *to;
17 }*e[maxn];
18
19 int qx[maxn*3],qy[maxn*3],qz[maxn*3];
20
21 int f[maxn][3];
22
23 int main()
24 {
25     node *p;
26     int n,m,x,y,z,xx,yy,zz,head,tail,s,t;
27     scanf("%d%d",&n,&m);
28     while (m--)
29     {
30         scanf("%d%d",&x,&y);
31         p=new node();
32         p->d=y;
33         p->to=e[x];
34         e[x]=p;
35     }
36
37     scanf("%d%d",&s,&t);
38     qx[1]=s,qy[1]=0,qz[1]=3;
39     head=0,tail=1;
40     while (head<tail)
41     {
42         head++;
43         x=qx[head];
44         y=qy[head];
45         z=qz[head];
46
47         p=e[x];
48         while (p)
49         {
50             xx=p->d;
51             yy=(y+1)%3;
52             zz=z+1;
53             if (!f[xx][yy])
54             {
55                 if (xx==t && yy==0)
56                 {
57                     printf("%d",(zz-3)/3);
58                     return 0;
59                 }
60                 tail++;
61                 qx[tail]=xx;
62                 qy[tail]=yy;
63                 qz[tail]=zz;
64                 f[xx][yy]=zz;
65             }
66             p=p->to;
67         }
68     }
69     printf("-1");
70     return 0;
71 }

原文地址:https://www.cnblogs.com/cmyg/p/11108069.html

时间: 2024-11-05 10:35:55

AtCoder Beginner Contest 132 E - Hopscotch Addict的相关文章

AtCoder Beginner Contest 132 解题报告

前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (c

AtCoder Beginner Contest 132 F - Small Products

数 sqrt 缩小范围 整除分块 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 1

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E