EOJ 徒弟的下山之路(广度优先搜索+优先队列)

Andrew-Malcom edited 2 年,5 月前

include

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<string>
using namespace std;//bfs
const int INF = 0x3f3f3f;
const int dx[] = { 0,0,1,1 };
const int dy[] = { -1,1,0,1 };
int times[1001][1001] = { 0 };
int num[1001][1001];
int n;
struct Node {
    int x, y, times;
    Node(int x, int y, int times) :x(x), y(y), times(times) {}
};
void bfs()
{
    int i, j, k;
    memset(times, INF, sizeof(times));
    queue<Node>q;
    for (i = 0; i < n; i++) {
        for (j = 0; j <= i; j++) cin >> num[i][j];
    }
    q.push(Node(0, 0, num[0][0]));
    times[0][0] = num[0][0];
    while (!q.empty())
    {
        Node cue = q.front();
        q.pop();
        for (i = 0; i < 4; i++) {
            int xx = cue.x + dx[i];
            int yy = cue.y + dy[i];
            int time = num[xx][yy] + times[cue.x][cue.y];
            if (num[xx][yy]&& time < times[xx][yy]) {
                times[xx][yy] = time;
                q.push(Node(xx, yy, time));
            }
        }
    }
    cout << times[n - 1][0];
}
int main()
{
    cin >> n;
    bfs();
    return 0;
}

Past Versions

Comments

10195101416-NiYuanCheng

帅气