Google Code Jam 2009 Qualification Round Problem B. Watersheds
https://code.google.com/codejam/contest/90101/dashboard#s=p1
Problem
Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.
Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.
- From each cell, water flows down to at most one of its 4 neighboring cells.
- For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell's, then the water does not flow, and the current cell is called a sink.
- Otherwise, water flows from the current cell to the neighbor with the lowest altitude.
- In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.
Every cell that drains directly or indirectly to the same sink is part of the same drainage basin. Each basin is labeled by a unique lower-case letter, in such a way that, when the rows of the map are concatenated from top to bottom, the resulting string is lexicographically smallest. (In particular, the basin of the most North-Western cell is always labeled 'a'.)
Input
The first line of the input file will contain the number of maps, T. T maps will follow, each starting with two integers on a line -- H and W -- the height and width of the map, in cells. The next H lines will each contain a row of the map, from north to south, each containingW integers, from west to east, specifying the altitudes of the cells.
Output
For each test case, output 1+H lines. The first line must be of the form
Case #X:
where X is the test case number, starting from 1. The next H lines must list the basin labels for each of the cells, in the same order as they appear in the input.
Limits
T ≤ 100;
Small dataset
1 ≤ H, W ≤ 10;
0 ≤ altitudes < 10.
There will be at most two basins.
Large dataset
1 ≤ H, W ≤ 100;
0 ≤ altitudes < 10,000.
There will be at most 26 basins.
Sample
Input |
Output |
5 |
Case #1: |
Notes
In Case #1, the upper-right and lower-left corners are sinks. Water from the diagonal flows towards the lower-left because of the lower altitude (5 versus 6).
Solution:
int H, W;
vector<vector<int>>mmap; pair<int, int> r_cell (int X, int Y)
{
int alt = mmap[X][Y];
int min_alt = alt;
int tX, tY, maX = -, maY = -; tX = X - ; tY = Y - ;
if (tX >= && tX < H && tY >= && tY < W)
if (mmap[tX][tY] < min_alt) {
min_alt = mmap[tX][tY];
maX = tX; maY = tY;
} tX = X - ; tY = Y - ;
if (tX >= && tX < H && tY >= && tY < W)
if (mmap[tX][tY] < min_alt) {
min_alt = mmap[tX][tY];
maX = tX; maY = tY;
} tX = X - ; tY = Y + ;
if (tX >= && tX < H && tY >= && tY < W)
if (mmap[tX][tY] < min_alt) {
min_alt = mmap[tX][tY];
maX = tX; maY = tY;
} tX = X + ; tY = Y - ;
if (tX >= && tX < H && tY >= && tY < W)
if (mmap[tX][tY] < min_alt) {
min_alt = mmap[tX][tY];
maX = tX; maY = tY;
} if ((min_alt) < alt) {
return r_cell(maX, maY);
} else {
return pair<int, int>(X, Y);
} } map<pair<int, int>, char> solve()
{ map<pair<int, int>, vector<pair<int, int>>>sinks;
map<pair<int, int>, char>sinklabel;
map<pair<int, int>, char>label;
char b_label = 'a' - ; for (int h = ; h < H; h++) {
for (int w = ; w < W; w++) {
pair<int, int> cell = r_cell(h, w); if (!sinks.count(pair<int, int>(cell.first, cell.second))) {
b_label++; // new sink
sinks.insert(pair<pair<int, int>, vector<pair<int, int>>>(pair<int, int>(cell.first, cell.second), vector<pair<int, int>>()));
sinklabel.insert(pair<pair<int, int>, char>(pair<int, int>(cell.first, cell.second), b_label));
} // add to existing sink
sinks.at(pair<int, int>(cell.first, cell.second)).push_back(pair<int, int>(h, w));
label.insert(pair<pair<int, int>, char>(pair<int, int>(h, w), sinklabel.at(pair<int, int>(cell.first, cell.second))));
}
} return label;
} int main()
{ freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout); int T;
scanf("%d\n", &T);
if (!T) {
cerr << "Check input!" << endl;
exit();
} for (int t = ; t <= T; t++) {
scanf("%d %d\n", &H, &W); // map
mmap.clear();
int alt;
for (int h = ; h < H; h++) {
vector<int> row;
for (int w = ; w < W; w++) {
scanf("%d", &alt);
row.push_back(alt);
}
mmap.push_back(row);
} auto result = solve();
printf("Case #%d:\n", t); for (int h = ; h < H; h++) {
for (int w = ; w < W; w++) {
printf("%c ", result.at(pair<int, int>(h, w)));
}
printf("\n");
}
} fclose(stdin);
fclose(stdout);
return ;
}
Google Code Jam 2009 Qualification Round Problem B. Watersheds的更多相关文章
- Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam
本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...
- Google Code Jam 2009 Qualification Round Problem A. Alien Language
https://code.google.com/codejam/contest/90101/dashboard#s=p0 Problem After years of study, scientist ...
- [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round
Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...
- [C++]Standing Ovation——Google Code Jam 2015 Qualification Round
Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...
- Google Code Jam 2014 资格赛:Problem B. Cookie Clicker Alpha
Introduction Cookie Clicker is a Javascript game by Orteil, where players click on a picture of a gi ...
- Google Code Jam 2014 资格赛:Problem D. Deceitful War
This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...
- [刷题]Google Code Jam 2017 - Round1 C Problem A. Ample Syrup
https://code.google.com/codejam/contest/3274486/dashboard Problem The kitchen at the Infinite House ...
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- Google Code Jam 2014 资格赛:Problem C. Minesweeper Master
Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...
随机推荐
- 深入理解MySQL的并发控制、锁和事务【转】
本文主要是针对MySQL/InnoDB的并发控制和加锁技术做一个比较深入的剖析,并且对其中涉及到的重要的概念,如多版本并发控制(MVCC),脏读(dirty read),幻读(phantom read ...
- Python-Web框架的本质
Web框架的本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. Python中使用socket和 ...
- C# TimeSpan获取 年月
public static string GetYearMonthDayString(this DateTime expires) { try { var now = DateTime.Now; Ti ...
- RNN BPTT
双向LSTM
- 读书笔记--C陷阱与缺陷(六)
第六章 1.预处理器:预处理器先对代码进行必要的转换处理,简化编程者的工作. 它的重要原因有以下两点: a. 假如要将程序中出现的所有实例都加以修改,但希望只改动程序一处数值,重新编译实现. 预处理器 ...
- maven网址
http://www.yiibai.com/maven/maven_environment_setup.html
- Python模块Pygame安装
一.使用pip安装Python包 大多数较新的Python版本都自带pip,因此首先可检查系统是否已经安装了pip.在Python3中,pip有时被称为pip3. 1.在Linux和OS X系统中检查 ...
- 20165203《Java程序设计》第二周Java学习总结
教材学习内容总结 第二章 (一)标识符 注意: 标识符由字母.下画线.美元符号和数字组成,长度不受限制. 标识符第一个字符不能是数学字符. 标识符不能是关键字. 标识符不能是true.false和nu ...
- 阿里云宝塔Linux服务器管理面版初始化地址不能登入(原创)
宝塔面板是一款可以让Linux服务器实现图形可视化操作的集成系统. 安装好以后,页面初始化登录地址http://{您的服务器IP}:888不能登入. 不能登入通常是我们没有权限访问,需要更改安全组赋予 ...
- 开源框架:SDWebImage
http://blog.csdn.net/uxyheaven/article/details/7909373 SDWebImage是我搞iOS以来少数佩服的框架,膜拜一下作者.真的写的非常棒! 这套开 ...