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 ...
随机推荐
- 配置vuejs加载模拟数据
[个人笔记,非技术博客] 1.使用前确保安装axios插件,vuejs官方推荐,当然使用其他插件也可以 2.配置dev-server.js var router = express.Router(); ...
- UVA10212 【The Last Non-zero Digit.】
暴力可做!!!(十秒还不打暴力!!!)暴力算阶乘边算边取余上代码 #include<iostream> #define int long long //开long long using n ...
- Ubuntu 16.04 使用docker资料汇总与应用docker安装caffe并使用Classifier(ros kinetic+usb_cam+caffe)
Docker是开源的应用容器引擎.若想简单了解一下,可以参考百度百科词条Docker.好像只支持64位系统. Docker官网:https://www.docker.com/ Docker - 从入门 ...
- MySQL约束笔记
MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列 · 查看表的字段信息:desc 表名; · 查看表的所有信息:show create table 表名; 添加主键约束:alter t ...
- Linux学习笔记之一及虚拟机的安装
学习Linux基础入门 学习实验楼Linux基础入门--学习笔记系列博客 第一节 Linux系统简介 Linux就是一个操作系统,操作系统在计算机系统中包括系统调用和内核两层.在简单了解了Linux的 ...
- mvn本地库导入jar包
导入脚本 #!/bin/sh mvn deploy:deploy-file -DgroupId=com.xxx.xxx -DartifactId=包名 -Dversion=4.0 -Dpackag ...
- appium----【Mac】address already in user 127.0.0.1:4725,端口被占用的查找与kill进程
报错截图示例: 解决方法: Mac: lsof -i tcp:4723 #查看端口号 sudo kill -9 29443 #杀死进程 Windows: netstat -aon|fi ...
- Dubbo内核实现之SPI简单介绍
这个部分单独写一页,看起来更高大上一些. 1.概括 Dubbo采用微内核+插件体系,使得设计优雅,扩展性强.那所谓的微内核+插件体系是如何实现的呢! 即我们定义了服务接口标准,让厂商去实现(如果不了解 ...
- HDU - 3577 Fast Arrangement 线段树
Fast Arrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- c语言程序与设计第三版-苏小红--第一轮学习笔记、难点整理
---恢复内容开始--- 1> 编程:需求分析.设计.编写程序(编码.编辑.链接.运行).调试程序 2> 指数形式:e的左边是数值部分(有效数字),不能省略,但可以表示成 .e-4:等等: ...