[topcoder]TheGridDivTwo
http://community.topcoder.com/stat?c=problem_statement&pm=13628&rd=16278
标程是BFS,我用DFS,都可解。
这里复杂的把pair写了hash函数,其实直接用个矩阵来存bool就可以了。
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <utility> using namespace std; struct pairhash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const
{
return std::hash<T>()(x.first) * 37 ^ std::hash<U>()(x.second);
}
}; class TheGridDivTwo {
public:
unordered_set<pair<int, int>, pairhash> visited;
unordered_set<pair<int, int>, pairhash> block; int find(vector <int> x, vector <int> y, int k) {
for (int i = 0; i < x.size(); i++) {
block.insert(make_pair(x[i], y[i]));
}
int result = 0;
pair<int, int> start = make_pair(0, 0);
findRe(result, start, k, 0);
return result;
} void findRe(int &result, pair<int, int> &p, int k, int step) {
visited.insert(p);
if (step == k) {
result = max(result, p.first);
} else {
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
for (int i = 0; i < 4; i++) {
pair<int, int> tmp = make_pair(p.first + dx[i], p.second + dy[i]);
if (tmp.first + k - step > result && valid(tmp)) {
findRe(result, tmp, k, step + 1);
}
}
}
visited.erase(p);
} bool valid(pair<int, int> &p) {
if (block.find(p) != block.end() || visited.find(p) != visited.end()) {
return false;
}
return true;
} };
[topcoder]TheGridDivTwo的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- 图片滚动插件jquery bxslider
https://www.cnblogs.com/axl234/p/4167196.html
- hutool http+天气预报
中国天气接口:http://www.weather.com.cn/data/sk/地址.html,只显示当天. sojson接口:http://t.weather.sojson.com/api/wea ...
- qsor快排序以及cmp函数
void qsort(void*base,size_t num,size_t width,int(__cdecl*compare)(const void*,const void*)); 各参数:1 待 ...
- linux 文件 s 权限
s权限的作用:表示对文件具用可执行权限的用户将使用文件拥有者的权限或文件拥有者所在组的权限在对文件进行执行. s权限的设置:4,用户拥有者的执行权限位, 6,用户组的执行权限位, 2, 两者都设置, ...
- 爬虫(GET)——爬取多页的html
工具:python3 目标:将编写的代码封装,不同函数完成不同功能,爬取任意页数的html 新学语法:with open as 除了有更优雅的语法,with还可以很好的处理上下文环境产生的异常. # ...
- 转 Python 操作 MySQL 数据库
#########http://www.runoob.com/python/python-mysql.html Python 标准数据库接口为 Python DB-API,Python DB-API为 ...
- spring mvc源码分析
1.传统xml配置方式 web.xml里面配置:org.springframework.web.servlet.DispatcherServlet,处理项目的spring配置文件:classpath* ...
- Android Studio CMake 生成多个so
生成多个so案例 这里stringFromJNI和stringFromJNI11分别是调用one-lib和two-lib两个so package com.test.ndkmoreso; import ...
- Deep Learning 和 Knowledge Graph howto
领军大家: Geoffrey E. Hinton http://www.cs.toronto.edu/~hinton/ 阅读列表: reading lists and survey papers fo ...
- Redis Intro - Skiplist
http://zhangtielei.com/posts/blog-redis-skiplist.html https://juejin.im/entry/59197a390ce4630069fbcf ...