算法之美-java图的表示(邻接矩阵、邻接表、有向、无向图)

7 12
1 2
6 1
3 1
2 3
2 4
2 5
2 7
3 4
4 5
5 6
5 7
6 7

6
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
1 1 0 0 0 0
1 0 1 0 0 0
1 0 0 1 0 0 

4
1 2 0 3
1 3 2 1
2 3 2 3
1 2 1 1 

4 4
7 8 9 10
6 15 16 11
5 14 13 12
4 3 2 1

4 5
1 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 0 1 1

 4 4
7 8 9 10
6 15 16 11
5 14 13 12
4 3 2 1

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Test{
static List<LinkedList<Integer>> totallist;
static int[][] Map;
static boolean[] visited;
static int MaxCount;
static int Count;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int R = scanner.nextInt();
int C = scanner.nextInt();
totallist = new ArrayList<LinkedList<Integer>>();
Map = new int[R][C];
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
totallist.add(new LinkedList<Integer>());
Map[i][j] = scanner.nextInt();
}
}
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
if(i-1>=0&&Map[i][j]-Map[i-1][j]>0) {
totallist.get(i*C+j).add((i-1)*C+j);
}
if(i+1<R&&Map[i][j]-Map[i+1][j]>0) {
totallist.get(i*C+j).add((i+1)*C+j);
}
if(j-1>=0&&Map[i][j]-Map[i][j-1]>0) {
totallist.get(i*C+j).add(i*C+j-1);
}
if(j+1<C&&Map[i][j]-Map[i][j+1]>0) {
totallist.get(i*C+j).add(i*C+j+1);
}
}
}
MaxCount=0;
for(int i=0;i<totallist.size();i++) {
Count = 0;
visited = new boolean[R*C];
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(i);
visited[i] = true;
while(!queue.isEmpty()) {
Integer top = queue.poll();
for(Integer item:totallist.get(top)) {
if(!visited[item]) {
Count++;
visited[item] = true;
queue.add(item);
}
}
}
MaxCount = MaxCount>Count?MaxCount:Count;
}
System.out.println(MaxCount);

}
}

4 5
1 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 0 1 1


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Test{
static int R;
static int C;
static int[][] Map;
static boolean[][] visited;
static int count;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
R = scanner.nextInt();
C = scanner.nextInt();
Map = new int[R][C];
visited = new boolean[R][C];
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
Map[i][j] = scanner.nextInt();
}
}
Queue<Integer[]> queue = new LinkedList<Integer[]>();
int totalCount = 0;
for(int i=0;i<R;i++) {
for(int j=0;j<C;j++) {
count = 0;
float sum = 0;
if(Map[i][j]==1&&(!visited[i][j])) {
visited[i][j] = true;
count++;
queue.add(new Integer[] {i,j});
while(!queue.isEmpty()) {
Integer[] tops = queue.poll();
int m = tops[0];
int n = tops[1];
if(m<R-1) {//只有右边和下边的，因为从上面来的，说明上面已经visitied了，不会再次访问
if(Map[i+1][j]==1&&(!visited[i+1][j])) {
visited[i+1][j] = true;
count++;
queue.add(new Integer[] {i+1,j});
}
}
if(n<C-1) {
if(Map[i][j+1]==1&&(!visited[i][j+1])) {
visited[i][j+1] = true;
count++;
queue.add(new Integer[] {i,j+1});
}
}
}
sum+=count;
totalCount+=Math.round(sum/2);
}
}
}
System.out.println(totalCount);
}
}


6
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
1 1 0 0 0 0
1 1 1 1 0 0
1 1 1 0 0 0
public class DGraph {
static int[][] Map;
static boolean[] visited;
static int MaxCount;
static int Count;
static int N;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();//点的个数
Map = new int[N][N];
visited = new boolean[N*N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
int num = sc.nextInt();
Map[i][j] = num;
}
}

MaxCount = 0;
for(int i =0;i<N;i++) {
for(int j =0;j<N;j++) {
int sum = Map[i][j];
DFS(i,j,sum);
}
}

System.out.println(MaxCount);

}
private static void DFS(int i,int j,int sum) {
// TODO Auto-generated method stub
if(i==N-1&&j==N-1) {
MaxCount = MaxCount<sum?sum:MaxCount;
return;
}
if(i<N-1) {
DFS(i+1,j, sum+Map[i+1][j]);
}
if(j<N-1) {
DFS(i,j+1, sum+Map[i][j+1]);
}
}

}

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Test {
static int[][] Map;
static boolean[][] visited;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
Map = new int[N][N];
visited = new boolean[N][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
Map[i][j] = scanner.nextInt();
}
}
int[][] result = new int[N][N];
int answer = 0;
Queue<int[]> queue = new LinkedList<int[]>();
int sum = Map[0][0];
queue.add(new int[] { 0, 0, sum });
visited[0][0] = true;
int startNum = Map[0][0];
result[0][0] = startNum;
while (!queue.isEmpty()) {
int[] top = queue.poll();
int x = top[0];
int y = top[1];
int total = top[2];
answer = answer > total ? answer : total;
if (x == N - 1 && y == N - 1) {
answer = total;
break;
}
if (x < N - 1) {
result[x + 1][y] = result[x + 1][y]<result[x][y] + Map[x + 1][y]?result[x][y] + Map[x + 1][y]:result[x + 1][y];
queue.add(new int[] { x + 1, y, result[x + 1][y] });
}
if (y < N - 1) {
result[x][y + 1] = result[x][y+ 1]<result[x][y] + Map[x][y + 1]?result[x][y] + Map[x][y + 1]:result[x][y+ 1];
queue.add(new int[] { x, y + 1, result[x][y + 1] });

}
}
System.out.println(result[N - 1][N - 1]);
}

}

5

4
1 2 0 3
1 3 2 1
2 3 2 3
1 2 1 1

5
2 0 2 0 0
1 2 1 2 2
1 2 1 0 0
2 1 0 0 1
0 1 1 0 2

public class Test{
static int[][] Map;
static int[][] result;
static int N;
static boolean[][] visited;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
Map = new int[N][N];
result = new int[N][N];
visited = new boolean[N][N];
for(int i =0;i<N;i++) {
for(int j =0;j<N;j++) {
Map[i][j] =  scanner.nextInt();
}
}
Queue<int[]> queue = new LinkedList<int[]>();
queue.add(new int[] {0,0});
visited[0][0] = true;
while(!queue.isEmpty()) {
int[] top = queue.poll();
int startIndex = top[0];
int endIndex = top[1];
if(startIndex==N-1&&endIndex==N-1) {
break;
}
if(Map[startIndex][endIndex]!=0) {
for(int i=1;i<=Map[startIndex][endIndex];i++) {
if(startIndex+i<N&&!visited[startIndex+i][endIndex]) {
result[startIndex+i][endIndex] = result[startIndex][endIndex]+1;
visited[startIndex+i][endIndex]  = true;
queue.add(new int[] {startIndex+i,endIndex});
}
if(endIndex+i<N&&!visited[startIndex][endIndex+i]) {
result[startIndex][endIndex+i] = result[startIndex][endIndex]+1;
visited[startIndex][endIndex+i]  = true;
queue.add(new int[] {startIndex,endIndex+i});
}
}
}
}
System.out.println(result[N-1][N-1]);
}
}

3

6

5 5
0 0 0 1 0
0 1 -1 0 0
1 0 0 2 2
1 1 0 0 -1
0 0 1 0 0


public class Test{
static int[][] Map;
static int[][] result;
static int N;
static int M;
static boolean[][] visited;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int M = scanner.nextInt();
Map = new int[N][M];
result = new int[N][M];
visited = new boolean[N][M];
for(int i =0;i<N;i++) {
for(int j =0;j<M;j++) {
Map[i][j] =  scanner.nextInt();
}
}
Queue<int[]> queue = new LinkedList<int[]>();
queue.add(new int[] {0,0});
visited[0][0] = true;
int startNum = Map[0][0];
result[0][0] = startNum;
while(!queue.isEmpty()) {
int[] top = queue.poll();
int startIndex = top[0];
int endIndex = top[1];
if(startIndex==N-1&&endIndex==N-1) {
break;
}
if(Map[startIndex][endIndex]!=-1) {
if(startIndex+1<N) {
result[startIndex+1][endIndex] = result[startIndex+1][endIndex]<result[startIndex][endIndex]+Map[startIndex+1][endIndex]?result[startIndex][endIndex]+Map[startIndex+1][endIndex]:result[startIndex+1][endIndex];
visited[startIndex+1][endIndex]  = true;
queue.add(new int[] {startIndex+1,endIndex});
}
if(endIndex+1<N) {
result[startIndex][endIndex+1] = result[startIndex][endIndex+1]<result[startIndex][endIndex]+Map[startIndex][endIndex+1]?result[startIndex][endIndex]+Map[startIndex][endIndex+1]:result[startIndex][endIndex+1];
visited[startIndex][endIndex+1]  = true;
queue.add(new int[] {startIndex,endIndex+1});
}
}
}
System.out.println(result[N-1][M-1]);
}
}


06-30 818
11-23 1357
04-04 487
11-04 1万+
07-12 6928
01-09
07-03 8356
12-12 688
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客