我的博客

来源:互联网 发布:西北师大知行学院吧 编辑:程序博客网 时间:2024/05/17 23:32

#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <stdio.h>
#include <string.h>

using namespace std;

#define SPACE    '~'
#define KING    'K'
#define Queen    'Q'
#define ROOK    'R'
#define KNIGHT    'G'
#define BISHOP    'B'
#define PAWN    'P'
#define MAXNODENUM   100


/*the black of the node*/
enum
{
 WHITE = 0,
 GRAY,
 BLACK
};

//cell infomation

typedef struct stru_Cell
{
 int x;
 int y;
 char symbol;
}Cell;

typedef struct stru_Vnode
{
 Cell vertexInfo;

 //the list of the adjvex
 list<int> neighbours;
}Vnode;


/*the graph by list*/
typedef struct stru_Graph
{
 Vnode graphArray[MAXNODENUM];
 int verNum;
 int visitedColor[MAXNODENUM];
 
}Graph;

char srcLoadArray[10][10];
char dstLoadArray[10][10];
int boundSize;

void BFS(Graph& graph, int verIndex, int dstverIndex)
{
 int father[MAXNODENUM];
 int dist[MAXNODENUM];
 queue<int> que;
 
 for (int i=0; i<graph.verNum; ++i)
 {
  graph.visitedColor[i] = WHITE;
  dist[i] = -1;
  father[i] = -1;
 }

 graph.visitedColor[verIndex] = GRAY;
 dist[verIndex] = 0;
 father[verIndex] = -1;

 que.push(verIndex);

 while (!que.empty())
 {
  int k = que.front();
  que.pop();

  list<int>::iterator it;
  for (it = graph.graphArray[k].neighbours.begin();
    it != graph.graphArray[k].neighbours.end();
    ++it)
  {
   if (WHITE == graph.visitedColor[*it]
   && graph.graphArray[*it].vertexInfo.symbol == SPACE)
   {
    graph.visitedColor[*it] = GRAY;
    dist[*it] = dist[k] + 1;
    father[*it] = k;
    que.push(*it);
    if (*it == dstverIndex)
    {
     printf("find the path\n");
     stack<int> path;

     int index = *it;
     while (index != -1)
     {
      path.push(index);
      index = father[index];
     }

     while (!path.empty())
     {
      int m = path.top();
      printf("path= %d\n", m);
      path.pop();
     }
    }
    
   }
  }
  graph.visitedColor[k] = BLACK;
 }

 for (int i=0; i<graph.verNum; ++i)
 {
  printf("dist = %d\n", dist[i]);
 }

}

int load_src_file(char* filePath)
{
 FILE* fp = fopen(filePath, "r");
 if (NULL == fp)
 {
  printf("load failed\n");
  return -1;
 }
 fscanf(fp, "%d", &boundSize);

 char buf[100] = {0};
 int i = 0;
 fgets(buf, sizeof(buf), fp);
 while (fgets(buf, sizeof(buf), fp) != 0)
 {
  for (int j=0; j<boundSize; ++j)
  {
   srcLoadArray[i][j] = buf[j];
  }
  i++;
 }

 for (int i=0; i<boundSize; ++i)
 {
  for (int j=0; j<boundSize; ++j)
  {
   printf("%c", srcLoadArray[i][j]);
  }
  printf("\n");
 }

 fclose(fp);
 return 0;
}

int init_graph(Graph& srcGraph)
{
 srcGraph.verNum = boundSize*boundSize;
 for (int i=0; i<boundSize; ++i)
 {
  for (int j=0; j<boundSize; ++j)
  {
   Vnode node;
   node.vertexInfo.symbol = srcLoadArray[i][j];
   node.vertexInfo.x = j;
   node.vertexInfo.y = i;

   for (int n=j-1; n<=j+1; ++n)
   {
    if (n == j)
    {
     continue;
    }
    if (n < 0 || n >= boundSize)
    {
     continue;
    }

    node.neighbours.push_back(i*boundSize + n);
   }

   for (int m=i-1; m<=i+1; ++m)
   {
    if (m == i)
    {
     continue;
    }
    if (m < 0 || m >= boundSize)
    {
     continue;
    }
    node.neighbours.push_back(m*boundSize + j);
   }
   

   srcGraph.graphArray[i*boundSize+j] = node;
  }

 }
 return 0;
}

int main(int argc,char** argv)
{
 load_src_file("./src.txt");
 
 Graph src;
 init_graph(src);

 BFS(src, 6, 5);
 
 return 0;
}

 

原创粉丝点击