What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
A square well is dug with a peculiar shape: each 1x1 section has varying heights above some floor. You wish to fill the well with water, filling from a hose above the square marked 1. Square 1 is the lowest (think of this as a heightmap in units from the bottom). Water flows at 1 cubic unit per unit time (e.g. 1 liter per minute if you want specific units). You wish to know when you fill a specific square.
You can assume water behaves like it does in the real world - it immediately disperses, evenly, to all accessible regions, and it cannot spontaneously leak from one square to another if there is no path.
Assume a constant flow rate for the water.
Today's question is - writing a program, can you tell at what time the well's target square is under a cubic unit of water?
Input Description
You'll be given a row with two numbers, N and N, telling you the dimensions of the well. Then you'll be given N rows of N colums of unique numbers. Then you'll get one row with one number, M, telling you the target square to cover with one cubic unit of water. Example:
3 3
1 9 6
2 8 5
3 7 4
4
Output Description
Your program should emit the time unit at which time the target square is covered in one cubic unit of water.
The above example's answer should be 16.
Explanation: In this case the column 9 8 7 forms a barrier from the 1 square to the 4 square, our target. As such you have to fill enough to get to a height of 7 to begin filling 4. (7-1) + (7-2) + (7-3) [all to get over the barrier] + 1 [to fill the four block].
Challenge Input
7 7
38 33 11 48 19 45 22
47 30 24 15 46 28 3
14 13 2 34 8 21 17
10 9 5 16 27 36 39
18 32 20 1 35 49 12
43 29 4 41 26 31 37
25 6 23 44 7 42 40
35
7 7
15 16 46 1 38 43 44
25 10 7 6 34 42 14
8 19 9 21 13 23 22
32 11 29 36 3 5 47
31 33 45 24 12 18 28
40 41 20 26 39 48 2
49 35 27 4 37 30 17
26
Solution
in C
#include <stdio.h>
#define MAX 256
static void
fill(int *grid, int w, int h, int i, char *visited)
{
static const int d[] = {
+1, +0, -1, +0, +0, +1, +0, -1, +1, +1, +1, -1, -1, +1, -1, -1,
};
int x = i % w;
int y = i / w;
visited[i] = 1;
grid[i]++;
for (int j = 0; j < 8; j++) {
int tx = x + d[j * 2 + 0];
int ty = y + d[j * 2 + 1];
int ti = ty * w + tx;
if (tx >= 0 && tx < w && ty >= 0 && ty < h && !visited[ti]) {
if (grid[i] > grid[ti]) {
grid[i]--;
fill(grid, w, h, ti, visited);
break;
}
}
}
}
int
main(void)
{
int w, h, dst;
int grid[MAX];
scanf("%d%d", &w, &h);
for (int i = 0; i < w * h; i++)
scanf("%d", grid + i);
scanf("%d", &dst);
int soff = 0;
int doff = 0;
int dval = (dst + 1) * w * h;
for (int i = 0; i < w * h; i++) {
if (grid[i] == 1)
soff = i;
if (grid[i] == dst)
doff = i;
grid[i] *= w * h;
}
int t = 0;
for (; grid[doff] < dval; t++) {
for (int i = 0; i < w * h; i++) {
char visited[MAX] = {0};
fill(grid, w, h, soff, visited);
}
}
printf("%d\n", t);
}
A square well is dug with a peculiar shape: each 1x1 section has varying heights above some floor. You wish to fill the well with water, filling from a hose above the square marked 1. Square 1 is the lowest (think of this as a heightmap in units from the bottom). Water flows at 1 cubic unit per unit time (e.g. 1 liter per minute if you want specific units). You wish to know when you fill a specific square.
You can assume water behaves like it does in the real world - it immediately disperses, evenly, to all accessible regions, and it cannot spontaneously leak from one square to another if there is no path.
Assume a constant flow rate for the water.
Today's question is - writing a program, can you tell at what time the well's target square is under a cubic unit of water?
Input Description
You'll be given a row with two numbers, N and N, telling you the dimensions of the well. Then you'll be given N rows of N colums of unique numbers. Then you'll get one row with one number, M, telling you the target square to cover with one cubic unit of water. Example:
3 3
1 9 6
2 8 5
3 7 4
4
Output Description
Your program should emit the time unit at which time the target square is covered in one cubic unit of water.
The above example's answer should be 16.
Explanation: In this case the column 9 8 7 forms a barrier from the 1 square to the 4 square, our target. As such you have to fill enough to get to a height of 7 to begin filling 4. (7-1) + (7-2) + (7-3) [all to get over the barrier] + 1 [to fill the four block].
Challenge Input
7 7
38 33 11 48 19 45 22
47 30 24 15 46 28 3
14 13 2 34 8 21 17
10 9 5 16 27 36 39
18 32 20 1 35 49 12
43 29 4 41 26 31 37
25 6 23 44 7 42 40
35
7 7
15 16 46 1 38 43 44
25 10 7 6 34 42 14
8 19 9 21 13 23 22
32 11 29 36 3 5 47
31 33 45 24 12 18 28
40 41 20 26 39 48 2
49 35 27 4 37 30 17
26
Solution
in C
#include <stdio.h>
#define MAX 256
static void
fill(int *grid, int w, int h, int i, char *visited)
{
static const int d[] = {
+1, +0, -1, +0, +0, +1, +0, -1, +1, +1, +1, -1, -1, +1, -1, -1,
};
int x = i % w;
int y = i / w;
visited[i] = 1;
grid[i]++;
for (int j = 0; j < 8; j++) {
int tx = x + d[j * 2 + 0];
int ty = y + d[j * 2 + 1];
int ti = ty * w + tx;
if (tx >= 0 && tx < w && ty >= 0 && ty < h && !visited[ti]) {
if (grid[i] > grid[ti]) {
grid[i]--;
fill(grid, w, h, ti, visited);
break;
}
}
}
}
int
main(void)
{
int w, h, dst;
int grid[MAX];
scanf("%d%d", &w, &h);
for (int i = 0; i < w * h; i++)
scanf("%d", grid + i);
scanf("%d", &dst);
int soff = 0;
int doff = 0;
int dval = (dst + 1) * w * h;
for (int i = 0; i < w * h; i++) {
if (grid[i] == 1)
soff = i;
if (grid[i] == dst)
doff = i;
grid[i] *= w * h;
}
int t = 0;
for (; grid[doff] < dval; t++) {
for (int i = 0; i < w * h; i++) {
char visited[MAX] = {0};
fill(grid, w, h, soff, visited);
}
}
printf("%d\n", t);
}
Comments
Post a Comment