爬格子呀6-4、6-5、6-9

来源:互联网 发布:上海乐蜀网络 编辑:程序博客网 时间:2024/05/17 00:08

今天又写了三个,准确的说是昨天写的,但是昨晚很久才睡的,所以就把它放到今天发了,主题是bfs,学会了还是很开心的;
代码如下:
6-4:

#include<cstdio>#include<iostream>#include<queue>using namespace std;int a[8][8];int b1, b2, d1, d2;const int INF = 0x3f3f3f;int dir[8][2] = { {1,2},{1,-2},{-1,2},{-1,-2},{2,1},{-2,1},{-2,-1},{2,-1} };bool legal(int x, int y) {    return x >= 0 && x < 8 && y >= 0 && y < 8;}struct poi {    int x, y;    poi():x(0),y(0){}};poi po1, po2;//整个bfs没有涉及到递归调用//因为每个点的相关点在同一层中被遍历到的时候路径都是一样的,所以就不存在记录距离大小进行比较的问题int bfs() {    queue<poi>p;    p.push(po1);//放进第一个点    a[po1.x][po1.y] = 0;//标记    while (p.size()) {//这个条件的确立就是保证他可以遍历所有的点        poi mid = p.front(), mid2;//递归的开始,也不能算是递归        p.pop();        int x = mid.x, y = mid.y;        if (x == po2.x&&y == po2.y)            return a[x][y];//这个是判断条件        for (int i = 0; i < 8; i++) {            mid2.x = x + dir[i][0];            mid2.y = y + dir[i][1];            if (legal(mid2.x, mid2.y) && a[mid2.x][mid2.y] == INF)                p.push(mid2);//放入相关点,或者连接点            a[mid2.x][mid2.y] = a[x][y] + 1;//这个是记录距离用的        }    }    return INF;}int main() {    char c1, c2;    cin >> c1 >> b1 >> c2 >> b2;    po1.x = 8 - b1;    po2.x = 8 - b2;    po1.y = c1 - 'a';    po2.y = c2 - 'a';    for (int i = 0; i < 8; i++)        for (int j = 0; j < 8; j++)            a[i][j] = INF;    cout << bfs();    return 0;}

6-5:

#include<cstdio>#include<iostream>#include<queue>using namespace std;int a[20][20], m, n, k, b[20][20];int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };const int INF = 0x3f3f3f3f;bool legal(int x, int y) {    return x >= 0 && x < m&&y >= 0 && y < n;}struct poi {    int x, y, t;    poi():x(0),y(0),t(0){}};poi po1, po2;//a为最短路径值,b为血量分布;int bfs() {    queue<poi>p;    p.push(po1);    a[0][0] = 0;    while (p.size()) {        poi mid = p.front(), mid1;        p.pop();        if (mid.x == po2.x&&mid.y == po2.y)            return a[mid.x][mid.y];        for (int i = 0; i < 4; i++) {            mid1.x = mid.x + dir[i][0];            mid1.y = mid.y + dir[i][1];            if (legal(mid1.x, mid1.y))            {                mid1.t = mid.t + b[mid1.x][mid1.y];                if (mid1.t < k) {                     a[mid1.x][mid1.y] = a[mid.x][mid.y] + 1;                    p.push(mid1);                }            }        }    }    return INF;}int main() {    cin >> m >> n >> k;    int i, j;    for (i = 0; i < m; i++) {        for (j = 0; j < n; j++)        {            cin >> b[i][j];            a[i][j] = INF;        }    }    po2.x = m - 1;    po2.y = n - 1;    cout << bfs();    return 0;}

6-9:

#include<cstdio>#include<iostream>#include<stack>#include<list>#include<map>#include<string>using namespace std;struct brand {    string suit;    string rank;};list<stack<brand>>a;bool can_move(brand x,brand y) {    return x.suit == y.suit || x.rank == y.rank;}void move(list<stack<brand>>::iterator &it, list<stack<brand>>::iterator &ittt) {    brand k;    k = it->top();    it->pop();    ittt->push(k);    if (it->empty())        a.erase(it);    it = ittt;}bool a_move(list<stack<brand>>::iterator &it) {    list<stack<brand>>::iterator itt, ittt;    itt = it;    advance(itt, -1);    ittt = itt;    if (distance(itt, a.begin()) >= 2)        advance(ittt, -2);    bool can1, can3;    can1 = can_move(it->top(), itt->top());    can3 = can_move(it->top(), ittt->top());    if (can1&&can3)    {        move(it, ittt);        return true;    }    else if (can3)    {        move(it, ittt);        return true;    }    else if (can1)    {        move(it, itt);        return true;    }    else        return false;}int main() {    int i = 52;    brand mid;    stack<brand>mid1;    string ini;    while (i--) {        cin >> ini;        mid.rank = ini.front();        mid.suit = ini.substr(1);        mid1.push(mid);        a.push_back(mid1);    }    list<stack<brand>>::iterator it=a.begin(), itt, ittt;    it++;    while (it != a.end()) {        while (a_move(it)) {}        it++;    }    cout << a.size() << "piles remaining:";    for (auto i : a)         cout << i.size() << ' ';    return 0;}
原创粉丝点击