uva676 Horse Step Maze

C - Horse Step Maze
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


  Horse Step Maze 

Maze search has been developed for a long time, and the related competition is held often. In this problem you have to design a program that simulates a computer mouse to find a path in a maze from a starting point to an ending point. To find such a path, the computer mouse must follow the `knight' steps as shown in Fig. 1. The numbers 1, 2, 3 and 4 indicate the sequence of four directions the computer mouse has to try while searching for a path. Specifically, the computer mouse must always try direction 1 first until it cannot continue searching the path to the ending point in the maze. When that happens, the computer mouse can backtrack and try direction 2. Similarly, if direction 2 can not work, direction 3 is tried and then direction 4.

Actually, our computer mouse is quite intelligent. Before it tries direction 1 it drops a stone. The mouse won't go into a field with a stone on it (unless it is backtracking). But it's not very clever, so after trying direction 4 (before it backtracks) it removes the stone.


\begin{picture}(20,12)(0,0)\put (10,6){\circle*{12}}\end{picture} represents the location of computer mouse at present


\begin{picture}(20,12)(0,0)\put (0,6){\vector(1,0){20}}\end{picture} represents the step that computer mouse can walk

The numbers 1, 2, 3 and 4 indicate the sequence of four directions the computer mouse must follow to find a path in the maze.


Fig. 1: The rule of the horse step

The size of given maze is 9 by 9, and its coordinates are shown in Fig. 2. Initially, you are given the starting and ending points of a maze. Your program must print the walking path of the mouse if the ending point of the maze can be reached and the number of steps is less than or equal to 50000. If the number of steps is over 50000 print `more than 50000 steps'. Otherwise, ``fail'' should be printed.


Fig. 2: The coordinates of the maze

Input 

The first line of the input is an integer N, then a blank line followed by N datasets. There is a blank line between datasets.

Each dataset consists of the coordinate of the starting point followed by the coordinate of the ending point in a maze.

Output 

For each dataset, output the coordinates of the walking path from the starting point to the ending point or `` fail ''. Print a blank line between datasets.

Sample Input 

1

(1,1)
(9,9)

Sample Output 

(1,1), (2,2), (1,3), (2,4), (1,5), (2,6), (1,7), (2,8), (1,9), (2,8), (3,9), (4,8), (5,9), (6,8), (7,9), (8,8), (9,9)


这题注意一点,由于走的是对角线,奇数点只能到奇数点,偶数点只能到偶数点,奇偶为横纵坐标相加的值

可以这样判(x1+y1-x2-y2)%2==0这样就可以,否则不行,dfs的时候步数大于50000直接退出就好了

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
// #include <windows.h>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
// #pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 20
struct Node
{
    int x, y;
    Node() {}
    Node (int a, int b)
    {
        x = a;
        y = b;
    }
};
Node path[50005];
int pathnum;
int sx, sy, ex, ey;
int dir[4][2] = { -1, 1, 1, 1, 1, -1, -1, -1};
bool vis[N][N];
bool flag;
void dfs(int x, int y)
{
    int nowx, nowy;
    if ( flag || pathnum > 50000)
    {
        return;
    }
    if (x == ex && y == ey)
    {
        flag = true;
        return;
    }
    for (int i = 0; i < 4; i++)
    {
        nowx = x + dir[i][0];
        nowy = y + dir[i][1];
        if (nowx < 1 || nowy < 1 || nowx > 9 || nowy > 9 || vis[nowx][nowy])
            continue;
        path[pathnum++] = Node(nowx, nowy);
        vis[x][y]=1;
        dfs(nowx, nowy);
        vis[x][y] = 0;
        if (  flag || pathnum > 50000)
            return;
        path[pathnum++] = Node(x, y);
    }

}
int cnt = 1;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        if (cnt++ != 1)
            printf("\n");
        char s[100];
        char e[100];
        scanf("%s%s", s, e);
        sx = s[1] - '0';
        sy = s[3] - '0';
        ex = e[1] - '0';
        ey = e[3] - '0';
        if ((ex + ey - sx - sy) % 2 != 0)
        {
            printf("fail\n");
            continue;
        }
        Node node(sx, sy);
        pathnum = 0;
        flag = false;
        memset(vis, 0, sizeof(vis));
        dfs(sx, sy);
        if (flag)
        {
            printf("(%d,%d)", sx, sy);
            for (int i = 0; i < pathnum; i++)
            {
                printf(", (%d,%d)", path[i].x, path[i].y);
            }
            printf("\n");
        }
        else if (pathnum > 50000)
        {
            printf("more than 50000 steps\n");
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值