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 ...
随机推荐
- asp.net操作word 配置在IIS上出现的问题
异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...
- unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开
unity 代码有调整,重新导出 iOS 最烦的就是 覆盖导出后项目不能打开,原因是 editor 里面的脚本,破坏了 Unity-iPhone.xcodeproj 里面的结构,具体是什么原因,也不 ...
- shell中的特殊符号总结
在shell中常用的特殊符号罗列如下: # ; ;; . , / \\ 'string'| ! $ ${} $? $$ $* \"string\"* ** ...
- 读书笔记--C陷阱与缺陷(三)
第三章 1. 指针与数组 书中强调C中数组注意的两点: 1) C语言只有一维数组,但是数组元素可以是任何类型对象,是另外一个数组时就产生了二维数组.数组大小是常数(但GCC实现了变长数组..) ...
- delphi 获取一个字符占用几个字节,方法
- 区间DP小结
也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...
- Centos简介
Centos作为主流的一种Linux操作系统,以后项目中,比如后期Redis,以及部署一些项目,会把Centos作为服务器操作系统,我们选用Centos,主要是免费,以及稳定. Centos详细介绍, ...
- LeetCode828. Unique Letter String
https://leetcode.com/problems/unique-letter-string/description/ A character is unique in string S if ...
- 判断一个字符是否为数字的两种方法(C/C++)
在平时,我们经常遇见判断字符是否为数字这种题目,虽然感觉还是很简单,不过我是个更喜欢用函数的人,因为我觉得这样更便捷,所以我更推荐第二种方式. 1.直接判断 #include <stdio.h& ...
- 不用的代码,存一份--用tornado实现的websocket
因为现在使用Channels来实现啦, 这样就在Django的体系里自已弄完了. 方便后期代码维护和服务器部署. 这份土陋的东东,就放起来吧. 毕竟,通过读取文件来返回实时websocket,不适应于 ...