CF-242-C bfs+stl

挺不错的一道搜索题,由于数据范围大,所以用stl中的set来标记是否可走以及是否走过。

其他的就是利用bfs的性质找最短路了。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <set>
 6 using namespace std;
 7
 8 typedef pair<int, int> pii;
 9 set<pii> s;
10 queue< pair<pii, int> > q;
11 int dx[] = { 0, 0, 1, -1, 1, 1, -1, -1 };
12 int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
13
14 int main ()
15 {
16     int x0, y0, x1, y1;
17     while ( scanf("%d%d%d%d", &x0, &y0, &x1, &y1) != EOF )
18     {
19         s.clear();
20         while ( !q.empty() ) q.pop();
21         int t;
22         scanf("%d", &t);
23         while ( t-- )
24         {
25             int r, a, b;
26             scanf("%d%d%d", &r, &a, &b);
27             for ( int k = a; k <= b; k++ )
28             {
29                 s.insert( make_pair( r, k ) );
30             }
31         }
32         q.push( make_pair( make_pair( x0, y0 ), 0 ) );
33         int ans = -1;
34         while ( !q.empty() )
35         {
36             int x = q.front().first.first;
37             int y = q.front().first.second;
38             int step = q.front().second;
39             q.pop();
40             if ( x == x1 && y == y1 )
41             {
42                 ans = step;
43                 break;
44             }
45             for ( int i = 0; i < 8; i++ )
46             {
47                 pii tmp( x + dx[i], y + dy[i] );
48                 if ( s.find( tmp ) == s.end() ) continue;
49                 s.erase(tmp);
50                 q.push( make_pair( tmp, step + 1 ) );
51             }
52         }
53         printf("%d\n", ans);
54     }
55     return 0;
56 }
时间: 2024-12-14 23:32:00

CF-242-C bfs+stl的相关文章

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

luogu题解P1032字串变换--BFS+STL:string骚操作

题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的string居然变得这么简洁!!! 各种string操作请看另一位大佬博客,写得很全啊: https://www.cnblogs.com/rvalue/p/7327293.html#commentform 其实我们这题只用到两个相关函数:\(S.find(string,pos)\)和\(S.substr()\

CF 19C 思维题STL应用

http://codeforces.com/problemset/problem/19/C Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that s

bfs+STL cf242c

1 #include <iostream> 2 #include <map> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 map<int, map<int,int> >mp; 9 queue<int> q; 10 11 int main() 12 { 13 int x0,y0,x1,y1,n; 14 while(scanf(&quo

CF 2A Winner(STL+模拟)

题目链接:http://codeforces.com/contest/2/problem/A 题目: The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points,

CF19D 线段树+STL各种应用

http://codeforces.com/problemset/problem/19/D Description Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point (0,?0) is located in the bottom-left corner, Ox axis

POJ 1915-Knight Moves (单向BFS &amp;&amp; 双向BFS 比较)

题目链接:Knight Moves 研究了一下双向BFS,不是很难,和普通的BFS一样,双向BFS不过是从 起点和终点同时开始搜索,可减少搜索时间 当两条搜索路线相遇时,结束. 貌似有一年百度的招聘 笔试,就是双向BFS.... 下面,比较一下BFS 和 双向BFS的用时: BFS STL的queue可能会浪费一点时间 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstrin

[C++]那些年被虐的STL

首先很感谢P1135奇怪的电梯 [2.14补充:此题已被AC!然后将被我花式虐[From语]哈哈哈哈哈哈哈哈哈哈好嗨哟感觉人生已经到达了巅峰感觉人生已经到达了高潮]这道题了!在做这道题的我大致就是下图qwq: dfs->sp->bfs->stl 于是小蒟蒻准备写一篇博客整理一下STL库啦! 祭出大佬@From 廿八--初六每日更新 目录: vector 封装数组 #### 向量(vector) 连续存储的元素 set 封装二叉树[集合] 集合(set) 由节点组成的红黑树,每个节点都包含

coderforces Gym 100803A/Aizu 1345/CSU 1536/UVALive 6832 Bit String Reordering(未完)

Portal: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1345  http://codeforces.com/gym/100803/attachments  A题 好题! 坑不多,切入比较难 一开始的想法是暴力,由于求得是最小解且此图太大无边界,所以不能DFS,首先想到BFS 解法1 BFS+STL queue 1 #include<iostream> 2 #include<algorithm> 3 #in