zoukankan      html  css  js  c++  java
  • poj3984

    简单题

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 10

    struct Point
    {
    int x, y;
    }q[maxn
    * maxn], stk[maxn * maxn];

    int map[maxn][maxn];
    int cost[maxn][maxn];
    int from[maxn][maxn];
    int dir[4][2] =
    {
    {
    1, 0 },
    {
    0, 1 },
    {
    -1, 0 },
    {
    0, -1 } };

    bool ok(Point &a)
    {
    if (a.x < 0 || a.y < 0 || a.x >= 5 || a.y >= 5)
    return false;
    return !map[a.x][a.y] && (cost[a.x][a.y] == -1);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
    scanf(
    "%d", &map[i][j]);
    int l = 0;
    int r = 1;
    q[
    0].x = 0;
    q[
    0].y = 0;
    memset(cost,
    -1, sizeof(cost));
    cost[
    0][0] = 0;
    while (l != r)
    {
    for (int i = 0; i < 4; i++)
    {
    Point p;
    p.x
    = q[l].x + dir[i][0];
    p.y
    = q[l].y + dir[i][1];
    if (ok(p))
    {
    q[r
    ++] = p;
    from[p.x][p.y]
    = i;
    cost[p.x][p.y]
    = cost[q[l].x][q[l].y] + 1;
    }
    }
    l
    ++;
    }
    int top = 0;
    Point a;
    a.x
    = 4;
    a.y
    = 4;
    int t;
    while (!(a.x == 0 && a.y == 0))
    {
    stk[top
    ++] = a;
    t
    = from[a.x][a.y];
    a.x
    -= dir[t][0];
    a.y
    -= dir[t][1];
    }
    printf(
    "(0, 0)\n");
    while (top--)
    printf(
    "(%d, %d)\n", stk[top].x, stk[top].y);
    return 0;
    }

  • 相关阅读:
    iOS进阶_三方使用步骤
    Runtime
    感想
    git
    随笔感想
    关于APP上架问题需要ipad图标的问题
    ubuntu安装
    JNI和NDK
    数据结构——队列链表实现
    数据结构——栈的实现(数组、Java)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2096930.html
Copyright © 2011-2022 走看看