797.所有可能的路径:
class Solution {
List<List<Integer>> ans=new LinkedList<>();
List<Integer> list=new LinkedList<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
list.add(0);
backTracking(graph,0);
return ans;
}
private void backTracking(int[][] graph,int x){
if (x==graph.length-1){
ans.add(new LinkedList<>(list));
}
for (int i = 0; i < graph[x].length; i++) {
list.add(graph[x][i]);
backTracking(graph,graph[x][i]);
list.remove(list.size()-1);
}
}
}
岛屿数量:
深搜:
class Solution {
int ans=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
public int numIslands(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]=='1'){
ans++;
grid[i][j]='0';
backTracking(grid,i,j);
}
}
}
return ans;
}
private void backTracking(char[][] grid,int x,int y){
for (int i = 0; i < 4; i++) {
int newx=x+dx[i];
int newy=y+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]=='0')continue;
grid[newx][newy]='0';
backTracking(grid,newx,newy);
}
}
}
广搜:
class Solution {
int ans=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
public int numIslands(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]=='1'){
ans++;
bfs(grid,new boolean[grid.length][grid[0].length],i,j);
}
}
}
return ans;
}
private void bfs(char[][] grid,boolean[][] vis,int x,int y) {
Deque<Pair> pairDeque=new LinkedList<>();
pairDeque.push(new Pair(x,y));
while (!pairDeque.isEmpty()){
Pair pair = pairDeque.pop();
int x1 = pair.x;
int y1 = pair.y;
for (int i = 0; i < 4; i++) {
int newx=x1+dx[i];
int newy=y1+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]=='0'){
continue;
}
grid[newx][newy]='0';
pairDeque.push(new Pair(newx,newy));
}
}
}
class Pair{
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
岛屿的最大面积:
int max=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
int count=1;
public int maxAreaOfIsland(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==1){
count=0;
dfs(grid,i,j);
max=Math.max(max,count);
}
}
}
return max;
}
private void dfs(int[][] grid,int x,int y){
count++;
grid[x][y]=0;
for (int i = 0; i < 4; i++) {
int newx=x+dx[i];
int newy=y+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]==0){
continue;
}
grid[newx][newy]=0;
dfs(grid,newx,newy);
}
}
public static void main(String[] args) {
LeetCode695dfs leetCode695=new LeetCode695dfs();
int max = leetCode695.maxAreaOfIsland(new int[][]{{1}});
System.out.println(max);
}
class Solution {
int max=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
int count=1;
public int maxAreaOfIsland(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==1){
count=0;
bfs(grid,i,j);
max=Math.max(count,max);
}
}
}
return max;
}
private void bfs(int[][] grid,int x,int y){
Deque<pair> pairDeque=new LinkedList<>();
pairDeque.push(new pair(x,y));
grid[x][y]=0;
while (!pairDeque.isEmpty()){
pair pop = pairDeque.pop();
count++;
int x1 = pop.x;
int y1 = pop.y;
for (int i = 0; i < 4; i++) {
int newx=x1+dx[i];
int newy=y1+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]==0){
continue;
}
grid[newx][newy]=0;
pairDeque.push(new pair(newx,newy));
}
}
}
class pair{
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
1020:飞地的数量:
class Solution {
int ans=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
boolean flag=true;
public int numEnclaves(int[][] grid) {
for (int i = 0; i < grid[0].length; i++) {
if (grid[0][i]==1){
grid[0][i]=0;
dfs(grid,0,i);
}
if (grid[grid.length-1][i]==1){
grid[grid.length-1][i]=0;
dfs(grid,grid.length-1,i);
}
}
for (int i = 0; i < grid.length; i++) {
if (grid[i][0]==1){
grid[i][0]=0;
dfs(grid,i,0);
}
if (grid[i][grid[0].length-1]==1){
grid[i][grid[0].length-1]=0;
dfs(grid,i,grid[0].length-1);
}
}
for (int i = 1; i < grid.length-1; i++) {
for (int j = 1; j < grid[0].length-1; j++) {
if (grid[i][j]==1) ans++;
}
}
return ans;
}
private void dfs(int[][] grid,int x,int y){
for (int i = 0; i < 4; i++) {
int newx=x+dx[i];
int newy=y+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]==0){
continue;
}
grid[newx][newy]=0;
dfs(grid,newx,newy);
}
}
}
这题广搜更胜一筹:
public class LeetCode1020bfs {
int ans=0;
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
public int numEnclaves(int[][] grid) {
for (int i = 0; i < grid[0].length; i++) {
if (grid[0][i]==1){
grid[0][i]=0;
bfs(grid,0,i);
}
if (grid[grid.length-1][i]==1){
grid[grid.length-1][i]=0;
bfs(grid,grid.length-1,i);
}
}
for (int i = 0; i < grid.length; i++) {
if (grid[i][0]==1){
grid[i][0]=0;
bfs(grid,i,0);
}
if (grid[i][grid[0].length-1]==1){
grid[i][grid[0].length-1]=0;
bfs(grid,i,grid[0].length-1);
}
}
for (int i = 1; i < grid.length-1; i++) {
for (int j = 1; j < grid[0].length-1; j++) {
if (grid[i][j]==1) ans++;
}
}
return ans;
}
private void bfs(int[][] grid,int x,int y){
Deque<pair> deque=new LinkedList<>();
deque.push(new pair(x,y));
grid[x][y]=0;
while (!deque.isEmpty()){
pair pop = deque.pop();
int x1 = pop.x;
int y1 = pop.y;
for (int i = 0; i < 4; i++) {
int newx=x1+dx[i];
int newy=y1+dy[i];
if (newx<0||newy<0||newx>=grid.length||newy>=grid[0].length||grid[newx][newy]==0){
continue;
}
grid[newx][newy]=0;
deque.push(new pair(newx,newy));
}
}
}
class pair{
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
被围绕的区域:
class Solution {
int[] dx={0,1,-1,0};
int[] dy={1,0,0,-1};
public void solve(char[][] board) {
int[][] flag=new int[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
if (board[i][0]=='O'){
dfs(board,i,0,flag);
}
if (board[i][board[0].length-1]=='O'){
dfs(board,i,board[0].length-1,flag);
}
}
for (int i = 0; i < board[0].length; i++) {
if (board[0][i]=='O'){
dfs(board,0,i,flag);
}
if (board[board.length-1][i]=='O'){
dfs(board,board.length-1,i,flag);
}
}
for (int i = 1; i <board.length-1 ; i++) {
for (int j = 1; j < board[0].length-1; j++) {
if (flag[i][j]==0&&board[i][j]=='O'){
board[i][j]='X';
}
}
}
}
private void dfs(char[][] board, int x, int y, int[][] flag) {
flag[x][y]=1;
for (int i = 0; i < 4; i++) {
int newx=x+dx[i];
int newy=y+dy[i];
if (newx<0||newy<0||newx>=board.length||newy>=board[0].length||board[newx][newy]=='X'||flag[newx][newy]==1){
continue;
}
dfs(board,newx,newy,flag);
}
}
}
****最大人工岛:
class Solution {
int[] dx = {0, 1, -1, 0};
int[] dy = {1, 0, 0, -1};
int isLandNumber=2;
int count=0;
Map<Integer,Integer> map=new HashMap<>();
public int largestIsland(int[][] grid) {
int res=0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==1){
count=0;
dfs(grid,i,j,isLandNumber);
map.put(isLandNumber,count);
res=Math.max(res,count);
isLandNumber++;
}
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
int area=0;
Set<Integer> set=new HashSet<>();
if (grid[i][j]==0){
for (int k = 0; k < 4; k++) {
int x=i+dx[k];
int y=j+dy[k];
if (x<0||y<0||x>=grid.length||y>=grid[0].length||grid[x][y]==0||set.contains(grid[x][y])){
continue;
}
area+=map.get(grid[x][y]);
set.add(grid[x][y]);
}
}
area++;
res=Math.max(res,area);
}
}
return res;
}
private void dfs(int[][] grid, int i, int j, int isLandNumber) {
grid[i][j]=isLandNumber;
count++;
for (int k = 0; k < 4; k++) {
int x=i+dx[k];
int y=j+dy[k];
if (x<0||y<0||x>=grid.length||y>=grid[0].length||grid[x][y]!=1){
continue;
}
dfs(grid,x,y,isLandNumber);
}
}
}
****钥匙和房间:
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
boolean[] vis=new boolean[rooms.size()];
dfs(rooms,vis,0);
for (int i = 0; i < vis.length; i++) {
if (vis[i]==false) return false;
}
return true;
}
private void dfs(List<List<Integer>> rooms, boolean[] vis, int i) {
if (vis[i]==true){
return;
}
List<Integer> list = rooms.get(i);
vis[i]=true;
for (Integer j : list) {
dfs(rooms,vis,j);
}
}
***单词接龙:
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set=new HashSet<>(wordList);
if (!set.contains(endWord)){
return 0;
}
LinkedList<String> queue=new LinkedList<>();
Map<String,Integer> map=new HashMap<>();
queue.offerLast(beginWord);
map.put(beginWord,1);
while (!queue.isEmpty()){
String s = queue.removeFirst();
Integer path = map.get(s);
for (int i = 0; i < s.length(); i++) {
char[] chars = s.toCharArray();
for (char j = 'a'; j <='z'; j++) {
chars[i]=j;
String newword=new String(chars);
if (newword.equals(endWord)){
return path+1;
}
if (set.contains(newword)&&!map.containsKey(newword)){
map.put(newword,path+1);
queue.offerLast(newword);
}
}
}
}
return 0;
}
}
***岛屿的周长:
class Solution {
public int islandPerimeter(int[][] grid) {
int sum=0;
int cover=0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==1){
sum++;
if (i-1>=0&&grid[i-1][j]==1) cover++;
if (j-1>=0&&grid[i][j-1]==1) cover++;
}
}
}
return sum*4-cover*2;
}
}
class Solution {
int[] dx={0,-1,1,0};
int[] dy={1,0,0,-1};
int C=0;
public int islandPerimeter(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==1){
for (int k = 0; k < 4; k++) {
int x=i+dx[k];
int y=j+dy[k];
if (x<0||y<0||x>=grid.length||y>=grid[0].length||grid[x][y]==0){
C++;
}
}
}
}
}
return C;
}
}