问题:
给定一个0
和1
的非空的二维数组网格
,一个岛是一个1
(表示陆地)的组,4个方向(水平或垂直)连接。你可以假设网格的所有四条边都被水包围。
计算不同岛屿的数量。当一个岛被认为与另一个岛相同时,它们有相同的形状,或在旋转后的形状相同(90,180,或270度)或翻转(左/右方向或向上/向下方向)。
样例:
Example 1:
Input: [[1,1,0,0,0],[1,0,0,0,0],[0,0,0,0,1],[0,0,0,1,1]] Output: 1 Explanation: The island is look like this: 11000 10000 00001 00011 Notice that: 11 1 and 1 11 are considered same island shapes. Because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.
Example 2:
Input: [[1,1,1,0,0],[1,0,0,0,1],[0,1,0,0,1],[0,1,1,1,0]] Output: 2 Explanation: The island is look like this: 11100 10001 01001 01110 Here are the two distinct islands: 111 1 and 1 1 Notice that: 111 1 and 1 111 are considered same island shapes. Because if we flip the first array in the up/down direction, then they have the same shapes.
注意事项
每个维度在给定网格
的长度不超过50
。
python:
class Solution:
"""
@param grid: the 2D grid
@return: the number of distinct islands
"""
def strReverse1(self, src):
if src == "":
return ""
return src[::-1]
def changeToFalse(self, grid, m, n, mMin, mMax, nMin, nMax):
if m < 0 or m >= len(grid) or n < 0 or n >= len(grid[0]):
return
if grid[m][n] == 1:
grid[m][n] = 2
Mmin = min(m, mMin[0])
del mMin[0]
mMin.append(Mmin)
Mmax = max(m, mMax[0])
del mMax[0]
mMax.append(Mmax)
Nmin = min(n, nMin[0])
del nMin[0]
nMin.append(Nmin)
Nmax = max(n, nMax[0])
del nMax[0]
nMax.append(Nmax)
self.changeToFalse(grid, m-1, n, mMin, mMax, nMin, nMax)
self.changeToFalse(grid, m+1, n, mMin, mMax, nMin, nMax)
self.changeToFalse(grid, m, n-1, mMin, mMax, nMin, nMax)
self.changeToFalse(grid, m, n+1, mMin, mMax, nMin, nMax)
def numDistinctIslands2(self, grid):
# write your code here
if len(grid) == 0 or len(grid[0]) == 0:
return 0
resultStr = []
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
mMin = []
mMin.append(i)
mMax = []
mMax.append(i)
nMin = []
nMin.append(j)
nMax = []
nMax.append(j)
self.changeToFalse(grid, i, j, mMin, mMax, nMin, nMax)
temp = ""
temp90 = ""
temp180 = ""
temp270 = ""
templ2r = ""
tempt2b = ""
flag = False
for p in range(mMin[0], mMax[0]+1, 1):
for q in range(nMin[0], nMax[0]+1, 1):
temp = temp + str(grid[p][q])
if p != mMax[0]:
temp += "_"
temp180 = self.strReverse1(temp)
for p90 in range(nMin[0], nMax[0]+1, 1):
for q90 in range(mMax[0], mMin[0]-1, -1):
temp90 = temp90 + str(grid[q90][p90])
if p90 != nMax[0]:
temp90 += "_"
temp270 = self.strReverse1(temp90)
for pl2r in range(mMin[0], mMax[0]+1, 1):
for ql2r in range(nMax[0], nMin[0]-1, -1):
templ2r = templ2r + str(grid[pl2r][ql2r])
if pl2r != mMax[0]:
templ2r += "_"
tempt2b = self.strReverse1(templ2r)
if len(resultStr) == 0:
resultStr.append(temp)
resultStr.append(temp90)
resultStr.append(temp180)
resultStr.append(temp270)
resultStr.append(templ2r)
resultStr.append(tempt2b)
else:
for index in range(len(resultStr)):
if resultStr[index] == temp or resultStr[index] == temp90 or resultStr[index] == temp180 or resultStr[index] == temp270 or resultStr[index] == templ2r or resultStr[index] == tempt2b:
flag = True
break
if flag == False:
resultStr.append(temp)
resultStr.append(temp90)
resultStr.append(temp180)
resultStr.append(temp270)
resultStr.append(templ2r)
resultStr.append(tempt2b)
return len(resultStr)//6
C++:
class Solution {
public:
/**
* @param grid: the 2D grid
* @return: the number of distinct islands
*/
void changeToFalse(vector<vector<int>> &grid, int m, int n, int &mMin, int &mMax, int &nMin, int &nMax)
{
if (m < 0 || m >= grid.size() || n < 0 || n >= grid[0].size())
{
return;
}
if (grid[m][n] == 1)
{
grid[m][n] = 2;
mMin = min(m, mMin);
mMax = max(m, mMax);
nMin = min(n, nMin);
nMax = max(n, nMax);
changeToFalse(grid, m - 1, n, mMin, mMax, nMin, nMax);
changeToFalse(grid, m + 1, n, mMin, mMax, nMin, nMax);
changeToFalse(grid, m, n - 1, mMin, mMax, nMin, nMax);
changeToFalse(grid, m, n + 1, mMin, mMax, nMin, nMax);
}
}
string stringRev(string src)
{
string temp = src;
reverse(temp.begin(),temp.end());
return temp;
}
int numDistinctIslands2(vector<vector<int>> &grid)
{
// write your code here
if (grid.size() == 0 || grid[0].size() == 0)
{
return 0;
}
vector<string> resultStr;
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[i].size(); j++)
{
if (grid[i][j] == 1)
{
int mMin = i;
int mMax = i;
int nMin = j;
int nMax = j;
changeToFalse(grid, i, j, mMin, mMax, nMin, nMax);
string temp = "";
string temp90 = "";
string temp180 = "";
string temp270 = "";
string templ2r = "";
string tempt2b = "";
bool flag = false;
for (int p = mMin; p <= mMax; p++)
{
for (int q = nMin; q <= nMax; q++)
{
temp = temp + to_string(grid[p][q]);
}
if(p != mMax)
{
temp += "_";
}
}
temp180 = stringRev(temp);
for (int p90 = nMin; p90 <= nMax; p90++)
{
for (int q90 = mMax; q90 >= mMin; q90--)
{
temp90 = temp90 + to_string(grid[q90][p90]);
}
if(p90 != nMax)
{
temp90 += "_";
}
}
temp270 = stringRev(temp90);
for (int pl2r = mMin; pl2r <= mMax; pl2r++)
{
for (int ql2r = nMax; ql2r >= nMin; ql2r--)
{
templ2r = templ2r + to_string(grid[pl2r][ql2r]);
}
if(pl2r != mMax)
{
templ2r += "_";
}
}
tempt2b = stringRev(templ2r);
if (resultStr.empty())
{
resultStr.push_back(temp);
resultStr.push_back(temp90);
resultStr.push_back(temp180);
resultStr.push_back(temp270);
resultStr.push_back(templ2r);
resultStr.push_back(tempt2b);
}
else {
for (int index = 0; index < resultStr.size(); index++)
{
if (resultStr[index] == temp || resultStr[index] == temp90 || resultStr[index] == temp180 || resultStr[index] == temp270 || resultStr[index] == templ2r || resultStr[index] == tempt2b)
{
flag = true;
break;
}
}
if (flag == false)
{
resultStr.push_back(temp);
resultStr.push_back(temp90);
resultStr.push_back(temp180);
resultStr.push_back(temp270);
resultStr.push_back(templ2r);
resultStr.push_back(tempt2b);
}
}
}
}
}
return int(resultStr.size()/6);
}
};