#include
#include
#include
using namespace std;
#define datatype char
#define ALLSTATE 1
#define SELECTSTATE 2
#define MAXSIZE 10
#define TOLEFT 0
#define TORIGHT 1
typedef struct node
{
int num;
datatype state[10];
bool IsRight;
struct node *next;
}MyState;
typedef struct
{
char vertex[MAXSIZE][MAXSIZE];
int StateGraph[MAXSIZE][MAXSIZE];
}MyGraph;
int num_situation = 0;
MyState *AllState = (MyState*)malloc(sizeof(MyState));
MyState *pEnd = AllState;
datatype way[] = { 'm','w','g','c' };
ofstream out("situation.txt");
ifstream in("situation.txt");
MyGraph *g = (MyGraph*)malloc(sizeof(MyGraph));
int visited[MAXSIZE];
void count_situation();
void comb(datatype a[], int n, int m, datatype b[], const int M);
void file2struct();
void fix(char state[]);
int output_allstate(int sel);
void select_situation();
bool IsRight(char temp[]);
void create_graph();
void init_Graph();
void GetNextState(MyState* NowState, int Edge[]);
void PassTheBrige(char temp[], char ch, int derction);
int GetStateNum(char state[]);
bool IsEqual(char array1[], char array2[]);
void output_graph();
void FindTheAnswer(int start, int end);
void DFS(int s, int e);
void printPath(int e);
char* Num2State(int num);
void main()
{
count_situation();
select_situation();
create_graph();
FindTheAnswer(GetStateNum("|mwgc"),GetStateNum("wmgc|"));
getchar();
}
void count_situation()
{
datatype b[4];
out << "|mwgc" << endl;
for (int i = 1; i <= 4; i++)
{
comb(way, 4, i, b, i);
}
out.close();
file2struct();
num_situation = output_allstate(ALLSTATE);
}
void comb(datatype a[], int n, int m, datatype b[], const int M)
{
for (int i = n; i >= m; i--)
{
b[m - 1] = i - 1;
if (m > 1)
{
comb(a, i - 1, m - 1, b, M);
}
else
{
for (int j = M - 1; j >= 0; j--)
{
out << a[b[j]];
}
out << "|" << endl;
}
}
}
void file2struct()
{
int number = 0;
char state[10];
if (in)
{
while (!in.eof())
{
in >> state;
fix(state);
MyState *temp = (MyState*)malloc(sizeof(MyState));
temp->num = number++;
temp->next = NULL;
strcpy(temp->state, state);
pEnd->next = temp;
pEnd = temp;
}
}
}
void fix(char state[])
{
int i = 0;
bool m = false, w = false, g = false, c = false;
while (state[i] != '|')
{
switch (state[i++])
{
case 'm':
m = true;
break;
case 'w':
w = true;
break;
case 'g':
g = true;
break;
case 'c':
c = true;
break;
default:
break;
}
}
if (m == false)
state[++i] = 'm';
if (w == false)
state[++i] = 'w';
if (g == false)
state[++i] = 'g';
if (c == false)
state[++i] = 'c';
}
void select_situation()
{
MyState *temp = AllState;
int i = -1;
while (temp->next != NULL)
{
if (!IsRight(temp->next->state))
{
MyState *del = temp->next;
temp->next = del->next;
free(del);
}
else
{
temp->num = i++;
temp = temp->next;
}
}
num_situation = output_allstate(SELECTSTATE);
}
bool IsRight(char temp[])
{
int i = 0;
bool m = false, w = false, g = false, c = false;
while (temp[i] != '|' && temp[i] != '\0')
{
switch (temp[i++])
{
case 'm':
m = true;
break;
case 'w':
w = true;
break;
case 'g':
g = true;
break;
case 'c':
c = true;
break;
default:
break;
}
}
if (w == true && g == true && c == true && m == false)
return false;
if (m == true && w == false && g == false && c == false)
return false;
if (g == true && c == true && m == false && w == false)
return false;
if (m == true && w == true && g == false && c == false)
return false;
if (w == true && g == true && m == false && c == false)
return false;
if (m == true && c == true && w == false && g == false)
return false;
return true;
}
int output_allstate(int sel)
{
int i = 1;
MyState *temp = AllState->next;
if (sel == ALLSTATE)
cout << "-------------所有的情况----------------" << endl;
else if (sel == SELECTSTATE)
cout << "-------------筛选后情况----------------" << endl;
while (temp->next != NULL)
{
cout << "第" << i++ << "种情况:" << temp->state << endl;
temp = temp->next;
}
return i - 1;
}
void create_graph()
{
init_Graph();
int Edge[4] = { -1,-1,-1,-1 };
MyState *temp = AllState->next;
int i = 0;
while (temp->next != NULL)
{
strcpy(g->vertex[i++], temp->state);
GetNextState(temp,Edge);
for (int j = 0; j < 4; j++)
{
if (Edge[j] != -1)
{
g->StateGraph[temp->num][Edge[j]] = 111;
}
}
for (int j = 0; j < 4; j++)
{
Edge[j] = -1;
}
temp = temp->next;
}
output_graph();
}
void init_Graph()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
g->StateGraph[i][j] = 0;
}
}
}
void GetNextState(MyState* NowState, int Edge[])
{
int i_Edge = 0;
int i = 0;
bool m = false, w = false, g = false, c = false;
char Copy[10];
strcpy(Copy, NowState->state);
while (NowState->state[i] != '|' && NowState->state[i] != '\0')
{
switch (NowState->state[i++])
{
case 'm':
m = true;
break;
case 'w':
w = true;
break;
case 'g':
g = true;
break;
case 'c':
c = true;
break;
default:
break;
}
}
if (m == true)
{
if (w == true)
{
PassTheBrige(Copy, 'w', TORIGHT);
PassTheBrige(Copy, 'm', TORIGHT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
if (g == true)
{
PassTheBrige(Copy, 'g', TORIGHT);
PassTheBrige(Copy, 'm', TORIGHT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
if (c == true)
{
PassTheBrige(Copy, 'c', TORIGHT);
PassTheBrige(Copy, 'm', TORIGHT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
PassTheBrige(Copy, 'm', TORIGHT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
else
{
if (w == false)
{
PassTheBrige(Copy, 'w', TOLEFT);
PassTheBrige(Copy, 'm', TOLEFT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
if (g == false)
{
PassTheBrige(Copy, 'g', TOLEFT);
PassTheBrige(Copy, 'm', TOLEFT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
if (c == false)
{
PassTheBrige(Copy, 'c', TOLEFT);
PassTheBrige(Copy, 'm', TOLEFT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
PassTheBrige(Copy, 'm', TOLEFT);
if (IsRight(Copy))
{
Edge[i_Edge++] = GetStateNum(Copy);
}
strcpy(Copy, NowState->state);
}
}
void PassTheBrige(char temp[], char ch, int derction)
{
int i = 0, j = 0;
for (i = 0; i < 5; i++)
{
if (temp[i] == ch)
break;
}
for (j = i; j < 5; j++)
{
temp[j] = temp[j + 1];
}
for (i = 0; i < 5; i++)
{
if (temp[i] == '|')
break;
}
if (derction == TORIGHT)
{
temp[4] = ch;
}
else
{
for (j = 4; j >= i; j--)
{
temp[j + 1] = temp[j];
}
temp[i] = ch;
}
}
int GetStateNum(char state[])
{
MyState* temp = AllState->next;
while (temp->next != NULL)
{
if (IsEqual(temp->state,state))
{
return temp->num;
}
else
{
temp = temp->next;
}
}
return -1;
}
bool IsEqual(char array1[], char array2[])
{
int i = 0;
bool m1 = false, w1 = false, g1 = false, c1 = false;
bool m2 = false, w2 = false, g2 = false, c2 = false;
while (array1[i] != '|' && array1[i] != '\0')
{
switch (array1[i++])
{
case 'm':
m1 = true;
break;
case 'w':
w1 = true;
break;
case 'g':
g1 = true;
break;
case 'c':
c1 = true;
break;
default:
break;
}
}
i = 0;
while (array2[i] != '|' && array2[i] != '\0')
{
switch (array2[i++])
{
case 'm':
m2 = true;
break;
case 'w':
w2 = true;
break;
case 'g':
g2 = true;
break;
case 'c':
c2 = true;
break;
default:
break;
}
}
if (m1 == m2 && w1 == w2 && g1 == g2 && c1 == c2)
return true;
else
return false;
}
void output_graph()
{
int i = 0, j = 0;
cout << endl;
cout << "-------------邻接矩阵为----------------" << endl;
cout << " ";
for (i = 0; i < 10; i++)
{
cout << g->vertex[i] << " ";
}
cout << endl;
for (i = 0; i < 10; i++)
{
cout << g->vertex[i] << " ";
for (j = 0; j < 10; j++)
{
cout << g->StateGraph[i][j]<<" ";
}
cout << endl;
cout << endl;
}
cout << endl;
}
void FindTheAnswer(int start, int end)
{
for (int i = 0; i < MAXSIZE; i++)
{
visited[i] = -1;
}
visited[start] = start;
cout << endl;
cout << "-------------最终结果为----------------" << endl;
DFS(start, end);
}
void DFS(int s, int e)
{
if (s == e)
{
printPath(e);
cout << endl;
}
for (int i = 1; i < MAXSIZE; i++)
{
if (g->StateGraph[s][i] > 0 && visited[i] == -1)
{
visited[i] = s;
DFS(i, e);
visited[i] = -1;
}
}
}
void printPath(int e)
{
if (visited[e] != e)
{
printPath(visited[e]);
cout << "-->";
}
cout << " (" << Num2State(e) << ") ";
}
char* Num2State(int num)
{
MyState* temp = AllState->next;
while (temp->next != NULL)
{
if (temp->num == num)
{
return temp->state;
}
else
{
temp = temp->next;
}
}
return NULL;
}
最终的结果为: