离散实验4
1 #include<iostream> 2 #include <iomanip> 3 #include<string> 4 using namespace std; 5 6 int main() 7 { 8 int i, j; 9 string C, R; 10 cout << "输入集合:" << endl; 11 cin >> C; 12 cout << "关系:" << endl; 13 cin >> R; 14 15 string setA; 16 int D = 0; 17 for (i = 0; i < C.length();i++) 18 if (C[i]>'a' && C[i] < 'z') 19 { 20 setA += C[i]; 21 D++; 22 } 23 string *CA = new string[D * 2]; 24 for (i = 0; i < D; i++) 25 CA[i] = setA[i]; 26 string RA; 27 for (i = 0; i < R.length(); i++) 28 if (R[i]>'a' && R[i] < 'z') 29 RA += R[i]; 30 int **M = new int*[D]; 31 for (i = 0; i < D; i++) 32 M[i] = new int[D]; 33 34 for (i = 0; i < D; i++) 35 for (j = 0; j < D; j++) 36 M[i][j] = 0; 37 for (i = 0; i < RA.length(); i += 2) 38 M[RA[i] - 'a'][RA[i + 1] - 'a'] = 1; 39 cout << "矩阵:" << endl; 40 41 for (i = 0; i < D; i++) 42 for (j = 0; j < D; j++) 43 { 44 cout << M[i][j] << " "; 45 if (j == D - 1) 46 cout <<endl; 47 } 48 cout << endl; 49 50 cout << "简化矩阵" << endl; 51 52 for (i = 1; i < D; i++) 53 for (j = 0; j < i; j++) 54 { 55 cout << M[i][j] << " "; 56 if (j == i - 1) 57 cout << endl; 58 } 59 cout << endl; 60 61 if (D == 1) 62 { 63 cout << "该关系极大相容类:" << endl; 64 return 0; 65 } 66 67 int DA =D,DB = D; 68 string strA, strB, strC; 69 if (D > 1) 70 { 71 for (i = DB - 2; i >= 0; i--) 72 { 73 for (j = i + 1; j < DB; j++) 74 if (M[j][i] == 1) 75 strA += char('a' + j); 76 for (j = 0; j < D; j++) 77 { 78 for (int k = 0; k < CA[j].length(); k++) 79 for (int m = 0; m < strA.length();m++) 80 if (CA[j][k] == strA[m]) 81 strB += strA[m]; 82 83 if (strB.length() != 0) 84 { 85 strC = strB; 86 strB = char('a' + 1); 87 strB += strC; 88 CA[DA] = strB; 89 DA++; 90 strC = " "; 91 strB = " "; 92 } 93 } 94 strA = " "; 95 D = DA; 96 int flag = 0; 97 for (j = 0; j < D; j++) 98 for (int k = j + 1; k < D; k++) 99 { 100 for (int l = 0; l < CA[j].length(); l++) 101 for (int m = 0; m < CA[k].length(); m++) 102 if (CA[j][l] == CA[k][m]) 103 flag++; 104 if (flag != 0 && flag == CA[j].length()) 105 { 106 CA[j] = " "; 107 DA--; 108 } 109 if (flag != 0 && flag == CA[k].length()) 110 { 111 CA[k] = " "; 112 DA--; 113 } 114 flag = 0; 115 } 116 for (j = 0; j < D; j++) 117 for (int k = 0; k < D - 1; k++) 118 if (CA[k] == " " && CA[k + 1] != " ") 119 { 120 CA[k] = CA[k + 1]; 121 CA[k + 1] = " "; 122 } 123 for (j = 0; j < D; j++) 124 cout << setw(5) << CA[j]; 125 cout << endl; 126 } 127 } 128 cout << endl; 129 cout << "极大相容类:" << endl; 130 for (i = 0; i < D; i++) 131 { 132 cout << "{"; 133 for (j = 0; j < CA[i].length(); j++) 134 { 135 cout << CA[i][j]; 136 if (j != CA[i].length() - 1) 137 cout << ","; 138 } 139 cout << "}"; 140 if (i != D - 1) 141 cout << ","; 142 } 143 cout << endl; 144 return 0; 145 146 }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAXedg 100 4 #define MAX 0 5 #define N 4 //着色的颜色数 6 int color[30]={0};//来存储对应块的对应颜色 7 typedef char vextype; 8 typedef int adjtype; 9 typedef struct //定义图 10 { 11 vextype vexs[MAXedg]; //存放边的矩阵 12 adjtype arcs[MAXedg][MAXedg]; //图的邻接矩阵 13 int vnum,arcnum; //图的顶点数和边数 14 }Graph; 15 //*********************************************************** 16 int LocateVex(Graph G,char u) 17 { 18 int i; 19 for(i=1;i<=G.vnum;i++) 20 { 21 if(u==G.vexs[i]) 22 return i; 23 } 24 if(i==G.vnum) 25 { 26 printf("Error u!\n"); 27 exit(1); 28 } 29 return 0; 30 } 31 //********************************************************** 32 void CreateGraph(Graph &G) //输入图 33 { 34 int i,j,k, w; 35 vextype v1,v2; 36 printf("输入图的顶点数和边数:\n"); 37 scanf("%d%d",&G.vnum,&G.arcnum); 38 getchar(); 39 printf("输入图的各顶点:\n"); 40 for(i=1;i<=G.vnum;i++) 41 { 42 scanf("%c",&G.vexs[i]); 43 getchar(); 44 } 45 for(i=0;i<=G.vnum;i++) 46 for(j=0;j<=G.vnum;j++) 47 G.arcs[i][j]=MAX; 48 printf("输入边的两个顶点和权值(均用1表示):\n"); 49 for(k=0;k<G.arcnum;k++) 50 { 51 scanf("%c", &v1);getchar(); 52 scanf("%c", &v2);getchar(); 53 scanf("%d", &w); getchar(); 54 i=LocateVex(G,v1); 55 j=LocateVex(G,v2); 56 G.arcs[i][j]=w; 57 G.arcs[j][i]=w; 58 } 59 } 60 //**************************************************************** 61 void PrintGraph(Graph G) //输出图的信息 62 { 63 int i,j; 64 printf("图的各顶点:\n"); 65 for(i=1;i<=G.vnum;i++) 66 printf("%c ",G.vexs[i]); 67 printf("\n"); 68 printf("图的邻接矩阵:\n"); 69 for(i=1;i<=G.vnum;i++) 70 { 71 for(j=1;j<=G.vnum;j++) 72 printf("%d ",G.arcs[i][j]); 73 printf("\n"); 74 } 75 } 76 //****************************************************************** 77 int colorsame(int s,Graph G)//判断这个颜色能不能满足要求 78 { 79 int i,flag=0; 80 for(i=1;i<=s-1;i++)//分别与前面已经着色的几块比较 81 if(G.arcs[i][s]==1&&color[i]==color[s]) 82 { 83 flag=1;break; 84 } 85 return flag; 86 } 87 //****************************************************************** 88 void output(Graph G)//输出函数 89 { 90 int i; 91 for(i=1;i<=G.vnum;i++) 92 printf("%d ",color[i]); 93 printf("\n"); 94 } 95 //****************************************************************** 96 void trycolor(int s,Graph G)//s为开始图色的顶点,本算法从1开始 97 { 98 int i; 99 if(s>G.vnum)//递归出口 100 { 101 output(G); 102 exit(1); 103 } 104 else 105 { 106 for(i=1;i<=N;i++)//对每一种色彩逐个测试 107 { 108 color[s]=i; 109 if(colorsame(s,G)==0) 110 trycolor(s+1,G);//进行下一块的着色 111 } 112 } 113 } 114 //***************************************************************** 115 int main() 116 { 117 Graph G; 118 CreateGraph(G); 119 PrintGraph(G); 120 printf("着色方案:\n"); 121 trycolor(1,G); 122 return 0; 123 }