7.9_401_课堂总结

· · 个人记录

P1379 八数码难题

题目链接

代码及解释

#include<bits/stdc++.h>
#define ll long long
using namespace std;
string st, ed = "123804765";//初始状态、结束状态
map<string, bool> mp;//存储搜到过的状态,查重
int dx[] = {-3, 3, -1, 1};//移动数组(上、下、左、右)

struct Node{
    int x, step;//“0”的位置、步数
    string s;//当前状态
};

void bfs(int x){
    queue<Node> que;//搜索时记录每步的队列
    que.push({x, 0, st});//队列初始化
    mp[st] = true;//查重数组初始化
    while(que.size()){//搜索
        Node f = que.front();//将当前状态存入一个结构体变量
        que.pop();//出队,表示已经搜到了这里
        if(f.s == ed){//当前状态等于结束状态时
            cout << f.step;//输出答案
            return;//结束
        }
        for(int i = 0; i < 4; i++){//移动
            int tx = f.x;//下一步“0”的位置
            string ss = f.s;//下一步的状态
            if(i == 2 && (tx == 0 || tx == 3 || tx == 6)){//边缘特判
                continue;//跳过
            }
            if(i == 3 && (tx == 2 || tx == 5 || tx == 8)){//边缘特判
                continue;//跳过
            }
            if(i == 0 && (tx == 0 || tx == 1 || tx == 2)){//边缘特判
                continue;//跳过
            }
            if(i == 1 && (tx == 6 || tx == 7 || tx == 8)){//边缘特判
                continue;//跳过
            }
            if(tx + dx[i] < 0 || tx + dx[i] >= st.size()){判断是否出界
                continue;//跳过
            }
            swap(ss[tx + dx[i]], ss[f.x]);//“0”移动
            if(mp[ss] == true){//如果当前状态已经搜过
                continue;   //跳过
            }
            mp[ss] = true;//标记当前状态
            que.push({tx + dx[i], f.step + 1, ss});//当前状态入队
        }
    }
}

int main(){
    cin >> st;
    int sx;//记录“0”的位置
    for(int i = 0; i < st.size(); i++){//寻找“0”的位置
        if(st[i] == '0'){//如果找到了“0”
            sx = i;//记录“0”的位置
        }
    }
    bfs(sx);//搜索
    return 0;
}

P2730 [USACO3.2] 魔板 Magic Squares

题目链接

代码及解释

#include<bits/stdc++.h>
#define ll long long
using namespace std;
string st = "12348765", ed;//初始状态、结束状态
map<string, bool> mp;//记录当前状态的数组

struct Node{
    int step;//步数
    string s, path;//当前状态及路径
};

string changeA(string s){//A操作
    string ts = s;
    for(int i = 0; i < 4; i++){
        swap(ts[i], ts[i + 4]);
    }
    return ts;
}

string changeB(string s){/B操作
    string ts = s;
    char a = ts[0], b = ts[1], c = ts[2], d = ts[3], e = ts[4], f = ts[5], g = ts[6], h = ts[7];
    ts[0] = d;
    ts[4] = h;
    ts[1] = a;
    ts[2] = b;
    ts[3] = c;
    ts[5] = e;
    ts[6] = f;
    ts[7] = g;
    return ts;
}

string changeC(string s){//C操作
    string ts = s;
    char a = ts[1], b = ts[2], c = ts[5], d = ts[6];
    ts[2] = a;
    ts[6] = b;
    ts[5] = d;
    ts[1] = c;
    return ts;
}

void bfs(){
    queue<Node> que;//搜索时记录每步的队列
    que.push({0, st, ""});//队列初始化
    mp[st] = true;//初始状态标记
    while(que.size()){//搜索
        Node f = que.front();//将当前状态存入一个结构体变量
        que.pop();//出队,表示已经搜到了这里
        if(f.s == ed){//当前状态等于结束状态时
            cout << f.step << "\n" << f.path;//输出答案
            return;//结束
        }
        string ts = changeA(f.s);//进行A操作并存下操作后的状态
        if(mp[ts] == false){//判断当前状态是否搜到过
            que.push({f.step + 1, ts, f.path + "A"});//当前状态入队
            mp[ts] = true;//标记当前状态
        }
        ts = changeB(f.s);//进行B操作并存下操作后的状态
        if(mp[ts] == false){//判断当前状态是否搜到过
            que.push({f.step + 1, ts, f.path + "B"});//当前状态入队
            mp[ts] = true;//标记当前状态
        }
        ts = changeC(f.s);//进行B操作并存下操作后的状态
        if(mp[ts] == false){//判断当前状态是否搜到过
            que.push({f.step + 1, ts, f.path + "C"});//当前状态入队
            mp[ts] = true;//标记当前状态
        }
    }
}

int main(){
    //U形输入初始状态-----------------------
    ed = "        ";
    for(int i = 0; i < 4; i++){
        int c;
        cin >> c;
        ed[i] = c + '0';
    }
    for(int i = 1; i <= 4; i++){
        int c;
        cin >> c;
        ed[8 - i] = c + '0';
    }
      //-------------------------------------
    bfs();//搜索
    return 0;
}