200. Number of Islands

/*

1. 找到一个grid[i][j]==1的节点,

2. 把它置位2

3. 从这个节点向外扩展,(递归),建岛完成。

4. 找第二个grid[i][j] == 1的节点,转2.

*/

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {        
        int m = grid.size();
        if(m == 0) return 0;
        int n = grid[0].size();
        int ans = 0;
        for(int i = 0;i<m;i++)
            for(int j = 0; j<n; j++)
            {   
                
                //dfs(grid,m,n,i,j);
                //++ans;
                
                //if(grid[i][j] != '1')
                if(grid[i][j] == '1')
                {
                    ++ans;
                    dfs(grid,m,n,i,j);
                }             
                
            }
        return ans;
    }
private:    
    void dfs(vector<vector<char>>& grid,int m, int n, int i, int j)
    {
        if(i<0 || j<0 || i>=m || j>=n || grid[i][j]!='1') return;
        grid[i][j] = '2';
        
        dfs(grid,m,n,i,j+1);
        dfs(grid,m,n,i,j-1);
        dfs(grid,m,n,i-1,j);
        dfs(grid,m,n,i+1,j);
    }
};

阅读更多
换一批

Islands

04-10

You are working in a team developing the new strategic game Island Warriors. The game proceeds on a hexagonal grid, the coordinate system on the map is introduced as shown on the picture below.nnnA hexagon can be either land, or sea. An island is a maximal connected set of land hexagons, an area of the island is the number of hexagons in it.nnYour current task is writing random map generator. The map generated must satisfy some conditions, in particular no island area must exceed s.nnThe design of your module is the following. Initially all hexagons are sea. Special random generator provides you with the sequence of hexagons. Getting the next hexagon, you check whether it is already the land one. If it is, you just skip this hexagon. If it is sea, you check whether convering it to land results in an island with area exceeding s. If it does, you also skip this hexagon. In the other case you convert it to land.nnSo, the design step is completed, now its time to implement the module. Fortunately, your teammate has already implemented random generator, so you just have to implement the rest of the module. To check yourself you decided first to output only the number of islands in the resulting map and their areas.nnInputnnThe first line of the input file contains n --- the number of hexagons provided to you by random generator (1 ≤ n ≤ 50000), and s (1 ≤ s ≤ n).nnThe following n lines contain two integer numbers each --- coordinates of the hexagons. Coordinates do not exceed 500 by their absolute values.nnThere are multiple cases. Process to the end of file.nnOutputnnOutput the number of islands in the resulting map and their areas. List areas in the ascending order.nnSample Inputnn7 4n0 1n2 1n3 0n1 -1n2 1n1 0n0 0nSample Outputnn2n2 3

World Islands

10-25

问题描述 :nnDubai is a haven for the rich. The government of Dubai finds a good way to make money. They built a lot of artificial islands on the sea and sell them. These islands are shaped into the continents of the world, so they are called “world islands”. All islands are booked out now. The billionaires who buy these islands wants to make friends with each other, so they want these islands all be connected by bridges. Bill Gates also has booked an island, but he is the only one who doesn’t want his island to be connected with other islands, because he prefer to travel on the sea on his old landing craft which is used in the Invasion of Normandy in World War II. Fortunately, Bill doesn’t care about which island is saved for him, so Dubai government can still find the best way to build the bridges. The best way means that the total length of the bridges is minimum. In a word, if there are n islands, what they should do is to build n�1�72 bridges connecting n-1 islands, and give the rest island to Bill Gates. They can give any island to Bill Gates. Now they pay you good money to help them to find out the best way to build the bridges. nPlease note;n1.An island can be considered as a point.n2.A bridge can be considered as a line segment connecting two islands.n3.A bridge connects with other bridges only at the islands.n输入:nnThe first line is an integer indicating the number of test cases.nFor each test case, the first line is an integer n representing the number of islands.(0

Islands and Bridges

08-11

Problem DescriptionnGiven a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with each island. We call a Hamilton path the best triangular Hamilton path if it maximizes the value described below. nnSuppose there are n islands. The value of a Hamilton path C1C2...Cn is calculated as the sum of three parts. Let Vi be the value for the island Ci. As the first part, we sum over all the Vi values for each island in the path. For the second part, for each edge CiCi+1 in the path, we add the product Vi*Vi+1. And for the third part, whenever three consecutive islands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+2. nnMost likely but not necessarily, the best triangular Hamilton path you are going to find contains many triangles. It is quite possible that there might be more than one best triangular Hamilton paths; your second task is to find the number of such paths. nnnnInputnThe input file starts with a number q (q<=20) on the first line, which is the number of test cases. Each test case starts with a line with two integers n and m, which are the number of islands and the number of bridges in the map, respectively. The next line contains n positive integers, the i-th number being the Vi value of island i. Each value is no more than 100. The following m lines are in the form x y, which indicates there is a (two way) bridge between island x and island y. Islands are numbered from 1 to n. You may assume there will be no more than 13 islands. nnn nnInputnThe input file starts with a number q (q<=20) on the first line, which is the number of test cases. Each test case starts with a line with two integers n and m, which are the number of islands and the number of bridges in the map, respectively. The next line contains n positive integers, the i-th number being the Vi value of island i. Each value is no more than 100. The following m lines are in the form x y, which indicates there is a (two way) bridge between island x and island y. Islands are numbered from 1 to n. You may assume there will be no more than 13 islands. n nnOutputnFor each test case, output a line with two numbers, separated by a space. The first number is the maximum value of a best triangular Hamilton path; the second number should be the number of different best triangular Hamilton paths. If the test case does not contain a Hamilton path, the output must be `0 0'. nnNote: A path may be written down in the reversed order. We still think it is the same path. n nnSample Inputn2n3 3n2 2 2n1 2n2 3n3 1n4 6n1 2 3 4n1 2n1 3n1 4n2 3n2 4n3 4n nnSample Outputn22 3n69 1n

有趣 的 ponds and islands 递归

02-04

今天在教室的黑板上看到了这个题目,觉得挺有趣。rn:rn给出一个矩阵,即通 ponds 或者 islands 的个数, 相连的 0 为一个pond , 相连的 1 为 一个island。 rn(其实跟找迷宫差不多)rn这是我写的递归程序,可是得不到正确答案。 请高手指教我错在哪里了??rnrn数据: 每个矩阵开头一行是说明列行大小的rnrn5 5rn0 1 0 1 0rn0 1 0 1 0rn0 0 0 1 0rn1 1 0 0 1rn0 0 0 1 0rn6 10rn0 0 0 1 0 0 1 0 1 0rn1 1 0 1 1 0 1 0 1 0rn0 0 0 0 0 0 0 0 0 0rn1 1 1 0 1 1 1 0 1 1rn0 0 0 0 1 1 1 0 1 0rn1 1 1 0 0 0 0 0 0 0rn10 10rn0 0 0 0 1 0 0 0 0 0rn1 1 0 1 1 1 1 0 1 1rn0 0 0 0 1 0 0 0 0 0rn1 0 0 0 1 1 1 1 0 1rn0 0 0 0 0 1 0 0 0 0rn1 0 0 0 1 1 1 0 1 1rn0 0 0 0 0 0 0 0 0 0rn1 1 1 1 1 1 1 1 1 1rn0 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0 1 0rnrnrnrn[code=C/C++]rn#include rn#include rn#include rn#include rnusing namespace std;rnrntypedef struct nodernrn int data;// 0 for island 1 for pondrn bool visited;rnrnnode;rnrnvoid recMark(node **Matrix, int y, int x, int flag)//rec find and markrnrn Matrix[y][x].visited = true;rn if(Matrix[y-1][x].data = flag)rn recMark(Matrix, y-1, x, flag);rn if(Matrix[y][x+1].data = flag)rn recMark(Matrix, y, x+1, flag);rn if(Matrix[y+1][x].data = flag)rn recMark(Matrix, y+1, x, flag);rn if(Matrix[y][x-1].data = flag)rn recMark(Matrix, y, x-1, flag);rnrnrnrnrnrnvoid loopFind(node **Matrix, int y , int x)//y and x represent the size of the matrixrnrn int pondCounts = 0;rn int islandCounts = 0;rn for(int _y = 1; _y <= y-2; _y++ )rn rn for(int _x = 1; _x <= x-2 ; _x++)rn rn if(Matrix[_y][_x].visited == false)//if not visited, then visit all relatedrn rn if(Matrix[_y][_x].data == 0)rn rn recMark(Matrix, _y, _x, 0);rn pondCounts++;rn rn else if(Matrix[_y][_x].data == 1)rn rn recMark(Matrix, _y, _x, 1);rn islandCounts++;rnrn rn rn rn rn cout<<"the number of pond: "<>y>>x;rn node theMatrix[y+2][x+2];rn for(int _x = 0;_x>theMatrix[tempX][tempY].data;rnrn rn rnrn //cout<<"reach here "<

没有更多推荐了,返回首页