kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984

迷宫问题 POJ - 3984

题目链接:https://vjudge.net/problem/POJ-3984

这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,希望你能慢慢理解。

#include <iostream>
#include <cstring>
#include<vector>
#include<string>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

#define inf (1LL << 31) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)

const int N = 10;
int mv_x[] = { 0, 0, -1, 1 };   //(1)
int mv_y[] = { 1, -1, 0, 0 };   //(2)       (1) + (2) 可以表示人的上下左右移动
char mp[N][N];  //地图
bool vis[N][N];  //有没有访问过了

struct node{
	int x, y;
	vector<string> vec;
	node(){}
	node(int a, int b){
		x = a;
		y = b;
	}
};

inline void input(){

	rep__(i, 0, 5) rep__(j, 0, 5) cin >> mp[i][j];
}

//检查有无越界的函数
inline bool check(int x, int y){
	return x >= 0 && x <= 4 && y >= 0 && y <= 4;
}

void fun(string& p){
	cout << p << endl;
}

void work(){
	
	//开始的处理,起点标记
	vis[0][0] = true;
	node T(0, 0);
	T.vec.push_back("(0, 0)");

	queue<node> que;
	que.push(T);

	while (!que.empty()){

		node tmp = que.front();
		que.pop();

		rep__(p, 0, 4){

			//人的移动
			int dx = tmp.x + mv_x[p];
			int dy = tmp.y + mv_y[p];
			
			//没越界  是可以走的   没访问过
			if (check(dx, dy) && mp[dx][dy] == '0' && !vis[dx][dy]){
				vis[dx][dy] = true; //标记

				node in = tmp;
				in.x = dx;
				in.y = dy;
				string t = "(x, x)";
				t[1] = '0' + dx;
				t[4] = '0' + dy;
				in.vec.push_back(t); //把新的点压入

				if (dx == 4 && dy == 4){ //遇到了出口,输出路线
					
					for_each(in.vec.begin(), in.vec.end(), fun);

					return;
				}
				que.push(in); //把新的状态压入队列
			}
		}
	}
}

int main(){

	ios::sync_with_stdio(false);
	cin.tie(0);

	input();
	work();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值