有一个数字序列,是个二维数组,如:
1 2 3 4 5 6 7 8
3 4 2 1 9 8 1 1
3 8 0 1 8 3 9 1
8 9 2 2 8 3 1 3
3 8 9 7 1 3 3 0
2 9 7 9 7 9 1 3
3 4 5 2 1 4 4 2
......要求输入一个数字串,如1234,能在上面的数字序列中找出该特征的数字串,要求无论是横的、竖的,拐弯的、十字型的,还有乱七八糟形状的都能找出来,只要连在一起就行,各位有什么好的办法?

解决方案 »

  1.   

    //就是走迷宫的算法~~
    //把每一个位置做为起点测试~~
    //参考如下代码~~
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        Button1: TButton;
        ListBox1: TListBox;
        Edit1: TEdit;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure ListBox1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}const
      cRowCount = 20; //高度
      cColCount = 20; //宽度const
      cMoveOffsets: array[0..3] of TPoint = ( //偏移参数
    (X: 00; Y: -1), //上
    (X: +1; Y: 00), //右
    (X: 00; Y: +1), //下
    (X: -1; Y: 00) //左
    );var
      vPointList: array[0..cColCount - 1, 0..cRowCount - 1] of Char; //矩阵
      vPassList: array[0..cColCount - 1, 0..cRowCount - 1] of Boolean; //是否处理过procedure pInit; //初始化
    var
      I, J: Integer;
    begin
      Randomize;
      for I := 0 to cColCount - 1 do
        for J := 0 to cRowCount - 1 do
          vPointList[I, J] := Char(Byte('0') + Random(10));
      FillChar(vPassList, SizeOf(vPassList), 0);
    end;procedure pCalc( //计算所有路径
      mStr: string; //源字符串
      mOutput: TStrings //输出
    );
    var
      I, J: Integer;
      vLength: Integer;  procedure fCalc( //计算一步
        mIndex: Integer; //序号
        mCol, mRow: Integer; //起始坐标
        mStep: string //步骤
      );
      var
        I: Integer;
      begin
        if (mCol < 0) or (mRow < 0) or
          (mCol >= cColCount) or (mRow >= cRowCount) then Exit; //非法坐标
        if vPassList[mCol, mRow] then Exit; //已经路过
        if mIndex > vLength then Exit; //下标越界
        if mStr[mIndex] <> vPointList[mCol, mRow] then Exit; //不是相应的字符
        vPassList[mCol, mRow] := True;
        mStep := Format('%s;%d-%d', [mStep, mCol, mRow]);
        if mIndex < vLength then
          for I := Low(cMoveOffsets) to High(cMoveOffsets) do
            fCalc(
              mIndex + 1,
              mCol + cMoveOffsets[I].X,
              mRow + cMoveOffsets[I].Y,
              mStep
            )
        else mOutput.Add(Copy(mStep, 2, MaxInt));
        vPassList[mCol, mRow] := False;
      end;begin
      if mStr = '' then Exit;
      if not Assigned(mOutput) then Exit;
      vLength := Length(mStr);
      mOutput.BeginUpdate;
      try
        mOutput.Clear;
        for I := 0 to cColCount - 1 do
          for J := 0 to cRowCount - 1 do
            fCalc(1, I, J, '');
      finally
        mOutput.EndUpdate
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      StringGrid1.RowCount := cRowCount;
      StringGrid1.ColCount := cColCount;
      StringGrid1.FixedCols := 0;
      StringGrid1.FixedRows := 0;
      StringGrid1.DefaultColWidth := 12;
      StringGrid1.DefaultRowHeight := 12;
      Edit1.Clear;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      I, J: Integer;
    begin
      pInit;  for I := 0 to cColCount - 1 do
        for J := 0 to cRowCount - 1 do
          StringGrid1.Cells[I, J] := vPointList[I, J];
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      pCalc(Edit1.Text, ListBox1.Items);  ListBox1.ItemIndex := 0;
      StringGrid1.Repaint;
    end;type
      TStringGridEx = class(TStringGrid);procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      I: Integer;
    begin
      if ListBox1.ItemIndex < 0 then Exit;
      I := Pos(Format(';%d-%d;', [ACol, ARow]),
        Format(';%s;', [ListBox1.Items[ListBox1.ItemIndex]]));
      case I of
        0: Exit;
        1: TStringGridEx(Sender).Canvas.Brush.Color := clRed;
      else TStringGridEx(Sender).Canvas.Brush.Color := clGreen;
      end;
      TStringGridEx(Sender).OnDrawCell := nil;
      try
        TStringGridEx(Sender).DrawCell(ACol, ARow, Rect, State);
      finally
        TStringGridEx(Sender).OnDrawCell := StringGrid1DrawCell;
      end;
    end;procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      StringGrid1.Repaint;
    end;end.