1.链接地址:

http://bailian.openjudge.cn/practice/2790/

2.题目:

总时间限制:
3000ms
内存限制:
65536kB
描述
一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行。同时当Extense处在某个格点时,他只能移动到东南西北(或者说上 下左右)四个方向之一的相邻格点上,Extense想要从点A走到点B,问在不走出迷宫的情况下能不能办到。如果起点或者终点有一个不能通行(为#),则 看成无法办到。
输入
第1行是测试数据的组数k,后面跟着k组输入。每组测试数据的第1行是一个正整数n (1 <= n <= 100),表示迷宫的规模是n * n的。接下来是一个n * n的矩阵,矩阵中的元素为.或者#。再接下来一行是4个整数ha, la, hb, lb,描述A处在第ha行, 第la列,B处在第hb行, 第lb列。注意到ha, la, hb, lb全部是从0开始计数的。
输出
k行,每行输出对应一个输入。能办到则输出“YES”,否则输出“NO”。
样例输入
2
3
.##
..#
#..
0 0 2 2
5
.....
###.#
..#..
###..
...#.
0 0 4 0
样例输出
YES
NO

3.思路:广搜,注意起点和终点如果不通则直接为NO

4.代码:

 #include <iostream>
#include <cstdio>
#include <queue> using namespace std; struct POINT
{
int x;
int y;
}; int idx_x[] = {-,,,};
int idx_y[] = {,,,-}; int main()
{
//freopen("C://input.txt","r",stdin); int i,j; int k;
cin >> k; while(k--)
{
int n;
cin >> n; bool **arr = new bool*[n];
for(i = ; i < n; ++i) arr[i] = new bool[n]; char ch;
for(i = ; i < n; ++i)
{
for(j = ; j < n; ++j)
{
cin >> ch;
if(ch == '.') arr[i][j] = true;
else arr[i][j] = false;
}
} int ha,la,hb,lb;
cin >> ha >> la >> hb >> lb; if(arr[ha][la] == false || arr[hb][lb] == false) {cout << "NO" << endl; continue;}
queue<POINT> q_point;
POINT point;
point.x = la;
point.y = ha;
q_point.push(point);
arr[point.y][point.x] = false; bool flag = false;
while(!q_point.empty())
{
point = q_point.front();
q_point.pop();
for(i = ; i < ; ++i)
{
POINT p2;
p2.x = point.x + idx_x[i];
p2.y = point.y + idx_y[i]; if(p2.y == hb && p2.x == lb) {flag = true;break;}
else if(p2.x >= && p2.x < n && p2.y >= && p2.y < n && arr[p2.y][p2.x])
{
q_point.push(p2);
arr[p2.y][p2.x] = false;
}
}
if(i < ) break;
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl; for(i = ; i < n; ++i) delete [] arr[i];
delete [] arr;
} return ;
}

OpenJudge 2790 迷宫的更多相关文章

  1. NOI openjudge 1792.迷宫

    一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Extense处在某个格点时,他只 ...

  2. openjudge走迷宫(DFS)

    题目: 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  3. DFS与BFS——理解简单搜索(中文伪代码+例题)

    新的方法和概念,常常比解决问题本身更重要. ————华罗庚 引子 深度优先搜索(Deep First Search) 广度优先搜索(Breath First Search) 当菜鸟们(比如我)初步接触 ...

  4. NOI / 2.5基本算法之搜索-6044:鸣人和佐助详解

    总时间限制: 1000ms 内存限制: 65536kB 题目 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢? 已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置.地图上的每个位置都可以走到, ...

  5. Openjudge 2.5 6264:走出迷宫

    总时间限制:  1000ms 内存限制:  65536kB 描述 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n* ...

  6. 【OpenJ_Bailian - 2790】迷宫(bfs)

    -->迷宫  Descriptions: 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不 ...

  7. C语言动态走迷宫

    曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...

  8. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  9. BFS_Maze_求解迷宫最短路径

    /* 10 10 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...

随机推荐

  1. algorithm@ Matrix fast power

    一. 什么是快速幂: 快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高.一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次 ...

  2. post方法

    CookieContainer cookie = new CookieContainer(); private string HttpPost(string Url, string postDataS ...

  3. Go Slices: usage and internals

    Introduction Go's slice type provides a convenient and efficient means of working with sequences of ...

  4. css3划过图片闪光

    css代码 01 .img { display:block; position: relative; width:800px; height:450px; margin:0 auto;} 02 .im ...

  5. context-param和init-param区别

    转载 http://www.cnblogs.com/hzj-/articles/1689836.html <context-param>的作用:web.xml的配置中<context ...

  6. 关于SpingMVC实现文件下载你所不知道的

    http://www.cnblogs.com/yolanda-lee/p/4683422.html 文件的上传下载一般在项目中还是非常实用的,此处专门整理一下文件的下载,至于文件的上传实现将在后续中补 ...

  7. Cocos2d-x学习笔记之Cocos2d-x开发环境搭建

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Cocos2d-x源码包下载地址: http://cocos2d-x.org/projects/cocos2d-x/ ...

  8. FZOJ2110 star(DFS)

    Overpower often go to the playground with classmates. They play and chat on the playground. One day, ...

  9. android学习资料

      在线查看android源码 1. https://github.com/android 2. http://grepcode.com/project/repository.grepcode.com ...

  10. Maven Build Profiles--reference

    What is Build Profile? A Build profile is a set of configuration values which can be used to set or ...