A星搜索和普通bfs比较 寻路比较

A星搜索普通bfs比较,似乎可以瘦下一大圈
在这里插入图片描述

#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;
typedef vector<vector<int> > VVI;

#define show(x...) \
	do { \
		cout << "[" << #x << " -> "; \
		err(x); \
	} while (0)

void err() { cout << "]" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
	   inline void read(T &x, T2 &... oth) {
		   x = 0;
		   char ch = getchar();
		   ll f = 1;
		   while (!isdigit(ch)) {
			   if (ch == '-') f *= -1; 
			   ch = getchar();
		   }
		   while (isdigit(ch)) {
			   x = x * 10 + ch - 48;
			   ch = getchar();
		   }
		   x *= f;
		   read(oth...);
	   }
	template <typename T, typename... T2>
	   inline void print(T x, T2... oth) {
		   ll p3=-1;
		   if(x<0) putchar('-'), x=-x;
		   do{
				print_f[++p3] = x%10 + 48;
		   } while(x/=10);
		   while(p3>=0) putchar(print_f[p3--]);
		   putchar(' ');
		   print(oth...);
	   }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;
int sr, sc, er, ec;
char mtx[128][128], vis[128][128];
bool vis2[128][128];

void init() {
	memset(vis, 0, sizeof(vis));
	memset(vis2, 0, sizeof(vis2));
	vis[sr][sc] = vis2[sr][sc] = true;
}

struct NodeBfs {
	int r, c, step;
} ;
int dr[] = { 0, 0, 1, -1 },
	dc[] = { 1, -1, 0, 0 };
#define Node NodeBfs
void bfs() {
	queue<Node> q;
	init();
	q.push({sr, sc, 0});
	while(!q.empty()) {
		auto now = q.front(); q.pop();
		vis2[now.r][now.c] = true;
		if(now.r == er && now.c == ec) break;
		for(int i=1; i<=4; i++) {
			int r = now.r, c = now.c;
			int nr = r + dr[i], nc = c + dc[i];
			if(nr>=1 && nr<=n && nc>=1 && nc<=m && !vis[nr][nc] && mtx[nr][nc]!='*') {
				vis[nr][nc] = true;
				q.push({nr, nc, now.step+1});
			}
		}
	}
	for(int i=1; i<=n; i++) { 
		for(int j=1; j<=m; j++) {
			printf("[%c]", vis2[i][j] ? 'o' : '.');
		} 
		printf("\n");
	}
}

int HVAL(int r, int c) {
	return (abs(r-er) + abs(c-ec));
}
struct node {
	int r, c, step;
	bool operator < (const node& no) const {
		return step + HVAL(r, c) < no.step + HVAL(no.r, no.c);
	}
} ;

void a_star() {
	priority_queue<node> q;
	init();	
	q.push({sr, sc, 0});
	while(!q.empty()) {
		auto now = q.top(); q.pop();
		vis2[now.r][now.c] = true;
		if(now.r == er && now.c == ec) break; 
		node tmp = { -1, -1, -1 };
		for(int i=1; i<=4; i++) {
			int r = now.r, c = now.c;
			int nr = r + dr[i], nc = c + dc[i];
			if(nr>=1 && nr<=n && nc>=1 && nc<=m && !vis[nr][nc] && mtx[nr][nc]!='*') {
				vis[nr][nc] = true;
				q.push({nr, nc, now.step+1}); //把相邻点都入堆, 堆内通过启发式函数选相对优的出堆
				// if(tmp.step == -1) tmp = { nr, nc, now.step+1 };
				// else tmp = min(tmp, {nr, nc, now.step+1});
			}
		}
		if(~tmp.step && false) {
			q.push(tmp);
			vis[tmp.r][tmp.c] = true;
		}
	}
	cout << endl;
	for(int i=1; i<=n; i++) { 
		for(int j=1; j<=m; j++) {
			printf("[%c]", vis2[i][j] ? 'o' : ' ');
		} 
		printf("\n");
	}

}

signed main() {
#ifdef debug
	freopen("test.txt", "r", stdin);
	clock_t stime = clock();
#endif
	read(n, m, K);
	while(K--) {
		int r, c;
		read(r, c);
		mtx[r][c] = '*';
	}
	read(sr, sc, er, ec);
	mtx[sr][sc] = 'S', mtx[er][ec] = 'T';
	for(int i=1; i<=n; i++) { 
		for(int j=1; j<=m; j++) {
			printf("[%c]", mtx[i][j] ? mtx[i][j] : ' ');
		} 
		printf("\n");
	}
	cout << endl;
	bfs();
	a_star();





#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值