BFS学习 Codeforces 301_div.2_Ice Cave

本文介绍了一款计算机游戏中,玩家需要通过遍历一个多级冰洞地图来寻找出口的问题。地图由整块和裂开的冰构成,玩家只能移动到相邻的格子,且一旦走过某格,该格会变成裂开状态。文章提供了使用广度优先搜索解决这一问题的代码实现。

C. Ice Cave
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.

The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.

Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.

Output

If you can reach the destination, print 'YES', otherwise print 'NO'.

链接:http://codeforces.com/contest/540/problem/C

这题就是bfs,感觉有点区别的地方有陷阱,一般的地方踩过一次之后也会变成陷阱

#include<iostream>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<cstring>
#include<sstream>
#include<cstdlib>
#include<map>
#include<deque>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
typedef pair<int, int> P; 
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define Mant 0x3f3f3f3f
#define Mint -0x3f3f3f3f
#define N 510
int dx[]={ 1, -1, 0, 0 };  
int dy[]={ 0, 0, 1, -1 };  

int n,m;
int r1,c1,r2,c2;
char graph[N][N];
 
bool bfs(){
	queue<P> q;
	q.push(P(r1,c1));
	graph[r1][c1]='X'; //第一步上去冰块已碎 
	while(!q.empty()){
		r1=q.front().first; c1=q.front().second; q.pop(); 
		for(int i=0;i<4;i++){
			int xx=r1+dx[i];
			int yy=c1+dy[i];
			if( xx<0 || xx>=n || yy<0 || yy>=m) continue;
			if(graph[xx][yy]=='X'){  //到达点肯定也碎 
				if(xx==r2 && yy==c2) return true;
				continue;
			}
			graph[xx][yy]='X'; //走了之后就变吃呢个易碎的冰块 
			q.push(P(xx,yy));
		}
	}
	return false;
} 
 
int main(){
	while(~scanf("%d %d",&n,&m)){
		for(int i=0;i<n;i++)
		    scanf("%s",graph[i]);
        scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
        r1--;c1--;r2--;c2--;
        printf("%s\n",bfs()? "YES" : "NO");
	}
	return 0;
}


#include <queue> #include <vector> #include <unordered_set> #include <costmap_2d/cost_values.h> #include "common/structure/node.h" #include "path_planner/graph_planner/jastar_planner.h" namespace rmp { namespace path_planner { /** * @brief Construct a new AStar object * @param costmap the environment for path planning * @param dijkstra using diksktra implementation * @param gbfs using gbfs implementation */ J_AStarPathPlanner::J_AStarPathPlanner(costmap_2d::Costmap2DROS* costmap_ros, double obstacle_factor, bool dijkstra, bool gbfs) : PathPlanner(costmap_ros, obstacle_factor) { // can not using both dijkstra and GBFS at the same time if (!(dijkstra && gbfs)) { is_dijkstra_ = dijkstra; is_gbfs_ = gbfs; } else { is_dijkstra_ = false; is_gbfs_ = false; } }; /** * @brief A* implementation * @param start start node * @param goal goal node * @param path optimal path consists of Node * @param expand containing the node been search during the process * @return true if path found, else false */ bool J_AStarPathPlanner::plan(const Point3d& start, const Point3d& goal, Points3d& path, Points3d& expand) { double m_start_x, m_start_y, m_goal_x, m_goal_y; if ((!validityCheck(start.x(), start.y(), m_start_x, m_start_y)) || (!validityCheck(goal.x(), goal.y(), m_goal_x, m_goal_y))) { return false; } Node start_node(m_start_x, m_start_y); Node goal_node(m_goal_x, m_goal_y); start_node.set_id(grid2Index(start_node.x(), start_node.y())); goal_node.set_id(grid2Index(goal_node.x(), goal_node.y())); // clear vector path.clear(); expand.clear(); // open list and closed list std::priority_queue<Node, std::vector<Node>, Node::compare_cost> open_list; std::unordered_map<int, Node> closed_list; open_list.push(start_node); // main process while (!open_list.empty()) { // pop current node from open list auto current = open_list.top(); open_list.pop(); // current node does not exist in closed list if (closed_list.find(current.id()) != closed_list.end()) continue; closed_list.insert(std::make_pair(current.id(), current)); expand.emplace_back(current.x(), current.y()); // goal found if (current == goal_node) { const auto& backtrace = _convertClosedListToPath<Node>(closed_list, start_node, goal_node); for (auto iter = backtrace.rbegin(); iter != backtrace.rend(); ++iter) { // convert to world frame double wx, wy; costmap_->mapToWorld(iter->x(), iter->y(), wx, wy); path.emplace_back(wx, wy); } return true; } // explore neighbor of current node for (const auto& motion : motions) { // explore a new node auto node_new = current + motion; node_new.set_g(current.g() + motion.g()); node_new.set_id(grid2Index(node_new.x(), node_new.y())); // node_new in closed list if (closed_list.find(node_new.id()) != closed_list.end()) continue; node_new.set_pid(current.id()); // next node hit the boundary or obstacle // prevent planning failed when the current within inflation if ((node_new.id() < 0) || (node_new.id() >= map_size_) || (costmap_->getCharMap()[node_new.id()] >= costmap_2d::LETHAL_OBSTACLE * obstacle_factor_ && costmap_->getCharMap()[node_new.id()] >= costmap_->getCharMap()[current.id()])) continue; // if using dijkstra implementation, do not consider heuristics cost if (!is_dijkstra_) node_new.set_h(std::hypot(node_new.x() - goal_node.x(), node_new.y() - goal_node.y())); // if using GBFS implementation, only consider heuristics cost if (is_gbfs_) node_new.set_g(0.0); // else, g will be calculate through node_new = current + m ROS_INFO("*********J-Astar Controller initialized!"); open_list.push(node_new); } } return false; } } // namespace path_planner } // namespace rmp 重写这段代码,要求J_AStarPathPlanner::J_AStarPathPlanner的输入和输出不变,更改评价函数简化成先往一个方向走,然后再考虑转向,点越接近起点或者终点,越优先考虑
最新发布
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值