盲人過獨木橋

同學發來的題目,讓幫忙解決。一開始拿到這個題目,感覺無從下手(新手懵逼),感覺也比較麻煩。等腦子清醒後,再想這個題目,其實也不難,只要一個清晰的思路就可以迎刃而解。在此記錄下,方便回憶複習。

題目描述

解題思路

源代碼程序:

#include <iostream>

using namespace std;

#define MAX_PERSON_NUM 50   //最多人數
#define begin 0             //獨木橋 頭
#define end 100             //獨木橋 尾


class Person
{
public:
    Person();
public:
    int location;       //位置
    int patient_flag;   //患病標誌
    int left_brige_flag;//離開獨木橋標誌
};
//初始化(構造函數)
Person::Person()
{
    this->location = 0;
    this->patient_flag = 0;
    this->left_brige_flag = 0;
}

int main()
{
    int i = 0,j = 0;
    int patient_num = 0;    //患病人數
    int end_flag = 0;       //結束標誌
    int left_brige_num = 0; //離開獨木橋的人數
    int person_num = 0;     //輸入的盲人數

    Person Parr[MAX_PERSON_NUM];
    //測試案例
    /*Parr[1].location = -10;
    Parr[1].patient_flag = 1;
    Parr[2].location = 8;
    Parr[3].location = -20;
    Parr[4].location = 12;
    Parr[5].location = 25;*/

    cout<<"輸入盲人數量:";
    cin>>person_num;
    cout<<"輸入一行("<<person_num<<"個)"<<"用空格隔開的整數(-100 —— 100):"<<endl;

    //保存每個盲人的屬性
    for ( i = 1; i < person_num; i++)
    {
        cin>>Parr[i].location;//每個人的位置
        if (i == 1)
        {
            Parr[1].patient_flag = 1;//第一個患病
        }
    }

    while (end_flag == 0)
    {
        //1. 所有盲人 向前走一步 rate = 1m/s
        for ( i = 1; i < person_num; i++)
        {
            if (Parr[i].left_brige_flag == 0)
            {
                Parr[i].location++;
            }
        }
        //2. 是否相遇、患病
        for ( i = 1; i < person_num;  i++)
        {
            for ( j = i+1; j < person_num; j++)
            {
                if (Parr[i].left_brige_flag == 1 || Parr[j].left_brige_flag == 1)
                {
                    continue;
                }
                if (abs(Parr[i].location) == abs(Parr[j].location))//相遇
                {
                    Parr[i].location = 0 - Parr[i].location;//反向
                    Parr[j].location = 0 - Parr[j].location;

                    if ((Parr[i].patient_flag == 1) || (Parr[j].patient_flag == 1))
                    {
                        Parr[i].patient_flag = 1;//患病
                        Parr[j].patient_flag = 1;
                    }
                }
            }
        }
        //3. 到達起點或者終點
        for ( i = 1; i < person_num; i++)
        {
            if ((Parr[i].location == begin) || (Parr[i].location == end))
            {
                Parr[i].left_brige_flag = 1;
            }
        }

        left_brige_num = 0;
        //4. 結束條件
        for ( i = 1; i < person_num; i++)
        {
            if (Parr[i].left_brige_flag == 1)
            {
                left_brige_num++;
            }
        }
        if (left_brige_num == person_num - 1)
        {
            end_flag = 1;
        }
    }

    //計算總的患病人數
    for ( i = 1; i < person_num; i++)
    {
        if (Parr[i].patient_flag == 1)
        {
            patient_num++;
        }
    }

    cout<<endl;
    cout<<"patient_num:"<<patient_num<<endl;//輸出患病人數
    cout<<endl;
    system("pause");
    return 0;
}

這道題目用C++來寫還是比較簡單的,正好可以利用面向對象的class。用C寫也可以,需要創建一個PERSON的結構體,或者數組來解決,不過就不如C++容易理解和編寫代碼。

代碼有可以優化的地方,也沒有添加輸入數據異常的判斷。只是一個思路的實現。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章