逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 34147    Accepted Submission(s): 8333

Problem Description
  给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
 
Input
  第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
 
Output
  每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
 

Sample Input

2
5 5
...**
*.**.
.....
.....
*....
1 1 1 1 3
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

Sample Output

no
yes
 
题意:逃离迷宫,转弯的次数要小于题目给的最大转弯数
思路:bfs,能往前走就往前走,输入行和列的时候看清楚啊,呜呜呜呜
 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int n,m;
int max_turn;
int start_x,start_y,end_x,end_y;
char a[maxn][maxn];
bool vis[maxn][maxn];
int dir[][]={,,,,,-,-,}; struct node
{
int x,y;
int turn_num;
}; int bfs()
{
queue<node>que;
node q1;
q1.x=start_x;
q1.y=start_y;
q1.turn_num=-;
que.push(q1);
vis[start_x][start_y]=;
while(!que.empty())
{
node q2=que.front();
que.pop();
if(q2.x==end_x && q2.y==end_y && q2.turn_num<=max_turn)
return ;
q1.turn_num=q2.turn_num+;
for(int i=;i<;i++)
{
q1.x=q2.x+dir[i][];
q1.y=q2.y+dir[i][];
while(q1.x>= && q1.x<=n && q1.y>= && q1.y<=m && a[q1.x][q1.y]=='.' )
{
if(!vis[q1.x][q1.y])
{
que.push(q1);
vis[q1.x][q1.y]=;
}
q1.x=q1.x+dir[i][];
q1.y=q1.y+dir[i][];
}
} }
return ;
} int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>a[i][j];
cin>>max_turn>>start_y>>start_x>>end_y>>end_x;
memset(vis,,sizeof(vis));
if(bfs())
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return ;
}
 

逃离迷宫 HDU - 1728(bfs)的更多相关文章

  1. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...

  3. hdu 1728 bfs **

    简单bfs,记录好状态即可 #include<cstdio> #include<iostream> #include<algorithm> #include< ...

  4. 逃离迷宫 HDU1728 (bfs)

    和连连看非常相似   都是求转向的BFS 改了一下就上交了... #include<cstdio> #include<cstring> #include<algorith ...

  5. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

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

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

  7. hdu 1728 逃离迷宫 bfs记步数

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

  8. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  9. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

随机推荐

  1. docker mysql安装

    Docker MySQL-Server 安装1.搜索docker search mysql# 一般会选择mysql-server 版本 2.拉取 docker pull mysql-server 3. ...

  2. MVC的viewPage 通用属性运用。

    试想下在MVC的前端页面JS或者html中需要使用多语言,而后端的多语言是维护在资源文件中的,前端如果使用的话需要使用AJAX频繁的获取,一个页面中可能会存在大量的需要语言转换的地方,频繁使用AJAX ...

  3. 在前端解決顯示null值一例

    用.net core2.1 api返回的JSON里包含null值,前端直接顯示出來了,影響閱讀, 用以下語句將null轉換為空字符串: // Build html. html += "< ...

  4. 关于报错“More than one fragment with the name [spring_web] was found. This is not legal ...”的解决办法

    最近在搭建一个spring mvc 项目时遇到“More than one fragment with the name [spring_web] was found. This is not leg ...

  5. Ubuntu获取root 权限,开机自动登入root

    新机器获取root权限,只需要给root 增加密码: sudo passwd root 修改开机自动登入: #sudo gedit /etc/lightdm/lightdm.conf 修改参数: au ...

  6. mac不限速下载百度网盘

    本文转载自:https://blog.csdn.net/u010837612/article/details/80029212 相信大家都比较困惑,百度网盘客户端限速后一般只有几十K的下载速度,Win ...

  7. javaSe-hashMap

    package com.java.chap08.sec05; public class Student { private String name; private Integer age; publ ...

  8. Windows 7, Visual Studio 2015下编译Webkit

    因工作需要,需要编译Windows版本的Webkit,中间走了不少弯路,都记录下来,供大家参考!也随时欢迎大家讨论(QQ群:345802342) 整个编译工作参考的是官方文档:https://webk ...

  9. IE浏览器已经卸载,但是桌面上的图标却无法删除的解决方案

    第一步——win+R运行[regedit],打开注册表. 第二步——在注册表里面依次找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer ...

  10. Android系统Binder机制学习总结

    一.Binder机制概述 在Android开发中,很多时候我们需要用到进程间通信,所谓进程间通信,实现进程间通信的机制有很多种,比如说socket.pipe等,Android中进程间通信的方式主要有三 ...