要求:1. 有N 行,M列整数数字存储在一个txt文件中(如下),每个数字都不重复,同行数字用空格分开(可能为N个空格),将其从文件中读取出来。
2. 以最大的数值作为起点,开始连线,每个数字只能连接其上下左右4个点中的一个,且所连数字必须比当前数字小,求用一条线所能连接的数字的最大个数(注意:不是最大和),输出个数及所连接的数字。
3. 考虑执行效率。
示例: 如文件中存储为如下格式,则25作为起点,可以做25-18-3或25-20-19-4连线
,而所求的最长连线应为: 25-24-23-22….3-2-1, 共25个。
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
本人用go语言可以实现找到最长路径的长度,但不知道怎么得到全部路径上的数字
package main
import (
“fmt”
// “time”
)
func check(x, y, m, n int) bool {
if x < 0 || x > n || y < 0 || y > m {
fmt.Print(“true”, “\n”)
return true
}
fmt.Print(“false”, “\n”)
return false
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func dp(r, c, maxx int, ma [5][5]int, le [3][3]int) int {
dir := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
maxx = 0
temx, temy := 0, 0
for t := 0; t < 4; t++ {
temx = r + dir[t][0]
temy = c + dir[t][1]
fmt.Print(temx, temy)
// fmt.Print(ma[temx][temy])
if check(temx, temy, 2, 2) || (ma[temx][temy] > ma[r][c]) {
fmt.Print(ma[r][c])
fmt.Print(“continue”)
continue
}
maxx = max(maxx, dp(temx, temy, maxx, ma, le))
fmt.Print(maxx)
}
le[r][c] = maxx + 1
fmt.Print(“***”, le[r][c], “***”)
return le[r][c]
}
func main() {
var ma = [5][5]int{{1, 8, 7}, {2, 4, 6}, {5, 9, 3}}
var le = [3][3]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
maxx := -1
maxx = max(maxx, dp(0, 1, maxx, ma, le))
fmt.Print(“###”, maxx, “###”)
}
2. 以最大的数值作为起点,开始连线,每个数字只能连接其上下左右4个点中的一个,且所连数字必须比当前数字小,求用一条线所能连接的数字的最大个数(注意:不是最大和),输出个数及所连接的数字。
3. 考虑执行效率。
示例: 如文件中存储为如下格式,则25作为起点,可以做25-18-3或25-20-19-4连线
,而所求的最长连线应为: 25-24-23-22….3-2-1, 共25个。
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
本人用go语言可以实现找到最长路径的长度,但不知道怎么得到全部路径上的数字
package main
import (
“fmt”
// “time”
)
func check(x, y, m, n int) bool {
if x < 0 || x > n || y < 0 || y > m {
fmt.Print(“true”, “\n”)
return true
}
fmt.Print(“false”, “\n”)
return false
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func dp(r, c, maxx int, ma [5][5]int, le [3][3]int) int {
dir := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
maxx = 0
temx, temy := 0, 0
for t := 0; t < 4; t++ {
temx = r + dir[t][0]
temy = c + dir[t][1]
fmt.Print(temx, temy)
// fmt.Print(ma[temx][temy])
if check(temx, temy, 2, 2) || (ma[temx][temy] > ma[r][c]) {
fmt.Print(ma[r][c])
fmt.Print(“continue”)
continue
}
maxx = max(maxx, dp(temx, temy, maxx, ma, le))
fmt.Print(maxx)
}
le[r][c] = maxx + 1
fmt.Print(“***”, le[r][c], “***”)
return le[r][c]
}
func main() {
var ma = [5][5]int{{1, 8, 7}, {2, 4, 6}, {5, 9, 3}}
var le = [3][3]int{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
maxx := -1
maxx = max(maxx, dp(0, 1, maxx, ma, le))
fmt.Print(“###”, maxx, “###”)
}
解决方案
100
仅供参考:
#include <stdio.h>
#define MAXN 100
int m[MAXN+2][MAXN+2];
char d;
int x,y,k,n,w;
char str[10];
void main() {
while (1) {
printf("Input n(1..%d):",MAXN);
fflush(stdout);
rewind(stdin);
if (1==scanf("%d",&n)) {
if (1<=n && n<=MAXN) break;
}
}
y=0 ;for (x=0;x<=n+1;x++) m[y][x]=1;
y=n+1;for (x=0;x<=n+1;x++) m[y][x]=1;
x=0 ;for (y=0;y<=n+1;y++) m[y][x]=1;
x=n+1;for (y=0;y<=n+1;y++) m[y][x]=1;
for (y=1;y<=n;y++) {
for (x=1;x<=n;x++) {
m[y][x]=0;
}
}
x=1;
y=1;
k=0;
d="D";
while (1) {
k++;
if (k>n*n) break;
m[y][x]=k;
switch (d) {
case "D":
if (0==m[y+1][x]) y++;
else {x++;d="R";}
break;
case "R":
if (0==m[y][x+1]) x++;
else {y--;d="U";}
break;
case "U":
if (0==m[y-1][x]) y--;
else {x--;d="L";}
break;
case "L":
if (0==m[y][x-1]) x--;
else {y++;d="D";}
break;
}
}
w=sprintf(str,"%d",n*n);
for (y=1;y<=n;y++) {
for (x=1;x<=n;x++) {
printf(" %0*d",w,m[y][x]);
}
printf("\n");
}
}