SGU128 Snake
SGU128,题意是给定N个点,问说能不能形成一个闭环G,要求G经过每个点,且在每个点处都有90度的转角,且不能出现自交。
没想出来,通过这提供的思路,由于每个点处都需要90度的转弯,因此每个点处必然有一条横向以及一条纵向的路径穿过,单从某个x来看,由于上述限制,因此需要有偶数个点两两配对。然后通过搜索判断是否连通,最后再借助树状数组判断是否有自交的情况(”+”这种自交形状)出现。
PS: 这里有个GDB的简单教程。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int MAXN = ; pair<int, int> points[MAXN];
vector<int> x_points[*MAXN], y_points[*MAXN];
int x_link[MAXN], y_link[MAXN];
bool visit[MAXN], towards[*MAXN]; class BinaryIndexedTree {
private:
int *c_, num_;
public:
BinaryIndexedTree() : c_(NULL) {}
BinaryIndexedTree(int num) : num_(num) {
c_ = new int[num_];
memset(c_, , sizeof(int) * num_);
}
~BinaryIndexedTree() {
if (c_ != NULL) {
delete[] c_;
c_ = NULL;
}
}
private:
int lowbit(int x) {
return x & (-x);
}
public:
int Query(int i) {
int res = ;
while (i > ) {
res += c_[i];
i -= this->lowbit(i);
}
return res;
}
int Query(int left, int right) {
return this->Query(right) - this->Query(left - );
}
void Update(int i, int value) {
while (i < num_) {
c_[i] += value;
i += this->lowbit(i);
}
}
}; bool x_cmp(int u, int v) {
return points[u].first < points[v].first;
} bool y_cmp(int u, int v) {
return points[u].second < points[v].second;
} int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
a += ;
b += ; points[i].first = a;
points[i].second = b; x_points[a].push_back(i);
y_points[b].push_back(i);
} bool ok = true;
int total_length = ;
memset(x_link, -, sizeof(x_link));
memset(y_link, -, sizeof(y_link)); do {
// x order
for (int i = ; i < * MAXN; i++) {
if (x_points[i].size() > ) {
if (x_points[i].size() & ) {
ok = false;
break;
} sort(x_points[i].begin(), x_points[i].end(), y_cmp); for (int j = ; j < x_points[i].size(); j += ) {
int down = x_points[i][j];
int up = x_points[i][j + ]; total_length += (points[up].second - points[down].second); y_link[down] = up; y_link[up] = down;
}
}
}
if (!ok) break; // y order
for (int i = ; i < * MAXN; i++) {
if (y_points[i].size() > ) {
if (y_points[i].size() & ) {
ok = false;
break;
} sort(y_points[i].begin(), y_points[i].end(), x_cmp); for (int j = ; j < y_points[i].size(); j += ) {
int left = y_points[i][j];
int right = y_points[i][j + ]; total_length += (points[right].first - points[left].first); x_link[left] = right; x_link[right] = left;
}
}
}
if (!ok) break; // search
memset(visit, false, sizeof(visit));
vector<int> Q;
Q.push_back();
visit[] = true; for (int i = ; i < Q.size(); i++) {
if (x_link[Q[i]] == - || y_link[Q[i]] == -) {
ok = false;
break;
} else {
if (visit[x_link[Q[i]]] == false) {
visit[x_link[Q[i]]] = true;
Q.push_back(x_link[Q[i]]);
} if (visit[y_link[Q[i]]] == false) {
visit[y_link[Q[i]]] = true;
Q.push_back(y_link[Q[i]]);
}
}
} for (int i = ; i < n; i++) {
if (visit[i] == false) {
ok = false;
break;
}
} // check self-cross
memset(towards, false, sizeof(towards));
BinaryIndexedTree tree = BinaryIndexedTree(MAXN * );
for (int i = ; i < *MAXN; i++) if (x_points[i].size() > ) {
for (int j = ; j < x_points[i].size(); j += ) {
int down = points[x_points[i][j]].second;
int up = points[x_points[i][j + ]].second; towards[down] = !towards[down];
towards[up] = !towards[up]; tree.Update(down, towards[down] ? + : -);
tree.Update(up, towards[up] ? + : -); if (down + < up - && tree.Query(down + , up - ) > ) {
ok = false;
break;
}
} if (!ok) break;
} } while(); if (ok) {
printf("%d\n", total_length);
} else {
printf("0\n");
}
}
SGU128 Snake的更多相关文章
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- Leetcode: Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- 2101 Problem A Snake Filled
题目描述 “What a boring world!”Julyed felt so bored that she began to write numbers on the coordinate pa ...
- 图像分割之(五)活动轮廓模型之Snake模型简介
在"图像分割之(一)概述"中咱们简单了解了目前主流的图像分割方法.下面咱们主要学习下基于能量泛函的分割方法.这里学习下Snake模型简单的知识,Level Set(水平集)模型会在 ...
- Epic - Snake Sequence
You are given a grid of numbers. A snakes equence is made up of adjacent numbers such that for each ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- 图像切割之(五)活动轮廓模型之Snake模型简单介绍
图像切割之(五)活动轮廓模型之Snake模型简单介绍 zouxy09@qq.com http://blog.csdn.net/zouxy09 在"图像切割之(一)概述"中咱们简单了 ...
- 353. Design Snake Game
贪食蛇. GAME OVER有2种情况,1是咬到自己,2是出界. 1)用QUEUE来保留占据的格子,每走一格就添加1个,然后POll()最后一个. 做一个一样的SET来check要走的格子是不是已经在 ...
随机推荐
- new失败判断
使用 malloc/calloc 等分配内存的函数时,一定要检查其返回值是否为空;但是C++ 里,如果 new 分配内存失败,默认是抛出bad_alloc异常,不会返回空:但是有些编译器对c++标准支 ...
- 如何查看Oracle的用户权限
ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己创建的对象的 ...
- [译]深入理解JVM
深入理解JVM 原文链接:http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals 每个使用Java的开发者都知道Java ...
- centos6.5 最小化安装无法上网
在VMware里装了个centos 6.5. 最小化安装后无法上网.在 google里找到答案 第一步:执行命令启动网卡 (最小化安装不是自动启动的) [root@localhost]# ifcon ...
- Demo学习: Closable Tabs
Closable Tabs 给tab页添加关闭按钮,设置UniTabSheet.Closable = True这样在tab页的右上角就会出现关闭图标,点击可以关闭当前tab页面: 1. 直接关闭,完成 ...
- Spark Streaming揭秘 Day34 解析UI监听模式
Spark Streaming揭秘 Day34 解析UI监听模式 今天分享下SparkStreaming中的UI部分,和所有的UI系统一样,SparkStreaming中的UI系统使用的是监听器模式. ...
- Android类库常用类库一览
在Android SDK中包括很多包文件,通过了解这些包的功能也有助于了解可以开发的功能. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: android.app ...
- epoll_create, epoll_ctl和epoll_wait
参考代码 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> #include & ...
- 【转】STL中mem_fun和mem_fun_ref的用法及区别
原文:http://www.cppblog.com/mysileng/archive/2012/12/25/196615.html 引子: 怎么对容器中的所有对象都进行同一个操作?我们可能首先想到的是 ...
- Flasback数据库(闪回数据库)
数据库闪回原理: 一旦启用了闪回数据库,修改的块映像会不时从数据库缓冲区复制到闪回缓冲区.通过新的后台恢复写入器(Recovery Writer, RVWR)将此闪回缓冲区内容刷新到磁盘和闪回日志接着 ...