导航:网站首页 >
求大神帮写一个c语言图的深度优先遍历,和广度优先... 求个大神帮写一个C语言RGB565转换为灰度图像的算法。
求大神帮写一个c语言图的深度优先遍历,和广度优先... 求个大神帮写一个C语言RGB565转换为灰度图像的算法。
相关问题:
匿名网友:
/*深度优先*/
#include
#include
struct node/*图的顶点结构*/
{
int vertex;
int flag;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
void creat_graph(int *node,int n)
{
graph newnode,p;/*定义一个新结点及指针*/
int start,end,i;
for(i=0;i
{
start=node[i*2];/*边的起点*/
end=node[i*2+1];/*边的终点*/
newnode=(graph)malloc(sizeof(struct node));
newnode->vertex=end;/*新结点的内容为边终点处顶点的内容*/
newnode->nextnode=NULL;
p=&(vertex_node[start]);/*设置指针位置*/
while(p->nextnode!=NULL)
p=p->nextnode;/*寻找链尾*/
p->nextnode=newnode;/*在链尾处插入新结点*/
}
}
void dfs(int k)
{
graph p;
vertex_node[k].flag=1;/*将标志位置1,证明该结点已访问过*/
printf("vertex[%d]",k);
p=vertex_node[k].nextnode;/*指针指向下个结点*/
while(p!=NULL)
{
if(vertex_node[p->vertex].flag==0)/*判断该结点的标志位是否为0*/
dfs(p->vertex);/*继续遍历下个结点*/
p=p->nextnode;/*若已遍历过p指向下一个结点*/
}
}
main()
{
graph p;
int node[100],i,sn,vn;
printf("please input the number of sides:\n");
scanf("%d",&sn);/*输入无向图的边数*/
printf("please input the number of vertexes\n");
scanf("%d",&vn);
printf("please input the vertexes which connected by the sides:\n");
for(i=0;i<4*sn;i++)
scanf("%d",&node[i]);/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for(i=1;i<=vn;i++)
{
vertex_node[i].vertex=i;/*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode=NULL;
}
creat_graph(node,2*sn);/*调用函数创建邻接表*/
printf("the result is:\n");
for(i=1;i<=vn;i++)/*将邻接表内容输出*/
{
printf("vertex%d:",vertex_node[i].vertex);/*输出顶点内容*/
p=vertex_node[i].nextnode;
while(p!=NULL)
{
printf("->%3d",p->vertex);/*输出邻接顶点的内容*/
p=p->nextnode;/*指针指向下个邻接顶点*/
}
printf("\n");
}
printf("the result of depth-first search is:\n");
dfs(1);/*调用函数进行深度优先遍历*/
printf("\n");
}
/***************************广度优先*******************************/
#include
#include
struct node
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph;
struct node vertex_node[10];
#define MAXQUEUE 100
int queue[MAXQUEUE];
int front = - 1;
int rear = - 1;
int visited[10];
void creat_graph(int *node, int n)
{
graph newnode, p; /*定义一个新结点及指针*/
int start, end, i;
for (i = 0; i < n; i++)
{
start = node[i *2]; /*边的起点*/
end = node[i *2+1]; /*边的终点*/
newnode = (graph)malloc(sizeof(struct node));
newnode->vertex = end; /*新结点的内容为边终点处顶点的内容*/
newnode->nextnode = NULL;
p = &(vertex_node[start]); /*设置指针位置*/
while (p->nextnode != NULL)
p = p->nextnode;
/*寻找链尾*/
p->nextnode = newnode; /*在链尾处插入新结点*/
}
}
int enqueue(int value) /*元素入队列*/
{
if (rear >= MAXQUEUE)
return - 1;
rear++; /*移动队尾指针*/
queue[rear] = value;
}
int dequeue() /*元素出队列*/
{
if (front == rear)
return - 1;
front++; /*移动队头指针*/
return queue[front];
}
void bfs(int k) /*广度优先搜索*/
{
graph p;
enqueue(k); /*元素入队*/
visited[k] = 1;
printf("vertex[%d]", k);
while (front != rear)
/*判断是否对空*/
{
k = dequeue(); /*元素出队*/
p = vertex_node[k].nextnode;
while (p != NULL)
{
if (visited[p->vertex] == 0)
/*判断其是否被访问过*/
{
enqueue(p->vertex);
visited[p->vertex] = 1; /*访问过的元素置1*/
printf("vertex[%d]", p->vertex);
}
p = p->nextnode; /*访问下一个元素*/
}
}
}
main()
{
graph p;
int node[100], i, sn, vn;
printf("please input the number of sides:\n");
scanf("%d", &sn); /*输入无向图的边数*/
printf("please input the number of vertexes\n");
scanf("%d", &vn);
printf("please input the vertexes which connected by the sides:\n");
for (i = 0; i < 4 *sn; i++)
scanf("%d", &node[i]);
/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for (i = 1; i <= vn; i++)
{
vertex_node[i].vertex = i; /*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode = NULL;
}
creat_graph(node, 2 *sn); /*调用函数创建邻接表*/
printf("the result is:\n");
for (i = 1; i <= vn; i++)
/*将邻接表内容输出*/
{
printf("vertex%d:", vertex_node[i].vertex); /*输出顶点内容*/
p = vertex_node[i].nextnode;
while (p != NULL)
{
printf("->%3d", p->vertex); /*输出邻接顶点的内容*/
p = p->nextnode; /*指针指向下个邻接顶点*/
}
printf("\n");
}
printf("the result of breadth-first search is:\n");
bfs(1); /*调用函数进行深度优先遍历*/
printf("\n");
}
匿名网友:
1.求大神帮写一个c语言图的深度优先遍历,和广度优先...
问:格式如图
2.求个大神帮写一个C语言RGB565转换为灰度图像的算法。
问:跪谢! 本人在NIOSii环境下运行,不要openvc的那个!
3.求大神用C语言帮我写一个搞笑又幽默的程序,一定要...
答:这是我写过的一些有趣的程序,用的DevCpp。因为源码不是很规范,所以只打包了exe,想要源码的话再说。 http://pan.baidu.com/s/1sjBeOFj
4.求大神帮忙用C语言写一个程序。 要求:定义两个线...
问:求大神帮忙用C语言写一个程序。 要求:定义两个线性表(内容由用户输入...
5.求大神帮忙写一个矢量加法的c语言程序。
答:一个向量用(x1,y1)表示,另外一个用(x2,y2)表示,和向量是(x1+y1,x2+y2) 如果你说已知两个向量的模和夹角,可以用余弦定理算和向量的长度,方向也可以通过正弦定理,余弦定理算,编程就不说了,太菜了。。。
6.C语言作业真不会呀,求大神帮忙写一个小程序
问:要求编写一个程序,在显示屏上显示如下内容(全为半角字符,且不包含空...
7.求大神帮忙写一个c语言万年历代码,(要求输入一个...
问:求大神帮忙写一个c语言万年历代码,(要求输入一个年月,打印出对应的日...
8.(C语言)一个旅游管理系统,有八个景点,具体要求...
问:可统计到各景点的人数 门票分为淡季和旺季 票种分为军人票(免票),学...
9.求大神帮写一液晶+STC89c52单片机+4*4矩阵键盘实现...
问:如题,求大神帮写程序,键盘包括0~9十个数字按键以及加减乘除等号五个运...
10.求大神帮忙C语言抓小偷的问题
问:看到一个题目,求帮忙教我如何做,题目如下 4个嫌疑人A,B,C,D其中一个是...
问题推荐