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 ...
随机推荐
- nanosleep()
函数原型 #include <time.h> int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); 描述 ...
- 安装Visual Studio Scrum 1.0过程模板
近几年里,Scrum变成了相当流行的软件开发方法学.因为它轻量.可迭代且快速等优点,以致于在敏捷开发中极受欢迎.微软甚至将TFS2010自带的MSF Agile5.0过程模板做得像Scrum,开发者们 ...
- Operfire 安装指南
http://www.cnblogs.com/hoojo/archive/2012/05/13/2498151.html 本文的英文原文来自 http://www.igniterealtime.org ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- MySQL权限操作:Grant、Revoke
数据库操作: 创建数据库.创建表——CREATE 删除数据库.删除表——DROP 删除表内容——TRUNCATE.DELETE(后者效率低.一行一行地删除记录) 查询数据库.查询表——SELECT 插 ...
- thinkphp模型创建
- 关于文件格式Fuzzing测试与漏洞挖掘的学习
最近对于文件的漏洞挖掘比较感兴趣,所以在找资料来看.顺带记录笔记,把这些笔记贴在博客中分享一下.最近打算把精力放在mp3格式的漏洞发掘上,一来这是常见的文件格式格式也比较清晰.二来这也是学长推荐的入手 ...
- JQuery中Table标签页和无缝滚动
HTML代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- 【BZOJ】3674: 可持久化并查集加强版
题解 感觉全世界都写过只有我没写过 毕竟是板子还是挺简单的,只要用可持久化线段树维护一下数组的形态就好了,每个数组里面维护这个数组的father,和这个点所在树的最长链的深度(如果这个点是根按秩合并要 ...
- 黑马程序员_java基础笔记(12)...内省(IntroSpector)
—————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流!—————————— 1,了解JavaBean.2,BeanUtils工具包. 1,了解JavaBean. ...