HDU查找(2)

1026 Ignatius and the Princess I

找最短路徑問題用bfs,注意對發生戰鬥位置的處理

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

class pos
{
public:
    //當前座標
    int i, j;
    //下一點座標,默認爲-1,-1
    int ii, jj;
    //走到下一座標需要的時間,不需要戰鬥時爲0,需要戰鬥時爲c[ii][jj] - '0'
    int time;

    pos(int ai, int aj, int atime = 0, int aii = -1, int ajj = -1)
        :i(ai),j(aj),time(atime),ii(aii),jj(ajj){}

    pos(){}
};

int n, m;
char c[101][101], cc[101][101];
//記錄每個點下一點的行座標和列座標,初始爲-1
int npl[101][101];
int npr[101][101];
queue<pos> p;

int t(char c)
{
    if (c == '.')
        return 0;
    else
        return c - '0';
}


bool bfs()
{
    p.push(pos(n - 1, m - 1, t(c[n - 1][m - 1])));

    while (p.size())
    {
        pos a = p.front();
        p.pop();
        if (c[a.i][a.j] == 'X')
            continue;
		//如果有戰鬥
        if (a.time > 0)
            p.push(pos(a.i, a.j, a.time - 1, a.ii, a.jj));
        else
        {

            //向上
            if (a.i != 0 && c[a.i - 1][a.j] != 'X')
                p.push(pos(a.i - 1, a.j, t(c[a.i][a.j]), a.i, a.j));
            //向左
            if (a.j != 0 && c[a.i][a.j - 1] != 'X')
                p.push(pos(a.i, a.j - 1, t(c[a.i][a.j]), a.i, a.j));
            //向下
            if (a.i != n - 1 && c[a.i + 1][a.j] != 'X')
                p.push(pos(a.i + 1, a.j, t(c[a.i][a.j]), a.i, a.j));
            //向右
            if (a.j != m - 1 && c[a.i][a.j + 1] != 'X')
                p.push(pos(a.i, a.j + 1, t(c[a.i][a.j]), a.i, a.j));

            //該點到終點的最短路線已被找到
            c[a.i][a.j] = 'X';
            npl[a.i][a.j] = a.ii;
            npr[a.i][a.j] = a.jj;

            if (a.i == 0 && a.j == 0)
                break;
        }
    }

    //是否有路徑
    if (npl[0][0] == -1)
        return false;
    else
        return true;
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF)
    {
        for (int i = 0; i < 100; i++)
            for (int j = 0; j < 100; j++)
                npl[i][j] = npr[i][j] = -1;
        while (p.size())
            p.pop();

        for (int i = 0; i < n; i++)
            scanf("%s", c[i]);

        memcpy(cc, c, sizeof(cc));

        if (!bfs())
            printf("God please help our poor hero.\nFINISH\n");
        else
        {
            int k = 0; 
            for (int i = 0, j = 0; npl[i][j] != -1;)
            {
                if (cc[i][j] == '.')
                    k++;
                else
                    k += cc[i][j] - '0' + 1;

                int tempi = npl[i][j];
                j = npr[i][j];
                i = tempi;
            }
            //如果終點有戰鬥
            if (cc[n - 1][m - 1] != '.')
            {
                char des = cc[n - 1][m - 1];
                while (cc[n - 1][m - 1]-- != '0')
                    ++k;
                cc[n - 1][m - 1] = des;
            }
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", k);
            
            k = 0;
            for (int i = 0, j = 0; npl[i][j] != -1;)
            {
                if (cc[i][j] != '.')
                    while (cc[i][j]-- != '0')
                        printf("%ds:FIGHT AT (%d,%d)\n", ++k, i, j);
                printf("%ds:(%d,%d)->(%d,%d)\n", ++k, i, j, npl[i][j], npr[i][j]);


                int tempi = npl[i][j];
                j = npr[i][j];
                i = tempi;
            }
            if (cc[n - 1][m - 1] != '.')
                while (cc[n - 1][m - 1]-- != '0')
                    printf("%ds:FIGHT AT (%d,%d)\n", ++k, n - 1, m - 1);
            printf("FINISH\n");
        }
    }
}

1043 Eight

有限種情況的問題可以用bfs暴力打表,當前狀態(string)可以轉化成int來儲存,注意輸入會有"12345678x"的情況這時只要返回"lr"即可,最後輸出不唯一,與bfs時先計算哪個方向有關

#pragma warning(disable:4996)
#include<cstdio>
#include<map>
#include<cmath>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
using namespace std;


struct Node {
    string state, str;
    int pos;
};

char beginmap[10];
map<int, string> mymap;


long long getnum(const char* p)
{
    long long val = 0;
    for (int i = 0; i < 9; i++)
    {
        val += *(p + i) - '0';
        val *= 10;
    }
    return val;
}

void bfs()
{
    queue<Node> moves;
    moves.push({ "123456780" ,"", 8 }); mymap[getnum("123456780")] = "";
    while (!moves.empty())
    {
        
        char state[10], a; 
        long long val;
        Node tmp = moves.front() ; moves.pop();
        string str = tmp.str;
        int pos = tmp.pos;

        if (pos % 3 != 2)
        {
            strcpy(state, tmp.state.c_str());
            a = state[pos];
            state[pos] = state[pos + 1];
            state[pos + 1] = a;
            val = getnum(state);
            if (mymap.find(val) == mymap.end())
            {
                string result = "l" + str;
                moves.push({ state,result,pos + 1 });
                mymap[val] = result;
            }
        }

        if (pos / 3 != 2)
        {
            strcpy(state, tmp.state.c_str());
            a = state[pos];
            state[pos] = state[pos + 3];
            state[pos + 3] = a;
            val = getnum(state);
            if (mymap.find(val) == mymap.end())
            {
                string result = "u" + str;
                moves.push({ state,result,pos + 3 });
                mymap[val] = result;
            }
        }

        if (pos % 3 != 0)
        {
            strcpy(state, tmp.state.c_str());
            a = state[pos];
            state[pos] = state[pos - 1];
            state[pos - 1] = a;
            val = getnum(state);
            if (mymap.find(val) == mymap.end())
            {
                string result = "r" + str;
                moves.push({ state,result,pos - 1 });
                mymap[val] = result;
            }
        }
        if (pos / 3 != 0)
        {
            strcpy(state, tmp.state.c_str());
            a = state[pos];
            state[pos] = state[pos - 3];
            state[pos - 3] = a;
            val = getnum(state);
            if (mymap.find(val) == mymap.end())
            {
                string result = "d" + str;
                moves.push({ state,result,pos - 3 });
                mymap[val] = result;
            }
        }

    }
    
}

int main()
{
    char get[50];
    bfs();
    while (gets(get) != NULL)
    {
        for (char* p = get, *q = beginmap; *p != '\0'; p++)
        {
            if (*p != ' ')
            {
                if (*p == 'x')
                    *q = '0';
                else
                    *q = *p;
                q++;
            }
        }
        if (!strcmp(beginmap,"123456780"))
        {
            printf("lr\n");
            continue;
        }

        map<int, string>::iterator it = mymap.find(getnum(beginmap));
        if (it == mymap.end())
            printf("unsolvable\n");
        else
            printf("%s\n", it->second.c_str());
    }


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