题意是要穿过一个迷宫并且将每一步打印出来。

用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久......

代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
char mp[maxn][maxn];
int n,m,t,cnt,vis[maxn][maxn],fight[maxn][maxn];
int dir[][] = {,,,,-,,,-};
struct node
{
int x,y,time;
bool operator<(const node &a) const {
return a.time<time;
}
// 不能用的重载
// friend bool operator < (node a,node b)
// {
// return a.time < b.time;
// }
};
//struct cmp
//{
// bool operator () (node a,node b)
// {
// return a.time < b.time;
// }
//};
bool bfs()
{
priority_queue<node> q;
// priority_queue<node,vector<node>,cmp> q;
node st;
st.x = st.y = st.time = ;
q.push(st);
mp[][] = 'X';
while (!q.empty())
{
node cur = q.top();
q.pop();
if(cur.x==n- && cur.y==m-)
{
t = cur.time;
return true;
}
for(int i = ; i < ; ++i)
{
node next;
next.x = cur.x+dir[i][];
next.y = cur.y+dir[i][];
if(mp[next.x][next.y]=='X') continue;
if(next.x<n && next.x>= && next.y<m && next.y>= )
{
if(mp[next.x][next.y]!='.')
{
next.time = cur.time++mp[next.x][next.y]-'';
fight[next.x][next.y] = mp[next.x][next.y]-'';
}
else next.time = cur.time+;
vis[next.x][next.y] = i+;
q.push(next);
mp[next.x][next.y] = 'X';
}
}
}
return false;
}
void prtmp(int x,int y)
{
if(vis[x][y] == ) return ;
int prex,prey;
prex = x - dir[vis[x][y]-][];
prey = y - dir[vis[x][y]-][];
prtmp(prex,prey);
cout << (cnt++) << "s:(" << prex << "," << prey << ")->(" << x << "," << y << ")\n";
while(fight[x][y]--)
cout << (cnt++) << "s:FIGHT AT (" << x << "," << y << ")\n";
}
int main()
{
while(cin >> n >> m)
{
memset(fight,,sizeof(fight));
memset(vis,,sizeof(vis));
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j)
cin >> mp[i][j];
if(bfs())
{
cnt = ;
cout << "It takes " << t << " seconds to reach the target position, let me show you the way.\n";
prtmp(n-,m-);
}
else cout << "God please help our poor hero.\n";
cout << "FINISH\n";
}
return ;
}

HDU 1026(迷宫 BFS+打印)的更多相关文章

  1. HDU 1026

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 记录bfs路径,用一个数组记录next前驱的方向,然后递归的时候减回去即可 #include <io ...

  2. HDU 1269 迷宫城堡(强连通)

    HDU 1269 迷宫城堡 pid=1269" target="_blank" style="">题目链接 题意:中文题 思路:强连通模板题 代 ...

  3. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  4. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

  5. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  6. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...

  7. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  8. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

随机推荐

  1. Gym - 101848C Object-Oriented Programming (树链剖分+线段树+动态开点)

    C. Object-Oriented Programming time limit per test 3.0 s memory limit per test 1024 MB input standar ...

  2. Linux 多网卡绑定bond

    mode=0:负载均衡模式,增加带宽,两块网卡使用的是同一个MAC地址,所以必须配置网卡相连的交换机,这两个端口应采用聚合方式. mode=1:主备模式,一个线断了,另一条自动备援. mode=6:负 ...

  3. [luogu2617][bzoj1901][Zju2112]Dynamic Rankings【树套树+树状数组+主席树】

    题目网址 [传送门] 题目大意 请你设计一个数据结构,支持单点修改,区间查询排名k. 感想(以下省略脏话inf个字) 真的强力吹爆洛谷数据,一般的树套树还给我T了一般的点,加强的待修主席树还给我卡了几 ...

  4. 跨SQL注入

    概念 SQL Injection按照字面意思来翻译就是"SQL注射",常被叫做"SQL注入",它的含义就是利用某些数据库的外部接口把用户数据插入到实际数据库操作 ...

  5. nginx.conf(centos6, 1.12)主配置文件修改

    #nginx1.12 centos6.xuser admin admin;worker_processes 4; error_log /data/services/logs/nginx_error.l ...

  6. PHP函数memory_get_usage获取PHP内存清耗量

    (PHP 4 >= 4.3.2, PHP 5, PHP 7) memory_get_usage — 返回分配给 PHP 的内存量 说明 int memory_get_usage ([ bool ...

  7. MYSQL主从复制制作配置方案

    1. 主从复制机器配置 操作系统:centos7 x64 基于vagrant下的virtual box的虚拟机两台 master ip:192.168.21.11, slave ip 192.168. ...

  8. LinkedList(JDK1.8)源码分析

    双向循环链表 双向循环链表和双向链表的不同在于,第一个节点的pre指向最后一个节点,最后一个节点的next指向第一个节点,也形成一个"环".而LinkedList就是基于双向循环链 ...

  9. Zabbix监控服务器硬盘状态

    安装Iptables服务: [root@localhost /]# yum install iptables-services [root@localhost /]# vim /etc/sysconf ...

  10. JavaServer Faces (JSF) with Spring

    JavaServer Faces (JSF) with Spring Last modified: April 30, 2018 by baeldung Spring+ Spring MVC JSF ...