• Codeforces 242C


    在这里插入图片描述在这里插入图片描述

    题目大意:

    首先你有一个109 * 109 大小的棋盘,第一行输入四个数,两两一组分别代表起点和终点,第二行输入一个 n ,接下来n行每行输入3个数r a b ,表示在第 r 行中,第a列到第b列中的格子是可以走的,从起点开始,你可以向八个方向移动,要求输出从起点到终点的最小步数,如果不能到达则输出 -1。

    解题思路:

    求最短步数就是标准的bfs模板,棋盘是1e9 * 1e9 的,用数组存是放不下的,可以借助一个map和pair<int, int>来映射哪些路可以走和哪些路已经走过了,队列中存放x, y, s(步数),这样就可以实现bfs了,套用bfs模板即可。

    Code:

    #pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    const int N = 5050;
    int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, -1, -1, 1, 1, 1, -1, -1};
    map<pii, int> mp;
    map<pii, int> vis;
    struct node { int x, y, s; };
    int x, y, xx, yy, ans = -1;
    void bfs()
    {
    	queue<node > q;
    	q.push({x, y, 0});
    	vis[make_pair(x, y)] = 1;//第一个点已经走过
    	while (!q.empty())
    	{
    		auto t = q.front();
    		q.pop();
    		if (t.x == xx && t.y == yy)
    		{
    			ans = t.s;
    			return;
    		}
    		for (int k = 0; k < 8; k ++)
    		{
    			int nx = t.x + dir[k][0];
    			int ny = t.y + dir[k][1];
    			if (!mp[make_pair(nx, ny)] || vis[make_pair(nx, ny)])//判断是否走过以及该点是否可以走,如果不符合条件则continue;
    				continue;
    			vis[make_pair(nx, ny)] = 1;//标记走过
    			q.push({nx, ny, t.s + 1});//该点入队
    		}
    	}
    	return;
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	cin >> x >> y >> xx >> yy;
    	int n;
    	cin >> n;
    	while (n--)
    	{
    		int r, a, b;
    		cin >> r >> a >> b;//(r, a) -> (r, b) 都是可以走的
    		if (a > b)  swap(a, b);
    		for (int i = a; i <= b; i ++)
    			mp[make_pair(r, i)] = 1;//mp用于存放该点是否可以走,1表示能走
    	}
    	bfs();
    	cout << ans << endl;
    	return 0;
    }
    
  • 相关阅读:
    【Java基础】-switch支持的数据类型
    【微服务】微服务间的通信方式
    MyBatis中 #{}与${}的区别
    exeplorer-损坏的映像【错误状态0xc000012f】
    Java异常-常见的运行时异常和非运行时异常
    jvm-GC集合篇一之GC查找可回收垃圾算法
    JVM-栈的内存结构之--方法返回地址
    JVM-虚拟机栈的内部结构之局部变量表
    jvm-虚拟机栈(java栈)
    JVM-运行时数据区之---PC寄存器
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294168.html
Copyright © 2020-2023  润新知