博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【hihocoder 1519】 逃离迷宫II
阅读量:4696 次
发布时间:2019-06-09

本文共 2535 字,大约阅读时间需要 8 分钟。

【题目链接】:

【题意】

Chinese

【题解】

bfs题;
根据bfs的性质;
第一次到达的点肯定是转弯次数最少的;
每次往一个方向走到头就好了;
搞个数组判判重.
这里在往一个方向走的时候;
如果途中遇到了终点;
也算能到达终点;
其他的就没什么坑点了;
【Number Of WA
3
【完整代码】

#include 
using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define ms(x,y) memset(x,y,sizeof x)typedef pair
pii;typedef pair
pll;const int dx[9] = {
0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {
0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);const int N = 510;struct node{ int x,y,fx;};int f[N][N][5],n,m,sx,sy,tx,ty;bool bo[N][N];char s[N];queue
dl;int bfs(int x,int y){ rep1(i,1,500) rep1(j,1,500) rep1(k,1,4) f[i][j][k] = -2; rep1(i,1,4) { f[x][y][i] = -1; int tq = x,tw = y; if (bo[tq+dx[i]][tw+dy[i]]) { while (bo[tq+dx[i]][tw+dy[i]]) { tq+=dx[i],tw+=dy[i]; if (tq==tx && tw==ty) return 0; } f[tq][tw][i] = 0; dl.push(node{tq,tw,i}); } } while (!dl.empty()) { node temp = dl.front(); int q = temp.x,w = temp.y,pre = temp.fx; dl.pop(); rep1(i,1,4) { int tq = q,tw = w; if (bo[tq+dx[i]][tw+dy[i]]) { while (bo[tq+dx[i]][tw+dy[i]]) { tq+=dx[i],tw+=dy[i]; if (tq==tx && tw==ty) return f[q][w][pre]+1; } if (f[tq][tw][i]==-2) { f[tq][tw][i] = f[q][w][pre]+1; dl.push(node{tq,tw,i}); } } } } return -1;}int main(){ //freopen("F:\\rush.txt","r",stdin); ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use //init?????? cin >> n >> m; rep1(i,1,n) { cin >>(s+1); rep1(j,1,m) { if (s[j]=='.') bo[i][j]=true; if (s[j]=='#') bo[i][j]=false; if (s[j]=='S') { bo[i][j] = true; sx = i,sy = j; } if (s[j]=='T') { bo[i][j] = true; tx = i,ty = j; } } } cout << bfs(sx,sy)<

转载于:https://www.cnblogs.com/AWCXV/p/7626322.html

你可能感兴趣的文章
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
数据库插入数据乱码问题
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>
IO流入门-第十章-DataInputStream_DataOutputStream
查看>>
DRF的分页
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
python:open/文件操作
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>