课堂作业

一、七巧板着色问题

#include <iostream>

using namespace std;

//三角板个数
const int n=7;
//邻接矩阵表,用来判断是否相邻
const int data[n][n] = {
    {0,1,0,0,1,0,1},
    {1,0,0,1,0,1,0},
    {0,0,0,0,1,0,1},
    {0,1,1,0,0,1,1},
    {1,0,0,0,0,0,1},
    {0,1,0,1,0,0,0},
    {1,0,1,1,1,0,0}
};
//每个三角板的颜色
int color[n]= {0,0,0,0,0,0,0};

static int total;
void tryFind(int s);//查找涂色方案的递归
int colorSame(int s);//判断与周围的三角板颜色是否相同
void output();//输出涂色方案

int main() {
    total=0;
    tryFind(0);
    cout<<"Total= "<<total<<endl;
    return 0;
}

void tryFind(int s) {
    //s=0~6,如果s=7说明已经涂完
    if(s==n)
        output();
    else {
        //1、2、3、4代表四种颜色
        //只有与周围三角块的颜色不同时才会进入下一个三角板的涂色
        for(int i=1; i<=4; i++) {
            color[s]=i;
            if(colorSame(s)==0)
                tryFind(s+1);
        }
    }
}

int colorSame(int s) {
    int flag=0;
    for(int i=0; i<s; i++) {
        //使用邻接矩阵判断是否相邻
        //如果相邻判断颜色是否相同
        if(data[i][s]==1 && color[i]==color[s])
            flag=1;
    }
    return flag;
}

void output() {
    cout<<"serial number: ";
    int flag1=0;
    int flag2=0;
    int flag3=0;
    int flag4=0;
    for(int i=0; i<n; i++) {
//        cout<<color[i]<<" ";
        if(color[i]==1) {
            flag1=1;
            cout<<""<<" ";
        } else if(color[i]==2) {
            flag2=1;
            cout<<""<<" ";
        } else if(color[i]==3) {
            flag3=1;
            cout<<""<<" ";
        } else {
            flag4=1;
            cout<<"绿"<<" ";
        }
    }
    cout<<endl;
    int k=flag1+flag2+flag3+flag4;
    cout<<"一共用了"<<k<<"种颜色"<<endl;
    total++;
    cout<<endl;
}
View Code

二、哈密顿路径问题

哈密顿回路: 
指一个对图的每个顶点都只穿越一次的回路。也可以 定义为n+1个相邻顶点v0, v1, … ,vn, v0的一个序列,其中序列的第一个顶点和最后一个顶点是相同的,而其他n-1个顶点是互不相同的。 

递归求解哈密顿回路

#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 50;
void print(int path[], int V)
{
    cout << "存在哈密顿回路" << endl;
    for (int i = 0; i < V; i++) cout << path[i] << " ";
    cout << path[0] << endl;
}
bool Hamidun(int mp[][maxn], int V, int path[], bool vis[], int now) {
    if (now == V) {
        if (mp[path[now - 1]][0] == 1)  return true;
        else return false;
    }
    for (int v = 1; v < V; v++) {
        //如果没访问过,并且有边相连
        if (!vis[v] && mp[path[now - 1]][v] == 1) {
            vis[v] = true;
            path[now] = v;
            //当本次递归的child也为true时返回true
            if (Hamidun(mp, V, path, vis, now + 1)) return true;
            //当本条递归线路失败时恢复原图
            path[now] = -1;
            vis[v] = false;
        }
    }
    return false;
}
bool check(int mp[][maxn], int V) {
    int path[maxn];
    memset(path, -1, sizeof(path));
    bool vis[maxn] = { 0 };
    path[0] = 0;
    vis[V] = true;
    if (Hamidun(mp, V, path, vis, 1) == false) {
        cout << "哈密顿回路不存在" << endl;
        return false;
    }
    print(path, V);
    return true;
}
int main() {
    int mp[maxn][maxn];
    int V;
    cin >> V;
    for (int i = 0;i < V;++i)
    {
        for (int j = 0;j < V;++j)
        {
            cin >> mp[i][j];
        }
    }
    check(mp, V);
    return 0;
}
View Code

三、农夫过河问题

农夫带羊过河

农夫返回

农夫带狼过河

农夫带羊返回

农夫带菜过河

农夫返回

农夫带羊过河

emmm暂时不知道怎么用代码实现orz

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/9180402.html