基于C#语音实现的俄罗斯套娃程序

?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace RussiaDoll
{
///


/// 结构体用于记录坐标
///

struct Point
{
public int x;
public int y;
}

///
/// 将二维数组中的每个元素看做一个点对象,具有以下属性
///

struct Node
{
public int value; //路口所放俄罗斯套娃的重量
public int maxValue; //从原点出发到达此路口所能获取到俄罗斯套娃的最大重量
public Point nextMaxPoint; //从原点出发到达此路口能够有最大的收获的路径中与此路口相邻的路口坐标
}

class Program
{
private int row;//东西方向道路数
private int col;//南北方向道路数
private Node[,] data;//二维数组
private int lastMaxValue;//可以获得的最大值
private String path = "";//输出路径

Program(){}

//x y为当前点坐标 nextX nextY为相邻点的坐标
private void GetNextMaxVlue(int x, int y, int nextX, int nextY)
{
//计算当前点最大值前先查表 若相邻点最大值不为0则不再调用GetMaxValue函数
int thisMaxValue = data[nextX, nextY].maxValue == 0 ? GetMaxValue(nextX, nextY) : data[nextX, nextY].maxValue;
thisMaxValue += data[x, y].value;
if (thisMaxValue > data[x, y].maxValue)
{ //若临时最值较大 则修改该点的最大值 并记录使之产生最值的点的坐标
data[x, y].maxValue = thisMaxValue;
data[x, y].nextMaxPoint.x = nextX;
data[x, y].nextMaxPoint.y = nextY;
}
}
//获取点的可获得的最大值
private int GetMaxValue(int x, int y)
{
if (x == 0 && y == 0)
{
data[x, y].maxValue = data[x, y].value;
}
else
{//从四个方向递归 递归条件:数组不越界且相邻点的值比当前点小
if (x - 1 >= 0 && data[x - 1, y].value < data[x, y].value)
GetNextMaxVlue(x, y , x - 1, y);
if (x + 1 <= row-1 && data[x + 1, y].value < data[x, y].value)
GetNextMaxVlue(x, y, x + 1, y);
if (y - 1 >= 0 && data[x, y - 1].value < data[x, y].value)
GetNextMaxVlue(x, y, x, y - 1);
if (y + 1 <= col-1 && data[x, y + 1].value < data[x, y].value)
GetNextMaxVlue(x, y, x, y + 1);
}
return data[x, y].maxValue;
}
///
/// 读取txt文件
///

/// 文件路径
/// 读取结果

private bool Read_File(String filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
String line = sr.ReadLine(); //读取txt文件的第一行

String[] content = line.Split(' '); //将第一行转换为数组

if (content.Length == 2)
{ //当第一行中记录了两个数据
row = Int32.Parse(content[0]);
col = Int32.Parse(content[1]);
}
else
{ //出错
return false;
}

data = new Node[row, col]; //为data数组分配row*col的空间

int rowCount = 0; //记录文件输入矩阵的行数

for (int i = 0; i < row; i++)
{ //循环读取矩阵的每一行
line = sr.ReadLine();
content = line.Split(' ');

if (content.Length != col)
{ //判断矩阵列数与col是否相等,如相等则正确,不相等则返回错误
return false;
}

for (int j = 0; j < col; j++)
{
data[i, j].value = Int32.Parse(content[j]); //data[i, j].value对赋值
}

rowCount++; //矩阵行数自增
}
if (row != rowCount)
{ //当矩阵行数与row不相等时,说明矩阵的形式不正确,返回错误
return false;
}
return true;
}
private void Write_File(String filePath)
{
if (!File.Exists(filePath))
{ //不存在此文件时
File.AppendAllText(filePath, path);
}
else
{
File.Delete(filePath);
File.AppendAllText(filePath, path);
}
}
private void GetMaxPath(int i, int j)
{//由于递归所得结果是倒序的 故借助栈来正序输出
Stack stackPath = new Stack();
if (i == 0 && j == 0)
stackPath.Push(data[i, j].value.ToString());
else
{
stackPath.Push(" "+data[i, j].value.ToString());//压栈
GetMaxPath(data[i, j].nextMaxPoint.x, data[i, j].nextMaxPoint.y);
}
while (stackPath.Count > 0)
{
path += stackPath.Pop();//出栈 修改path
}
}
private void GetPath(String readFilePath, String writeFilePath)
{
if (Read_File(readFilePath))
{
int maxi = 0;//记录lastmaxValue的横坐标
int maxj = 0;//记录lastmaxValue的纵坐标
//循环 获取每个点可达的最大值
for (int i = 0; i < row; i++)
{

for (int j = 0; j < col; j++)
{
data[i, j].maxValue = GetMaxValue(i, j);
if (data[i, j].maxValue > lastMaxValue)
{
lastMaxValue = data[i, j].maxValue;
maxi = i;
maxj = j;
}
}
}
//获取能够获得最大值的路径
GetMaxPath(maxi, maxj);
}
else //错误信息
{
path = "提示:输入文件格式有误,请保证格式正确。";
}
Write_File(writeFilePath); //将最大路径写入文件
}

static void Main(string[] args)
{
Program test = new Program();
String readPath = "../../InFile.txt";
String writePath = "../../OutFile.txt";
test.GetPath(readPath,writePath);
}
}
}

相关文档
最新文档