课堂作业

一、七巧板着色问题

#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;
int mp[maxn][maxn];
int path[maxn];
int V;
bool vis[maxn];
void print() {
    cout << "存在哈密顿回路" << endl;
    for (int i = 0; i < V; i++) cout << path[i]+1 << " ";
    cout << path[0]+1 << endl;
}
bool Hamidun(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;
            if (Hamidun(now+1)) return true;
            //当本条递归线路失败时恢复原图
            /****/
            path[now] = -1;
            vis[v] = false;
            /****/
        }
    }
    return false;
}
bool check() {
    path[0] = 0;
    vis[V] = true;
    if (Hamidun(1) == false) {
        cout << "哈密顿回路不存在" << endl;
        return false;
    }
    print();
    return true;
}
int main() {
    while(cin >> V) {
        for (int i = 0; i < V; ++i) {
            for (int j = 0; j < V; ++j) {
                cin >> mp[i][j];
            }
        }
        check();
    }
    return 0;
}
/*
5
1 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 1 1 0 0
*/
View Code

5
0 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 1
1 1 0 0 0
存在哈密顿回路
1 2 3 4 5 1

三、农夫过河问题

农夫带羊过河

农夫返回

农夫带狼过河

农夫带羊返回

农夫带菜过河

农夫返回

农夫带羊过河

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

每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
原文地址:https://www.cnblogs.com/buerdepepeqi/p/9180402.html