project euler 81

本文探讨了一个特定的路径和问题,即在一个80x80的矩阵中寻找从左上角到右下角的最短路径,仅允许向右或向下移动。通过使用Dijkstra算法实现,提供了一个具体的Java程序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 81


Path sum: two ways

In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down, is indicated in bold red and is equal to 2427.

         
131 673 234 103 18
201 96 342 965 150
630 803 746 422 111
537 699 497 121 956
805 732 524 37 331

Find the minimal path sum, in matrix.txt (right click and “Save Link/Target As…”), a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right by only moving right and down.


路径和:两个方向

在如下的5乘5矩阵中,从左上方到右下方始终只向右或向下移动的最小路径和为2427,由标注红色的路径给出。

         
131 673 234 103 18
201 96 342 965 150
630 803 746 422 111
537 699 497 121 956
805 732 524 37 331

在这个31K的文本文件matrix.txt(右击并选择“目标另存为……”)中包含了一个80乘80的矩阵,求出从该矩阵的左上方到右下方始终只向右和向下移动的最小路径和。

package projecteuler;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Stack;

import junit.framework.TestCase;

public class Prj81 extends TestCase {

	public static final String PATH = "E:\\whua\\mathWorkspace\\test_jgraph\\src\\projecteuler\\Prj81.txt";
	public static final int ROW_COUNT = 80;

	public static int[][] DATA;

	public void testPathSumTwoWays() {

		DijkStraShortPath shortPath = new DijkStraShortPath() {

			class Vertex implements IVertex {

				private int m;

				@Override
				public String toString() {
					return "Vertex [m=" + m + ", n=" + n + "]";
				}

				private int n;
				private int rowCount;

				public Vertex(int m, int n, int Col) {
					this.m = m;
					this.n = n;
					this.rowCount = Col;
				}

				@Override
				public int getVertexId() {

					return m * rowCount + n;
				}

				@Override
				public Object getDescrp() {
					return "(" + m + "," + n + ")";
				}

			}

			@Override
			public IVertex id2Vertex(int id) {
				return new Vertex(id / ROW_COUNT, id % ROW_COUNT, ROW_COUNT);
			}

			@Override
			public void createEdge() {

				int[][] data = readStr();
				int rowCount = data[0].length;
				int numOfVertex = rowCount * rowCount;
				this.flag = new boolean[numOfVertex];
				this.edge = new int[numOfVertex][numOfVertex];

				for (int i = 0; i < numOfVertex; i++) {
					for (int j = 0; j < numOfVertex; j++) {
						edge[i][j] = MAX_MASK;
					}
				}

				for (int i = 0; i < rowCount - 1; i++) {
					for (int j = 0; j < rowCount; j++) {
						edge[i * rowCount + j][(i + 1) * rowCount + j] = data[i][j]
								+ data[i + 1][j];
					}
				}

				for (int i = 0; i < rowCount; i++) {
					for (int j = 0; j < rowCount - 1; j++) {
						edge[i * rowCount + j][i * rowCount + j + 1] = data[i][j]
								+ data[i][j + 1];
					}
				}

			}

			@Override
			public Object trace(int startId, int findId) {

				System.out.println("START++++");
				int sum = 0;
				sum += DATA[findId / ROW_COUNT][findId % ROW_COUNT];
				IVertex vt = path[findId];
				Stack<IVertex> stack = new Stack<IVertex>();
				while (vt.getVertexId() != startId) {
					stack.push(vt);
					int id = vt.getVertexId();
					sum += DATA[id / ROW_COUNT][id % ROW_COUNT];
					vt = path[vt.getVertexId()];
				}

				sum += DATA[startId / ROW_COUNT][startId % ROW_COUNT];
				System.out.println( id2Vertex(findId));
				Object[] objArr = stack.toArray();
				for (Object o : objArr) {
					System.out.println(o);
				}
				System.out.println(path[startId]);
				System.out.println("END++++");
				System.out.println("sum=" + sum);
				return objArr;

			}
		};

		shortPath.findPath(0);
		shortPath.trace(0, ROW_COUNT * ROW_COUNT - 1);
	}

	static int[][] readStr() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(PATH), "utf8"));

			int[][] ret = new int[ROW_COUNT][ROW_COUNT];
			int count = 0;

			String str = "";
			while ((str = reader.readLine()) != null) {
				String[] strs = str.split(",");

				for (int i = 0; i < strs.length; i++) {
					ret[count][i] = Integer.parseInt(strs[i]);
				}

				count++;
			}

			reader.close();
			DATA = ret;
			return ret;
			// int[][] ret = new int[][] { { 131, 673, 234, 103, 18 },
			// { 201, 96, 342, 965, 150 }, { 630, 803, 746, 422, 111 },
			// { 537, 699, 497, 121, 956 }, { 805, 732, 524, 37, 331 } };
			//
			// DATA = ret;
			// return ret;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public interface IVertex {

		int getVertexId();

		Object getDescrp();
	}

	public static abstract class DijkStraShortPath {

		public static final int MAX_MASK = Integer.MAX_VALUE;
		protected int[][] edge;
		protected boolean[] flag;
		protected IVertex[] path;

		public abstract void createEdge();

		public abstract IVertex id2Vertex(int id);

		public abstract Object trace(int startId, int endId);

		public int[] findPath(int startId) {

			createEdge();

			path = new IVertex[flag.length];
			IVertex start = id2Vertex(startId);
			path[startId] = start;
			int numOfVertex = flag.length;
			int[] dist = new int[numOfVertex];

			for (int i = 0; i < dist.length; i++) {
				dist[i] = edge[start.getVertexId()][i];
			}

			for (int i = 0; i < dist.length; i++) {
				if (i != startId && edge[startId][i] != MAX_MASK) {
					path[i] = id2Vertex(startId);
				}
			}

			dist[startId] = 0;
			flag[startId] = true;

			for (;;) {

				int minId = -1;
				int minVal = MAX_MASK;
				for (int i = 0; i < flag.length; i++) {

					if (!flag[i]) {
						if (dist[i] < minVal) {
							minVal = dist[i];
							minId = i;
						}
					}
				}

				if (minId == -1) {
					break;
				}

				for (int j = 0; j < flag.length; j++) {
					if (j != minId && !flag[j] && edge[minId][j] != MAX_MASK
							&& edge[minId][j] + dist[minId] < dist[j]) {
						dist[j] = edge[minId][j] + dist[minId];
						path[j] = id2Vertex(minId);
					}
				}
				flag[minId] = true;

			}
			return dist;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值