Problem
Geologists sometimes divide an area of land in different regions based on where rainfall flows down to. These regions is called drainage basins.
Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin The same label, subject to the following rules. From each cell, the water flows down to the most one of its 4 neighboring cells. For each cell, if none of its 4 neighboring cells have a lower altitude than the current cell ' s, then the water does not fl OW, 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'll 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 was part of the same drainage basin. Each basin was labeled by a unique lower-case-letter, in such a-in-a-, when the rows of the map were concatenated from to p to bottom, the resulting string is lexicographically smallest. (in particular, the basin of the most North-western cell are always labeled ' a '.)
Input
The first line of the input file would contain the number of maps, T. T Maps would follow, each starting with double integers on a line-- H and W --the height and Widt H of the map, in cells. The next H lines would each contain a row of the map, from north to south, each containing W integers, fr Om West to east, specifying the altitudes of the cells.
Output
For each test case, output 1+H lines. The first line must is 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'll is at the most of the basins.
Large DataSet
1 ≤ H, W ≤ 100;
0≤altitudes < 10,000.
There'll is at the most basins.
This topic is actually connected block tagging problem, if water from P0 (x0,y0) flow to P1 (x1, y1), it means that P0 and P1 are connected.
Then all the connected blocks are marked, and then the tag numbers of the connected blocks are re-filled in order from left to right and from top to bottom.
In this topic, I use a similar method to mark the connected blocks, and then sort the blocks.
Because there is no sort at the beginning, so the submission failed, and later added to the sorting, small set succeeded, but finally, the results come out, the big set of the result is not right, until now have not found the problem ....
On the code (big set has a problem):
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
int Setval (char* pVal, int* pPar, int nPos)
{
if (pval[npos]==0)
{
Pval[npos] = Setval (PVal, PPar, Ppar[npos]);
}
return Pval[npos];
}
int main ()
{
file* FP;
int i, x, y;
fp = fopen ("1.txt", "R");
int nround = 0;
FSCANF (FP, "%d", &nround);
for (i=0; i<nround; i++)
{
int nwidth, nheight;
FSCANF (FP, "%d%d", &nheight, &nwidth);
int* Phei = (int*) malloc (nwidth*nheight*sizeof (int));
int* PPar = (int*) malloc (nwidth*nheight*sizeof (int));
char* PVal = (char*) malloc (nwidth*nheight*sizeof (char));
memset (pVal, 0, nwidth*nheight*sizeof (char));
int* Pheit = Phei;
for (y=0; y<nheight; y++)
{
for (x=0; x<nwidth; x + +, pheit++)
{
FSCANF (FP, "%d", Pheit);
}
}
int nPos = 0;
int NChar = 0;
for (y=0; y<nheight; y++)
{
for (x=0; x<nwidth; x + +, npos++)
{
int nhei = Phei[npos];
int nminhei = 0xFFFFFF;
int nminpos;
if (y>0)
{
if (Phei[npos-nwidth]<nminhei)
{
Nminhei = Phei[npos-nwidth];
Nminpos = Npos-nwidth;
}
}
if (x>0)
{
if (Phei[npos-1]<nminhei)
{
Nminhei = phei[npos-1];
Nminpos = nPos-1;
}
}
if (x<nwidth-1)
{
if (Phei[npos+1]<nminhei)
{
Nminhei = phei[npos+1];
Nminpos = npos+1;
}
}
if (y<nheight-1)
{
if (Phei[npos+nwidth]<nminhei)
{
Nminhei = Phei[npos+nwidth];
Nminpos = Npos+nwidth;
}
}
if (Nminhei<nhei)
{
Ppar[npos] = Nminpos;
}
Else
{
Ppar[npos] = NPos;
Pval[npos] = ' a ' +nchar;
nchar++;
}
}
}
NPos = 0;
for (y=0; y<nheight; y++)
{
for (x=0; x<nwidth; x + +, npos++)
{
Setval (PVal, PPar, NPos);
}
}
char* Pmask = (char*) malloc (nchar*sizeof (char));
int* Pminpos = (int*) malloc (nchar*sizeof (int));
for (x=0; x<nchar; x + +)
{
PMASK[X] = x;
PMINPOS[X] = 0xFFFFFF;
}
NPos = 0;
for (y=0; y<nheight; y++)
{
for (x=0; x<nwidth; x + +, npos++)
{
if (npos<pminpos[pval[npos]-' a '])
{
Pminpos[pval[npos]-' a '] = NPos;
}
}
}
for (y=0; y<nchar; y++)
{
for (x=0; x<nchar; x + +)
{
if (pminpos[y]<pminpos[x])
{
int ntmp;
ntmp = Pminpos[y];
pminpos[y] = pminpos[x];
pminpos[x] = ntmp;
ntmp = Pmask[y];
pmask[y] = pmask[x];
pmask[x] = ntmp;
}
}
}
printf ("Case #%d:/n", i+1);
npos = 0;
for (y=0; y<nheight; y++)
{
for (x=0; x<nwidth; x + +, nPos++
{
printf ("%c", pmask[pval[npos]-' a ']+ ' a ');
}
printf ("/n");
}
Free (pmask);
Free (Phei);
Free (PPar);
Free (pVal);
}
Fclose (FP);
return 0;
}