Openjudge 佐助和鳴人(BFS+雙變量)

已知一張地圖(以二維矩陣的形式表示)以及佐助和鳴人的位置。地圖上的每個位置都可以走到,只不過有些位置上有大蛇丸的手下,需要先打敗大蛇丸的手下才能到這些位置。鳴人有一定數量的查克拉,每一個單位的查克拉可以打敗一個大蛇丸的手下。假設鳴人可以往上下左右四個方向移動,每移動一個距離需要花費1個單位時間,打敗大蛇丸的手下不需要時間。如果鳴人查克拉消耗完了,則只可以走到沒有大蛇丸手下的位置,不可以再移動到有大蛇丸手下的位置。佐助在此期間不移動,大蛇丸的手下也不移動。請問,鳴人要追上佐助最少需要花費多少時間?

輸入 輸入的第一行包含三個整數:M,N,T。代表M行N列的地圖和鳴人初始的查克拉數量T。0 < M,N < 200,0 ≤ T < 10
後面是M行N列的地圖,其中@代表鳴人,+代表佐助。*代表通路,#代表大蛇丸的手下。 輸出 輸出包含一個整數R,代表鳴人追上佐助最少需要花費的時間。如果鳴人無法追上佐助,則輸出-1。 樣例輸入
樣例輸入1
4 4 1
#@##
**##
###+
****

樣例輸入2
4 4 2
#@##
**##
###+
****

樣例輸出
樣例輸出1
6

樣例輸出2
4

  • 思路大概是用BFS,然後隊列中後取出的同一位置元素的t(時間)肯定比之前取過的t要大,但不排除t大但查克拉消耗少的情況,所以加個判定就OK。
  • 網上的代碼如下:
#include <cstdio>  
#include <cstring>  
#include <queue>  
#include <algorithm>  
using namespace std;  
  
const int maxn = 210;  
char g[maxn][maxn];  
int G[maxn][maxn];  
int m, n, w;  
int sr, sc, er, ec;  
int dr[4] = {0, 1, 0, -1};  
int dc[4] = {1, 0, -1, 0};  
  
struct Node  
{  
    int r, c, w, t;  
    Node(int r, int c, int w, int t) : r(r), c(c), w(w), t(t) {}  
};  
  
  
int main()  
{  
    scanf("%d%d%d", &m, &n, &w);  
    for(int i = 0; i < m; i++)  
    {  
        scanf("%s", g[i]);  
        for(int j = 0; j < n; j++)  
        {  
            G[i][j] = -1;           //每個格子的查克拉數量初始化爲-1,因爲鳴人沒有查克拉的時候(即爲0),依然可以走這個格子  
            if(g[i][j] == '@')  
                sr = i, sc = j;  
            if(g[i][j] == '+')  
                er = i, ec = j;  
        }  
    }  
    queue<Node> q;  
    q.push(Node(sr, sc, w, 0));  
    G[sr][sc] = w;  
    int ans = 1 << 30;  
    while(!q.empty())  
    {  
        Node p = q.front();  
        if((p.r == er && p.c == ec))  
        {  
            ans = p.t;  
            break;  
        }  
        for(int i = 0; i < 4; i++)  
        {  
            int tr = p.r+dr[i];  
            int tc = p.c+dc[i];  
            if(tr >= 0 && tr < m && tc >= 0 && tc < n )  
            {  
                if(g[tr][tc] == '#' && p.w > 0)  
                {  
                    q.push(Node(tr, tc, p.w-1, p.t+1));  
                    G[tr][tc] = p.w-1;  
                }  
                else if(g[tr][tc] == '*' || g[tr][tc] == '+')  
                {  
                    q.push(Node(tr, tc, p.w, p.t+1));  
                    G[tr][tc] = p.w;  
                }  
            }  
        }  
        q.pop();  
    }  
    if(ans != 1 << 30) printf("%d\n", ans);  
    else printf("-1");  
  
    return 0;  
}  

會報錯TE,原因大概是作者寫了G[][]後面又忘了用,AC代碼如下:
#include <cstdio>  
#include <cstring>  
#include <queue>  
#include <algorithm>  
using namespace std;  
  
const int maxn = 210;  
char g[maxn][maxn];  
int G[maxn][maxn];  
int m, n, w;  
int sr, sc, er, ec;  
int dr[4] = {0, 1, 0, -1};  
int dc[4] = {1, 0, -1, 0};  
  
struct Node  
{  
    int r, c, w, t;  
    Node(int r, int c, int w, int t) : r(r), c(c), w(w), t(t) {}  
};  
  
  
int main()  
{  
    scanf("%d%d%d", &m, &n, &w);  
    for(int i = 0; i < m; i++)  
    {  
        scanf("%s", g[i]);  
        for(int j = 0; j < n; j++)  
        {  
            G[i][j] = -1;           //ÿ¸ö¸ñ×ӵIJé¿ËÀ­ÊýÁ¿³õʼ»¯Îª-1£¬ÒòΪÃùÈËûÓвé¿ËÀ­µÄʱºò£¨¼´Îª0£©£¬ÒÀÈ»¿ÉÒÔ×ßÕâ¸ö¸ñ×Ó  
            if(g[i][j] == '@')  
                sr = i, sc = j;  
            if(g[i][j] == '+')  
                er = i, ec = j;  
        }  
    }  
    queue<Node> q;  
    q.push(Node(sr, sc, w, 0));  
    G[sr][sc] = w;  
    int ans = 1 << 30;  
    while(!q.empty())  
    {  
        Node p = q.front();  
        if((p.r == er && p.c == ec))  
        {  
            ans = p.t;  
            break;  
        }  
        for(int i = 0; i < 4; i++)  
        {  
            int tr = p.r+dr[i];  
            int tc = p.c+dc[i];  
            if(tr >= 0 && tr < m && tc >= 0 && tc < n && p.w>G[tr][tc]) //*** 
            {  
                if(g[tr][tc] == '#' && p.w > 0)  
                {  
                    q.push(Node(tr, tc, p.w-1, p.t+1));  
                    G[tr][tc] = p.w-1;  
                }  
                else if(g[tr][tc] == '*' || g[tr][tc] == '+')  
                {  
                    q.push(Node(tr, tc, p.w, p.t+1));  
                    G[tr][tc] = p.w;  
                }  
            }  
        }  
        q.pop();  
    }  
    if(ans != 1 << 30) printf("%d\n", ans);  
    else printf("-1");  
  
    return 0;  
}  

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