数据结构作业之图与邻接表

在线性表中,数据元素之间是被串起来的,仅有线性关系,每个数据元素只有一个直接前驱和一个直接后继。在树形结构中,数据元素之间有着明显的层次关系,并且每一层上的数据元素可能和下一层中多个元素相关,但只能和上一层中一个元素相关。图是一种较线性表和树更加复杂的数据结构。在图形结构中,结点之间的关系可以是任意的,图中任意两个数据元素之间都可能相关。

邻接矩阵用两个数组保存数据。一个一维数组存储图中顶点信息,一个二维数组存储图中边或弧的信息。

clip_image009

头文件及结构体

#include <stdio.h>
#include <malloc.h>

int *visitedPtr;

typedef struct Graph {
	int **connections;
	int numNodes;
} *GraphPtr;

初始化图及记录函数

GraphPtr initGraph(int paraSize, int **paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct Graph));
	resultPtr->numNodes = paraSize;
	resultPtr->connections = (int **)malloc(5 * sizeof(int *));
	for (i = 0; i < paraSize; i++) {
		resultPtr->connections[i] = (int *)malloc(5 * sizeof(int));
		for (j = 0; j < 5; j++) {
			resultPtr->connections[i][j] = paraData[i][j];
		}
	}

	return resultPtr;
}

void initTranverse(GraphPtr paraGraphPtr) {
	int i;

	visitedPtr = (int *)malloc(paraGraphPtr->numNodes * sizeof(int));

	for (i = 0; i < paraGraphPtr->numNodes; i++) {
		visitedPtr[i] = 0;
	}
}

此记录函数用于记录一个节点是否已经被遍历过

深度优先遍历和广度优先遍历

void depthFirstTranverse(GraphPtr paraGraphPtr, int paraNode) {
	int i;

	visitedPtr[paraNode] = 1;
	printf("%d\t", paraNode);

	for ( i = 0; i < paraGraphPtr->numNodes; i++) {
		if (paraGraphPtr->connections[paraNode][i] && visitedPtr[i] == 0) {
			depthFirstTranverse(paraGraphPtr, i);
		}
	}
}

void widthFirstTranverse(GraphPtr paraGraphPtr, int paraNode, int cnt) {
	int tempNode[paraGraphPtr->numNodes];
	int i, front = 0;
	visitedPtr[paraNode] = 1;
	tempNode[cnt] = paraNode;
	while ((cnt + 1) != paraGraphPtr->numNodes) {
		paraNode = tempNode[front];
		for (i = 0; i < paraGraphPtr->numNodes; i++) {
			if (visitedPtr[i])
				continue;
			if (paraGraphPtr->connections[paraNode][i] == 0)
				continue;
			visitedPtr[i] = 1;
			cnt++;
			tempNode[cnt] = i;
		}
		front++;
	}

	for (i = 0; i < paraGraphPtr->numNodes; i++) {
		printf("%d\t", tempNode[i]);
	}
}

 广度优先遍历我用来一个数组来代替了队列的功能,相比起来在此处数组相对简洁一些。

测试函数和程序入口

void testGraphTranverse() {
	int i, j;

	int myGraph[5][5] = {
		{0, 1, 0, 1, 0},
		{1, 0, 1, 0, 1},
		{0, 1, 0, 1, 1},
		{1, 0, 1, 0, 0},
		{0, 1, 1, 0, 0}
	};
	int **tempPtr;
	printf("Preparing data\r\n");

	tempPtr = (int **)malloc(5 * sizeof(int *));
	for (i = 0; i < 5; i++) {
		tempPtr[i] = (int *)malloc(5 * sizeof(int));
	}

	for (i = 0; i < 5; i++) {
		for (j = 0; j < 5; j++) {
			tempPtr[i][j] = myGraph[i][j];
		}
	}

	printf("Data ready\r\n");

	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	printf("num nodes = %d \r\n", tempGraphPtr->numNodes);
	printf("Graph initialized\r\n");

	printf("Depth first visit:\r\n");
	initTranverse(tempGraphPtr);
	depthFirstTranverse(tempGraphPtr, 4);

	printf("\r\nWidth first visit:\r\n");
	initTranverse(tempGraphPtr);
	widthFirstTranverse(tempGraphPtr, 4, 0);
}
int main() {
	testGraphTranverse();
	return 0;
}

输出结果

Preparing data
Data ready
num nodes = 5
Graph initialized
Depth first visit:
4       1       0       3       2
Width first visit:
4       1       2       0       3

邻接表

我们发现,当图中的边数相对于顶点较少时,邻接矩阵是对存储空间的极大浪费。我们可以考虑对边或弧使用链式存储的方式来避免空间浪费的问题。回忆树结构的孩子表示法,将结点存入数组,并对结点的孩子进行链式存储,不管有多少孩子,也不会存在空间浪费问题。

应用这种思路,我们把这种数组与链表相结合的存储方法称为邻接表(Adjacency List)。

邻接表的处理办法是这样。

1)  图中顶点用一个一维数组存储,当然也可以用单链表来存储,不过用数组可以较容易的读取顶点信息,更加方便。另外,对于顶点数组中,每个数据元素还需要存储指向第一个邻接点的指针,以便于查找该顶点的边信息。

2)  图中每个顶点vi的所有邻接点构成一个线性表,由于邻接点的个数不定,所以用单链表存储,无向图称为顶点vi的边表,有向图则称为以vi为弧尾的出边表。

下图是一个无向图的邻接表结构:
头文件及结构体 

#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
#define true 1
#define false 0
int* visitedPtr;

/**
 * 图的数据结构
 */
typedef struct Graph{
	int** connections;
	int numNodes;
} *GraphPtr;

 初始化图

/**
 * 初始化一个图
 */
GraphPtr initGraph(int paraSize, int** paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct Graph));
	resultPtr -> numNodes = paraSize;
	resultPtr -> connections = (int**)malloc(paraSize * sizeof(int*));
	for (i = 0; i < paraSize; i ++) {
		resultPtr -> connections[i] = (int*)malloc(paraSize * sizeof(int));
		for (j = 0; j < paraSize; j ++) {
			resultPtr -> connections[i][j] = paraData[i][j];
		}
	}
	
	return resultPtr;
}

队列的准备工作

/**
 * 队列数据结构
 */
typedef struct GraphNodeQueue{
	int* nodes;
	int front;
	int rear;
}GraphNodeQueue, *QueuePtr;

/**
 * 初始化队列
 */
QueuePtr initQueue(){
	QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
	resultQueuePtr->nodes = (int*)malloc(QUEUE_SIZE * sizeof(int));
	resultQueuePtr->front = 0;
	resultQueuePtr->rear = 1;
	return resultQueuePtr;
}

/**
 * 判断队列是否满
 */
int isQueueEmpty(QueuePtr paraQueuePtr){
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
		return true;
	}
	return false;
}

/**
 * 入队
 */
void enqueue(QueuePtr paraQueuePtr, int paraNode){
	if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
		printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
		return;
	}
	paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
	paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
}

/**
 * 出队
 */
int dequeue(QueuePtr paraQueuePtr){
	if (isQueueEmpty(paraQueuePtr)) {
		printf("Error, empty queue\r\n");
		return NULL;
	}

	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

	return paraQueuePtr->nodes[paraQueuePtr->front];
}

 邻接表的准备工作

/**
 * 邻接表节点
 */
typedef struct AdjacencyNode {
	int column;
	struct AdjacencyNode* next;
}AdjacencyNode, *AdjacentNodePtr;

/**
 * 邻接表队列
 */
typedef struct AdjacencyList {
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

/**
 * 创建一个邻接表队列
 */
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) {
	//申请空间
	int i, j, tempNum;
	AdjacentNodePtr p, q;
	tempNum = paraPtr->numNodes;
	AdjacencyListPtr resultPtr = (AdjacencyListPtr)malloc(sizeof(struct AdjacencyList));
	resultPtr->numNodes = tempNum;
	resultPtr->headers = (AdjacencyNode*)malloc(tempNum * sizeof(struct AdjacencyNode));
	
	//压入数据
	for (i = 0; i < tempNum; i ++) {
		//初始化头结点
		p = &(resultPtr->headers[i]);
		p->column = -1;
		p->next = NULL;

		for (j = 0; j < tempNum; j ++) {
			if (paraPtr->connections[i][j] > 0) {
				//创建一个新节点
				q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
				q->column = j;
				q->next = NULL;

				//链接
				p->next = q;
				p = q;
			}
		}
	}

	return resultPtr;
}

/**
 * 打印邻接表
 */
void printAdjacentList(AdjacencyListPtr paraPtr) {
	int i;
	AdjacentNodePtr p;
	int tempNum = paraPtr->numNodes;

	printf("This is the graph:\r\n");
	for (i = 0; i < tempNum; i ++) {
		p = paraPtr->headers[i].next;
		while (p != NULL) {
			printf("The node %d connects %d, ", i, p->column);
			p = p->next;
		}
		printf("\r\n");
	}
}

利用队列的广度优先搜索

/**
 * 广度优先搜索
 */
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart){
	printf("width first \r\n");
	//使用队列来存储数据
	int i, j, tempNode;
	AdjacentNodePtr p;
	i = 0;

	//初始化数据
	visitedPtr = (int*) malloc(paraListPtr->numNodes * sizeof(int));
	
	for (i = 0; i < paraListPtr->numNodes; i ++) {
		visitedPtr[i] = 0;
	}

	QueuePtr tempQueuePtr = initQueue();
	printf("%d\t", paraStart);
	visitedPtr[paraStart] = 1;
	enqueue(tempQueuePtr, paraStart);
	while (!isQueueEmpty(tempQueuePtr)) {
		tempNode = dequeue(tempQueuePtr);

		for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) {
			j = p->column;
			if (visitedPtr[j]) 
				continue;

			printf("%d\t", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}
	}
	printf("\r\n");
}

测试函数和程序入口

/**
 * 测试图的遍历
 */
void testGraphTranverse() {
	int i, j;
	int myGraph[5][5] = { 
		{0, 1, 0, 1, 0},
		{1, 0, 1, 0, 1}, 
		{0, 1, 0, 1, 1}, 
		{1, 0, 1, 0, 0}, 
		{0, 1, 1, 0, 0}};
	int** tempPtr;
	printf("Preparing data\r\n");
		
	tempPtr = (int**)malloc(5 * sizeof(int*));
	for (i = 0; i < 5; i ++) {
		tempPtr[i] = (int*)malloc(5 * sizeof(int));
	}
	 
	for (i = 0; i < 5; i ++) {
		for (j = 0; j < 5; j ++) {
			tempPtr[i][j] = myGraph[i][j];
		}
	}
 
	printf("Data ready\r\n");
	
	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);

	printAdjacentList(tempListPtr);

	widthFirstTranverse(tempListPtr, 4);
}

/**
 * 程序入口
 */
int main(){
	testGraphTranverse();
	return 0;
}

输出结果

Preparing data
Data ready
This is the graph:
The node 0 connects 1, The node 0 connects 3,
The node 1 connects 0, The node 1 connects 2, The node 1 connects 4,
The node 2 connects 1, The node 2 connects 3, The node 2 connects 4,
The node 3 connects 0, The node 3 connects 2,
The node 4 connects 1, The node 4 connects 2,
width first
4       1       2       0       3

完整代码

#include <stdio.h>
#include <malloc.h>

int *visitedPtr;

typedef struct Graph {
	int **connections;
	int numNodes;
} *GraphPtr;

GraphPtr initGraph(int paraSize, int **paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct Graph));
	resultPtr->numNodes = paraSize;
	resultPtr->connections = (int **)malloc(5 * sizeof(int *));
	for (i = 0; i < paraSize; i++) {
		resultPtr->connections[i] = (int *)malloc(5 * sizeof(int));
		for (j = 0; j < 5; j++) {
			resultPtr->connections[i][j] = paraData[i][j];
		}
	}

	return resultPtr;
}

void initTranverse(GraphPtr paraGraphPtr) {
	int i;

	visitedPtr = (int *)malloc(paraGraphPtr->numNodes * sizeof(int));

	for (i = 0; i < paraGraphPtr->numNodes; i++) {
		visitedPtr[i] = 0;
	}
}

void depthFirstTranverse(GraphPtr paraGraphPtr, int paraNode) {
	int i;

	visitedPtr[paraNode] = 1;
	printf("%d\t", paraNode);

	for ( i = 0; i < paraGraphPtr->numNodes; i++) {
		if (paraGraphPtr->connections[paraNode][i] && visitedPtr[i] == 0) {
			depthFirstTranverse(paraGraphPtr, i);
		}
	}
}

void widthFirstTranverse(GraphPtr paraGraphPtr, int paraNode, int cnt) {
	int tempNode[paraGraphPtr->numNodes];
	int i, front = 0;
	visitedPtr[paraNode] = 1;
	tempNode[cnt] = paraNode;
	while ((cnt + 1) != paraGraphPtr->numNodes) {
		paraNode = tempNode[front];
		for (i = 0; i < paraGraphPtr->numNodes; i++) {
			if (visitedPtr[i])
				continue;
			if (paraGraphPtr->connections[paraNode][i] == 0)
				continue;
			visitedPtr[i] = 1;
			cnt++;
			tempNode[cnt] = i;
		}
		front++;
	}

	for (i = 0; i < paraGraphPtr->numNodes; i++) {
		printf("%d\t", tempNode[i]);
	}
}

void testGraphTranverse() {
	int i, j;

	int myGraph[5][5] = {
		{0, 1, 0, 1, 0},
		{1, 0, 1, 0, 1},
		{0, 1, 0, 1, 1},
		{1, 0, 1, 0, 0},
		{0, 1, 1, 0, 0}
	};
	int **tempPtr;
	printf("Preparing data\r\n");

	tempPtr = (int **)malloc(5 * sizeof(int *));
	for (i = 0; i < 5; i++) {
		tempPtr[i] = (int *)malloc(5 * sizeof(int));
	}

	for (i = 0; i < 5; i++) {
		for (j = 0; j < 5; j++) {
			tempPtr[i][j] = myGraph[i][j];
		}
	}

	printf("Data ready\r\n");

	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	printf("num nodes = %d \r\n", tempGraphPtr->numNodes);
	printf("Graph initialized\r\n");

	printf("Depth first visit:\r\n");
	initTranverse(tempGraphPtr);
	depthFirstTranverse(tempGraphPtr, 4);

	printf("\r\nWidth first visit:\r\n");
	initTranverse(tempGraphPtr);
	widthFirstTranverse(tempGraphPtr, 4, 0);
}
int main() {
	testGraphTranverse();
	return 0;
}
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
#define true 1
#define false 0
int* visitedPtr;

/**
 * 图的数据结构
 */
typedef struct Graph{
	int** connections;
	int numNodes;
} *GraphPtr;

/**
 * 初始化一个图
 */
GraphPtr initGraph(int paraSize, int** paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct Graph));
	resultPtr -> numNodes = paraSize;
	resultPtr -> connections = (int**)malloc(paraSize * sizeof(int*));
	for (i = 0; i < paraSize; i ++) {
		resultPtr -> connections[i] = (int*)malloc(paraSize * sizeof(int));
		for (j = 0; j < paraSize; j ++) {
			resultPtr -> connections[i][j] = paraData[i][j];
		}
	}
	
	return resultPtr;
}

/**
 * 队列数据结构
 */
typedef struct GraphNodeQueue{
	int* nodes;
	int front;
	int rear;
}GraphNodeQueue, *QueuePtr;

/**
 * 初始化队列
 */
QueuePtr initQueue(){
	QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
	resultQueuePtr->nodes = (int*)malloc(QUEUE_SIZE * sizeof(int));
	resultQueuePtr->front = 0;
	resultQueuePtr->rear = 1;
	return resultQueuePtr;
}

/**
 * 判断队列是否满
 */
int isQueueEmpty(QueuePtr paraQueuePtr){
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
		return true;
	}
	return false;
}

/**
 * 入队
 */
void enqueue(QueuePtr paraQueuePtr, int paraNode){
	if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
		printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
		return;
	}
	paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
	paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
}

/**
 * 出队
 */
int dequeue(QueuePtr paraQueuePtr){
	if (isQueueEmpty(paraQueuePtr)) {
		printf("Error, empty queue\r\n");
		return NULL;
	}

	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

	return paraQueuePtr->nodes[paraQueuePtr->front];
}

/**
 * 邻接表节点
 */
typedef struct AdjacencyNode {
	int column;
	struct AdjacencyNode* next;
}AdjacencyNode, *AdjacentNodePtr;

/**
 * 邻接表队列
 */
typedef struct AdjacencyList {
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

/**
 * 创建一个邻接表队列
 */
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) {
	//申请空间
	int i, j, tempNum;
	AdjacentNodePtr p, q;
	tempNum = paraPtr->numNodes;
	AdjacencyListPtr resultPtr = (AdjacencyListPtr)malloc(sizeof(struct AdjacencyList));
	resultPtr->numNodes = tempNum;
	resultPtr->headers = (AdjacencyNode*)malloc(tempNum * sizeof(struct AdjacencyNode));
	
	//压入数据
	for (i = 0; i < tempNum; i ++) {
		//初始化头结点
		p = &(resultPtr->headers[i]);
		p->column = -1;
		p->next = NULL;

		for (j = 0; j < tempNum; j ++) {
			if (paraPtr->connections[i][j] > 0) {
				//创建一个新节点
				q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
				q->column = j;
				q->next = NULL;

				//链接
				p->next = q;
				p = q;
			}
		}
	}

	return resultPtr;
}

/**
 * 打印邻接表
 */
void printAdjacentList(AdjacencyListPtr paraPtr) {
	int i;
	AdjacentNodePtr p;
	int tempNum = paraPtr->numNodes;

	printf("This is the graph:\r\n");
	for (i = 0; i < tempNum; i ++) {
		p = paraPtr->headers[i].next;
		while (p != NULL) {
			printf("The node %d connects %d, ", i, p->column);
			p = p->next;
		}
		printf("\r\n");
	}
}

/**
 * 广度优先搜索
 */
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart){
	printf("width first \r\n");
	//使用队列来存储数据
	int i, j, tempNode;
	AdjacentNodePtr p;
	i = 0;

	//初始化数据
	visitedPtr = (int*) malloc(paraListPtr->numNodes * sizeof(int));
	
	for (i = 0; i < paraListPtr->numNodes; i ++) {
		visitedPtr[i] = 0;
	}

	QueuePtr tempQueuePtr = initQueue();
	printf("%d\t", paraStart);
	visitedPtr[paraStart] = 1;
	enqueue(tempQueuePtr, paraStart);
	while (!isQueueEmpty(tempQueuePtr)) {
		tempNode = dequeue(tempQueuePtr);

		for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) {
			j = p->column;
			if (visitedPtr[j]) 
				continue;

			printf("%d\t", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}
	}
	printf("\r\n");
}

/**
 * 测试图的遍历
 */
void testGraphTranverse() {
	int i, j;
	int myGraph[5][5] = { 
		{0, 1, 0, 1, 0},
		{1, 0, 1, 0, 1}, 
		{0, 1, 0, 1, 1}, 
		{1, 0, 1, 0, 0}, 
		{0, 1, 1, 0, 0}};
	int** tempPtr;
	printf("Preparing data\r\n");
		
	tempPtr = (int**)malloc(5 * sizeof(int*));
	for (i = 0; i < 5; i ++) {
		tempPtr[i] = (int*)malloc(5 * sizeof(int));
	}
	 
	for (i = 0; i < 5; i ++) {
		for (j = 0; j < 5; j ++) {
			tempPtr[i][j] = myGraph[i][j];
		}
	}
 
	printf("Data ready\r\n");
	
	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);

	printAdjacentList(tempListPtr);

	widthFirstTranverse(tempListPtr, 4);
}

/**
 * 程序入口
 */
int main(){
	testGraphTranverse();
	return 0;
}
这是我的作业。。。。希望对各位有#include <stdio.h> #include <malloc.h> #include <stdlib.h> #define SIZE (xsize*ysize+1) //一系列全局变量便于传递参数 int *location,*way, xsize,ysize,firstx,firsty, noworder; int getnum (void);//取数函数,取数成功返回1,否则返回-1 int init (void); //初始化函数,申请数组空间,以及初始化数组, //申请成功返回1,否则返回-1 int play (void); //下棋函数,下棋成功返回1,否则返回-1 int back (void); //悔棋函数,悔棋成功返回1,否则返回-1 void print (void);//输出函数,顺序输出马踩的棋盘一维坐标 //////////////////////////// void main () { int canget,caninit,canplay,canback; do canget=getnum(); while(canget==-1); caninit=init(); if(caninit==-1) exit (0);//终止程序运行 for (;noworder<SIZE-1;) { if(way[location[noworder]]>0 && way[location[noworder]]<=8) { canplay=play(); if(canplay==-1) way[location[noworder]]++;//当前方法不可行,改变方法 } else { canback=back(); if(canback==-1) { printf("不可能遍历整个棋盘!\n"); getchar();getchar(); exit (0);//当程序不能再悔棋时终止程序运行 } else way[location[noworder]]++; //当前方法不可行,改变方法 } } if(noworder==SIZE-1)//已经遍历整个棋盘 print(); getchar();getchar(); } //////////////////////////// int getnum() { printf("输入棋盘规格(假定无0点)和入口坐标:\n"); printf("输入棋盘规格xsize="); scanf("%d",&xsize); printf("输入棋盘规格ysize="); scanf("%d",&ysize); printf("输入入口坐标x="); scanf("%d",&firstx); printf("输入入口坐标y="); scanf("%d",&firsty); if (firstx>xsize || firsty>ysize || firstx<=0 || firsty<=0 || xsize <3 || ysize<3) { printf("输入有误,重新输入:\n\n\a"); return -1; } else return 1; } //////////////////////////// int init (void) { location=(int *)malloc(sizeof(int)*SIZE); way=(int *)malloc(sizeof(int)*SIZE); if(location==NULL || way==NULL) { printf("系统申请内存空间失败!程序执行终止!\a"); return -1; } for(int i=0;i<SIZE;i++)//初始化数组 { way[i]=0; location[i]=0; } noworder=1; location[1]=(firsty-1)*xsize+firstx; way[location[1]]=1; return 1; } //////////////////////////// void print(void) { printf("\n\n可以遍历,顺序如下:\n\n"); for (int i=1;i<SIZE;i++) { printf("%3d-->",location[i]); printf("OK\n"); } } //////////////////////////// int play() { int x,y,nextlocation; //一维坐标值à二维坐标值 x=location[noworder] % xsize; if(x==0) x=xsize; y=(location[noworder]-x)/xsize+1; switch (way[location[noworder]]) { case 1 : x+=2;y-=1;break; case 2 : x+=2;y+=1;break; case 3 : x+=1;y+=2;break; case 4 : x-=1;y+=2;break; case 5 : x-=2;y+=1;break; case 6 : x-=2;y-=1;break; case 7 : x-=1;y-=2;break; case 8 : x+=1;y-=2;break; } nextlocation = xsize*(y-1)+x; if (x>xsize || y>ysize || x<=0 || y<=0 || way[nextlocation]!=0)//越界或重复 return -1; else//下棋 { noworder++; location[noworder] = nextlocation; way[location[noworder]]=1; return 1; } } //////////////////////////// int back (void) { if(noworder==1)//不能再悔棋,不能遍历 return -1; else { way[location[noworder]]=0;//注意不能搞错语句顺序 location[noworder]=0; noworder--; return 1; } }用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值