这是 meelo 原创的 IEEEXtreme极限编程大赛题解

Xtreme 10.0 - Mysterious Maze

题目来源 第10届IEEE极限编程大赛

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mysterious-maze

There is a valuable treasure that is hidden below a complex maze. The maze is made of rooms and are square in shape, and a maze of size N will have N × N rooms with all of them closed initially. When a room is open, one can enter into it from any of its adjacent open rooms; two rooms are adjacent if they share a common wall.

The maze was built in a way that it opens itself by opening its rooms randomly. A maze is said to be open if there is at least one path from any one of the rooms facing the top of the maze to any room on the bottom side facing the treasure. Anyone, who attempts to enter the maze without being able to reach the treasure and return, will be cruelly killed by the maze.

The local government has spent years researching the maze and figured out a way to determine the sequence of rooms being opened in almost real time. Based on this data, the government has posed the following challenge, with a small percentage of the treasure to whomever solves the problem:

Given the sequence of room openings, determine when the maze becomes open, or if it remains closed throughout.

Input Format

Input begins with a single integer N, which denotes the size of maze.

All of the next lines (except the last one) denotes the sequence of the rooms the maze is opening. Each line contains 2 integers X and Y which denotes the row and column of the room opened by the maze. The last line just includes -1 and marks the end of input.

Constraints

1 <= XY <= N <= 1000

Output Format

Output a single integer R based on the final status of the maze. R denotes the number of room openings that occur before the maze first becomes open, or -1 if the maze remains closed.

Sample Input

4
1 4
2 3
3 2
4 3
4 1
2 1
1 1
-1

Sample Output

-1

Explanation

It is easy to understand if you plot the maze. The following is the state of the maze at the end of the inputs. Xindicates that a room is closed and O that a room is open. Note that there is no path from the top of the maze to the bottom of the maze.

O X X O
O X O X
X O X X
O X O X

Consider the second input sample (which is available if you click on the Run Code button):

4
1 4
2 3
3 2
4 3
4 1
2 1
1 1
3 1
3 4
2 2
-1

Below is a figure with the maze after 7 rooms are open. Note that there is no path from the top of the maze to the bottom of the maze, and therefore the maze is closed.

O X X O
O X O X
X O X X
O X O X

However, after 8th room is open, there is a path, as shown below:

O X X O
O X O X
O O X X
O X O X

Thus, the expected output is:

8

题目解析

这个题目像是一个搜索题,从上侧出发看是否能够搜索到下侧。搜索一次需要O(M^2)。每开一次门,都需要搜索1次。复杂度就到了10^9,铁定会超时。

迷宫是否连通,有点像两个节点是否是1类。判断两个节点是否是1类,可以用并查集嘛。

每打开一个房间,就将上下左右的房间合并成1类,需要O(log(M))的时间。

上侧和下侧分别建立一个虚拟节点,初始与第1行的房间和最后1行的房间相连。

每打开一个房间,判断上侧虚拟节点是否与下侧虚拟节点节点是否属于同一类。如果属于同一类,迷宫则被打通了。

总的复杂度为O(Nlog(M))。

程序

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int find(vector<int> &parent, int x) {
int px;
if(parent[x] < ) {
px = x;
}
else {
px = find(parent, parent[x]);
parent[x] = px;
}
return px;
} void union_(vector<int> &parent, int x, int y) {
int px = find(parent, x);
int py = find(parent, y); if(px != py) { if(parent[px] < parent[py]) {
parent[px] += parent[py];
parent[py] = px;
}
else {
parent[py] += parent[px];
parent[px] = py;
}
}
} bool legal(int x, int y, int H) {
return (x >= && x <= H) && (y >= && y <=H);
} int pos2Index(int x, int y, int H) {
return (x-) * H + (y-);
} int main() {
int H;
cin >> H;
vector<int> parent(H*H+, -);
vector<vector<bool> > maze(H+, vector<bool>(H+, )); for(int i=; i<=H; i++) {
union_(parent, pos2Index(, i, H), H*H);
union_(parent, pos2Index(H, i, H), H*H+);
} int dir[][] = {{,}, {,-}, {,}, {-,}};
int count = ;
while(true) {
int x, y;
cin >> x;
if(x == -) {
cout << - << endl;
break;
}
cin >> y;
maze[x][y] = true;
for(int d=; d<; d++) {
int nx = x + dir[d][];
int ny = y + dir[d][]; if(legal(nx, ny, H) && maze[nx][ny]) {
union_(parent, pos2Index(x, y, H), pos2Index(nx, ny, H));
}
}
if(find(parent, H*H) == find(parent, H*H+)) {
cout << count;
break;
}
count++;
} return ;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Mysterious Maze的更多相关文章

  1. IEEEXtreme 10.0 - Mancala'h

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mancala'h 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  3. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  4. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  5. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  7. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  8. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  9. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

随机推荐

  1. [转载]js正则表达式语法

    1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...

  2. nodejs express框架一个工程中同时使用ejs模版和jade模版

    在某些项目中,比如你接手了一个别人的项目然后你不想用蛋疼的ejs,或者你不想用蛋疼的jade.你有不想重写之前的页面,那么你现在可能需要新引入ejs或者jade模块,你仅仅需要做下面两步也许就能完成使 ...

  3. 「Django」rest_framework学习系列-权限认证

    权限认证:1.项目下utils文件写permissions.py文件 from rest_framework.permissions import BasePermission class SVIPP ...

  4. 前端PHP入门-011-可变函数

    可变函数,我们也会称呼为变量函数.简单回顾一下之前的知识点: <?php $hello = 'world'; $world = '你好'; //输出的结果为:你好 echo $$hello; ? ...

  5. DLL初试

    环境: VC++6.0 步骤: 1.建立一个WIN32 DYNAMIC-LINK LIBRARY工程,编写CPP文件,文件内容例如: #include "stdafx.h" #in ...

  6. HTML 5 Web 存储:localStorage和sessionStorage

    本文内容摘自http://www.w3school.com.cn/ 在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessi ...

  7. 【CODEVS】2800 送外卖

    [算法]最短路(floyd)+状态压缩型动态规划 [题解] 经典的TSP问题(货郎担问题):求最小权哈密顿回路(遍历全图点一次且仅一次).本题稍作改动,先说原TSP问题解法:状压DP. 状态用二进制表 ...

  8. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

  9. js父页面和子页面相互取值

    iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...

  10. ORA-01552 非系统表空间不能使用系统回滚段处理

    今天新搭建了一个10g的测试数据库,运行都很正常,但是在打开autotrace功能后执行语句,报错 SQL> set autotrace on SQL> select username,s ...