Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication5.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #define size 10
- int fig[10][10] = {
- { 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
- { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
- { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0 },
- { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
- { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
- { 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
- { 0, 0, 0, 0, 0, 1, 1, 0, 0, 1 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
- };
- class node{
- public:
- int value;
- node *next = NULL;
- };
- class image{
- public:
- image(){ for (int i = 0; i < size; i++){ graph[i] = new node; } }
- node* graph[size];
- void create();
- void list();
- };
- void image::create(){
- for (int i = 0; i < size; i++){
- for (int j = 0; j < size; j++){
- node* x = new node();
- x->value = j;
- if (fig[i][j] == 1){
- node* p = graph[i];
- while (p->next != NULL){
- p = p->next;
- }
- p->next = x;
- }
- }
- }
- }
- void image::list(){
- for (int i = 0; i < size; i++){
- cout << "頂點 " << i + 1 << "=>";
- node* p = graph[i]->next;
- while (p!= NULL){
- cout << "[" << p->value+1 << "]\t";
- p = p->next;
- }
- cout << endl;
- }
- }
- void visit(int i, int*isvisit, node** head){
- node* ptr;
- isvisit[i] = 1;
- cout << "visit= " << i+1 << endl;
- ptr = head[i]->next;
- while (ptr != NULL){
- //cout << "this point:" << i+1 << endl;
- //cout << "check pint:" << ptr->value + 1 << endl;
- if (isvisit[ptr->value] == 0)
- visit(ptr->value, isvisit, head);
- ptr = ptr->next;
- }
- }
- void main()
- {
- image p1;
- int isvisit[size];
- for (int i = 0; i < size; i++)
- {
- isvisit[i] = 0;
- }
- p1.create();
- p1.list();
- visit(0, isvisit, p1.graph);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement