程序設計思維與實踐 Week2 作業 (3/4/數據班)

寫在前面

在看代碼的過程中整理了動態數組的相關內容,由於篇幅較長,且比較重要,單獨整理了一篇博客如下:

動態數組初始化

A - MAZE

題意

東東有一張地圖,想通過地圖找到妹紙。地圖顯示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹紙,這兩個位置保證爲0。既然已經知道了地圖,那麼東東找到妹紙就不難了,請你編一個程序,寫出東東找到妹紙的最短路線。輸入是一個5 × 5的二維數組,且保證一定有解

思路

從(0,0)位置開始BFS,每次向上下左右四個方向擴展狀態。直到最終達到(4,4)爲止。

可以map,來記錄路徑。map中的關係爲當前狀態又哪一個狀態轉移而來。

根據BFS的性質,第一個達到(4,4)的值定位最優解。

總結

這是一道簡答的BFS題,利用map記錄路徑即可。

這裏注意,對於結構體重載比較運算符只需要重載一個<就夠了,至於爲什麼,因爲c++會自動補出其它運算符。PS:只能用於<,其它符號不可以。

代碼

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
using namespace std;
struct re {
    int x, y;
    bool operator<(const re& a) const { return x != a.x ? x < a.x : y < a.y; }
};

int vis[10][10], v[10][10];
int fx[] = {0, 0, 1, -1};
int fy[] = {1, -1, 0, 0};
map<re, re> M;
void dfs(re s) {
//	cout << s.x<<" "<< s.y<<endl;
    if (M.find(s) != M.end()) {
        dfs(M[s]);
    }
    cout << "(" << s.x << ", " << s.y <<")"<<endl;
}
void bfs() {
    queue<re> Q;
    Q.push({0, 0});
    vis[0][0]= 1;
    while (!Q.empty()) {
        re s = Q.front();
        Q.pop();
        if (s.x == 4 && s.y == 4) {
            dfs({4, 4});
            return;
        }
        for (int i = 0; i < 4; i++) {
            int tx = s.x + fx[i], ty = s.y + fy[i];
            if (tx < 0 || ty < 0 || tx > 4 || ty > 4 || vis[tx][ty] || v[tx][ty]) continue;
            M[{tx, ty}] = s;
            vis[tx][ty] = 1;
            Q.push({tx, ty});
        }
    }
}
int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cin >> v[i][j];
        }
    }
    bfs();
    return 0;
}

B - Pour Water

題意

倒水問題 “fill A” 表示倒滿A杯,"empty A"表示倒空A杯,“pour A B” 表示把A的水倒到B杯並且把B杯倒滿或A倒空。

思路

首先初始化位兩個杯子均爲空(0,0)。

對於每一個狀態,至多有六種狀態可以轉移:

  • 倒滿A
  • 倒滿B
  • 倒空A
  • 倒空B
  • A倒入B
  • B倒入A

注意進行適當剪枝,如當前A已空,不必再次倒空A即可。

同樣可以利用map來記錄路徑。

總結

這是一個類隱藏圖問題,將每一個狀態看成一個點,擴展出其它節點。直到滿足條件。同樣可以利用map來記錄路徑。

代碼

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
using namespace std;
struct re {
    int x, y;
    string str;
    bool operator < (const re& a) const { return x != a.x ? x < a.x : y < a.y; }
};
map<re, re> M;
void dfs(re t) {
    if (t.x == 0 && t.y == 0) {
    	return;    
    }
    dfs(M[t]);
    cout << t.str << endl;
}
void bfs(int A, int B, int C) {
    queue<re> Q;
    M.clear();
    re fnode;
    fnode.x = fnode.y = 0;
    Q.push(fnode);
    //    cout << A<<" "<<B<<" "<<C<<endl;
    while (!Q.empty()) {
        re snode = Q.front();

        Q.pop();
        //        cout << snode.x<<" "<<snode.y<<endl;
        if (snode.x == C || snode.y == C) {
            dfs(snode);
            cout << "success" << endl;
            return;
        }
        if (snode.x != A) {  // x 倒滿
            re tnode = snode;
            tnode.x = A;
            tnode.str = "fill A";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }
        if (snode.y != B) {  // y 倒滿
            re tnode = snode;
            tnode.y = B;
            tnode.str = "fill B";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }
        if (snode.y != 0) {  // y 倒空
            re tnode = snode;
            tnode.y = 0;
            tnode.str = "empty B";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }
        if (snode.x != 0) {  // x 倒空
            re tnode = snode;
            tnode.x = 0;
            tnode.str = "empty A";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }
        if (snode.x != 0) {  // x 倒給 y
            re tnode = snode;
            tnode.y = min(B, tnode.y + tnode.x);
            tnode.x = snode.x - (tnode.y - snode.y);
            tnode.str = "pour A B";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }
        if (snode.y != 0) {  // y 倒給 x
            re tnode = snode;
            tnode.x = min(A, tnode.x + tnode.y);
            tnode.y = snode.y - (tnode.x - snode.x);
            tnode.str = "pour B A";
            if (M.find(tnode) == M.end()) {
                M[tnode] = snode;
                Q.push(tnode);
            }
        }

        /* code */
    }
}
int main() {
    int A, B, C;
    while (cin >> A >> B >> C) {
        bfs(A, B, C);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章