zoukankan      html  css  js  c++  java
  • poj3009

    简单题

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

    #define maxn 25

    struct Point
    {
    int x, y;
    } s, e;

    bool map[maxn][maxn];
    int n, m, ans;
    bool found;
    int dir[4][2] =
    {
    {
    0, 1 },
    {
    1, 0 },
    {
    0, -1 },
    {
    -1, 0 } };

    void input()
    {
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    {
    int a;
    scanf(
    "%d", &a);
    if (a == 1)
    map[i][j]
    = false;
    else
    map[i][j]
    = true;
    if (a == 2)
    {
    s.x
    = i;
    s.y
    = j;
    }
    if (a == 3)
    {
    e.x
    = i;
    e.y
    = j;
    }
    }
    }

    int fail(Point &a)
    {
    if (a.x < 0 || a.y < 0 || a.x >= n || a.y >= m)
    return -1;
    if (!map[a.x][a.y])
    return 1;
    return 0;
    }

    void dfs(Point &a, int step)
    {
    if (step >= 10)
    return;
    Point b;
    for (int i = 0; i < 4; i++)
    {
    b.x
    = a.x + dir[i][0];
    b.y
    = a.y + dir[i][1];
    if (fail(b))
    continue;
    int x;
    while (!(x = fail(b)))
    {
    if (b.x == e.x && b.y == e.y)
    {
    found
    = true;
    ans
    = min(ans, step + 1);
    return;
    }
    b.x
    += dir[i][0];
    b.y
    += dir[i][1];
    }
    if (x == -1)
    continue;
    map[b.x][b.y]
    = true;
    b.x
    -= dir[i][0];
    b.y
    -= dir[i][1];
    dfs(b, step
    + 1);
    b.x
    += dir[i][0];
    b.y
    += dir[i][1];
    map[b.x][b.y]
    = false;
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d%d", &m, &n), m | n)
    {
    input();
    found
    = false;
    ans
    = 20;
    dfs(s,
    0);
    if (found)
    printf(
    "%d\n", ans);
    else
    printf(
    "%d\n", -1);
    }
    return 0;
    }

  • 相关阅读:
    JavaScript基础
    Dao的扩展
    错题解析
    实现windows程序的数据绑定
    C#第三章
    第二章
    初始windows程序
    使用ADO.NET查询和操作数据
    使用ADO.NET访问数据库
    4.计算机层次与编程语言
  • 原文地址:https://www.cnblogs.com/rainydays/p/2095919.html
Copyright © 2011-2022 走看看