首页 > 其他分享 >hdu1429 胜利大逃亡(续)--BFS

hdu1429 胜利大逃亡(续)--BFS

时间:2022-12-06 19:39:46浏览次数:90  
标签:hdu1429 return temp vis -- step BFS int state


原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1429​


一:分析

定义一个三维数组,标记该点是否走过,其中第三维代表在该路径上所获钥匙的标记。


二:AC代码

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

#include<iostream>
#include<algorithm>
#include<queue>
#include<cctype>
using namespace std;

struct Node
{
int x, y;
int step;
int state;
Node(){}
Node(int x, int y, int step, int state) :x(x), y(y), step(step), state(state) {}
};

int dir[4][2] = { { 0, 1 },{ 0, -1 },{ 1, 0 },{ -1, 0 } };
char s[25][25];
bool vis[25][25][1 << 10];
int n, m, t;

int get(char c)
{
return c - (isupper(c) ? 'A' : 'a');
}

bool check(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == '*')
return false;
return true;
}


int bfs(int x, int y)
{
queue<Node> Q;
vis[x][y][0] = 1;

Q.push(Node(x, y, 0, 0));

while (!Q.empty())
{
Node cur = Q.front();
Q.pop();
if (cur.step >= t - 1)
break;

for (int i = 0; i < 4; i++)
{
Node temp = cur;
temp.x += dir[i][0];
temp.y += dir[i][1];
temp.step++;

if (check(temp.x, temp.y) == 0 || vis[temp.x][temp.y][temp.state])
continue;

if (isupper(s[temp.x][temp.y]) && !(temp.state&(1 << get(s[temp.x][temp.y]))))//遇到门,没有钥匙,不走
continue;

if (islower(s[temp.x][temp.y]))//找到钥匙
temp.state |= (1 << get(s[temp.x][temp.y]));

if (s[temp.x][temp.y] == '^')
return temp.step;

if (!vis[temp.x][temp.y][temp.state])
{
vis[temp.x][temp.y][temp.state] = 1;
Q.push(temp);
}
}
}

return -1;
}

int main()
{
int x, y;
while (~scanf("%d%d%d", &n, &m, &t))
{
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &s[i][j]);
if (s[i][j] == '@')
{
x = i;
y = j;
}
}
}

memset(vis, 0, sizeof(vis));

printf("%d\n", bfs(x, y));
}

return 0;
}






标签:hdu1429,return,temp,vis,--,step,BFS,int,state
From: https://blog.51cto.com/u_11937443/5916540

相关文章

  • hdu1495 非常可乐--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1495​​一:分析思路:预处理m<n<s,以后处理方便点初始状态,m,n杯中可乐体积为0,s杯中体积为s;然后分六种情况:1,......
  • hdu1253 胜利大逃亡--BFS & BFS的总结
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1253​​一:题意一个三维A*B*C,起点(0,0,0),终点(A-1,B-1,C-1),求在t时间内(包括t)能否到达?可以,输出花费的最少时间,否则......
  • hdu1254 推箱子--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1254​​一:分析分两步,一是箱子走到终点,二是人得走到箱子的前一个位置。先是bfs_box在t点找到一个可行点tt,进而......
  • hdu1240 Asteroids!--DFS & BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1240​​一:题意三维空间,中o表示可以走,x表示不能走,给出行走的起始点和目的点的坐标,问最少多少步可以从起点到达目......
  • hdu1180 诡异的楼梯--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1180​​需要注意这句话:Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.#define_CRT_SECURE_NO_DEPRECATE#def......
  • hdu1242 Rescue--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1242​​一:题意x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间思路:B......
  • 使用Spring Reactor优化推荐流程
    1.背景公司有一个推荐系统Rec,这个系统的主要功能是:向外部系统提供推荐接口根据请求获取推荐策略根据推荐策略完成推荐的召回、过滤、打分、排序阶段Rec作为微服务......
  • hdu1195 Open the Lock--单向BFS & 双向BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1195​​一:题意两个四位数的数字,经过一下三种方式变换,求出变成另一个数字的最小时间。加1;减1;相邻交换其中9+1变......
  • hdu1175 连连看 --DFS/BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1175​​直接上代码,不是很难。#define_CRT_SECURE_NO_DEPRECATE#define_CRT_SECURE_Cy1_OVERLOAD_STANDARD_N......
  • UE4学习笔记23——【动画】Mixamo自动绑骨并导入虚幻4
    P61.Mixamo自动绑骨并导入虚幻4P61需要插件“MixamoAnimationRetargeting”(200多块......)(这节课就简单听听,以后用到了再看)(桥豆麻袋!不用买这个插件,这节课的东西也能......