Beispiel #1
0
 std::list<Point> landsNearby(int x, int y) {
   std::list<Point> result;
   if (isLand(x+1, y)) result.push_back(Point(x+1, y));
   if (isLand(x-1, y)) result.push_back(Point(x-1, y));
   if (isLand(x, y+1)) result.push_back(Point(x, y+1));
   if (isLand(x, y-1)) result.push_back(Point(x, y-1));
   return result;
 }
 vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
     vector<int> res;
     vector<bool> isLand(m * n, false);
     Union_Find uf(m * n);
     int cnt = 0, cur, next, x, y, col, row;
     for(int i = 0; i < positions.size(); ++i){
         cnt++;
         row = positions[i].first, col = positions[i].second;
         cur = row * n + col;
         isLand[cur] = true;
         for(int j = 0; j < 4; ++j){
             x = col + dx[j], y = row + dy[j];
             next = y * n + x;
             if(x >= 0 && x < n && y >= 0 && y < m && isLand[next]){
                 if(!uf.Find(cur, next)){ 
                     uf.Union(cur, next);
                     cnt--;
                 }
             }
         }
         res.push_back(cnt);
     }
     return res;
 }
Beispiel #3
0
int main(int argc, char** argv) {
	std::cout << "*homework 15*" << std::endl;

	std::ifstream inputFile(SOURCE_DIR "/sources/15/input.txt");
	std::ofstream outputFile(SOURCE_DIR "/sources/15/output.txt");

	if ( !inputFile.is_open() || !outputFile.is_open()){
		std::cout << "cannot open files" << std::endl;
		return 1;
	}

	size_t linesCount = 0;
	size_t lineLength = 0;
	std::string strBuff;
	landmap_t landmap;
	const std::string marks =
			"01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	while (std::getline(inputFile, strBuff)) {
		landmap.push_back(strBuff);
	}
	linesCount = landmap.size();
	lineLength = landmap[0].length();

	renderMap(landmap);

	unsigned int landCount = 0;
	unsigned int correction = 0;
	char currMark = marks[0];

	//jump over lines
	for (unsigned int y = 0; y < linesCount; y++) {

		//jump inside line
		for (unsigned int x = 0; x < lineLength; x++) {

			if (isLand(landmap, x, y)) {

				currMark = marks[landCount];

				while (isLand(landmap, x + 1, y)) {

					if (isLand(landmap, x, y - 1)
							&& getLandChar(landmap, x, y - 1) != currMark) {
						correction++;
					}

					if (isLand(landmap, x, y - 1)) {
						currMark = getLandChar(landmap, x, y - 1);
					}

					setLandChar(landmap, x, y, currMark);
					x++;
				}

				if (isLand(landmap, x, y - 1)
						&& getLandChar(landmap, x, y - 1) != currMark) {
					correction++;
				}

				setLandChar(landmap, x, y, currMark);

				if (!isLand(landmap, x + 1, y)) {
					landCount++;
				}
			}
		}

		//normalize line
		for (unsigned int x = 0; x < lineLength; x++) {
			if (isLand(landmap, x, y)) {
				currMark = getLandChar(landmap, x, y);

				while (isLand(landmap, x + 1, y)) {
					setLandChar(landmap, x + 1, y, currMark);
					x++;
				}
			}
		}
	}

	renderMap(landmap);

	std::cout << " l:" << landCount << " c:" << correction << " rez:"
			<< landCount - correction << std::endl;

	outputFile << landCount - correction << std::endl;

	inputFile.close();
	outputFile.close();

	return 0;
}