zoukankan      html  css  js  c++  java
  • New Year and Buggy Bot

    Bob programmed a robot to navigate through a 2d maze.

    The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.

    There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.

    The robot can only move up, left, right, or down.

    When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.

    The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.

    Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.

    Input

    The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze.

    The next n lines will contain exactly m characters each, denoting the maze.

    Each character of the maze will be '.', '#', 'S', or 'E'.

    There will be exactly one 'S' and exactly one 'E' in the maze.

    The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.

    Output

    Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.

    Example
    Input
    5 6
    .....#
    S....#
    .#....
    .#....
    ...E..
    333300012
    Output
    1
    Input
    6 6
    ......
    ......
    ..SE..
    ......
    ......
    ......
    01232123212302123021
    Output
    14
    Input
    5 3
    ...
    .S.
    ###
    .E.
    ...
    3
    Output
    0
    Note

    For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.

    只要安全到达出口就可以了,可以有多余的指令。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    int n,m,dir[4][2] = {0,1,1,0,0,-1,-1,0},c = 0,visited[4],f[4],sx,sy;
    char mp[51][51],s[101];
    int check()
    {
        int px = sx,py = sy;
        for(int i = 0;s[i];i ++)
        {
            px += dir[f[s[i] - '0']][0],py += dir[f[s[i] - '0']][1];
            if(px < 0 || py < 0 || px >= n || py >= m || mp[px][py] == '#')return 0;
            if(mp[px][py] == 'E')return 1;
        }
        return 0;
    }
    void dfs(int k)
    {
        if(k == 4)
        {
            if(check())c ++;
            return;
        }
        for(int i = 0;i < 4;i ++)
        {
            if(!visited[i])
            {
                visited[i] = 1;
                f[k] = i;
                dfs(k + 1);
                visited[i] = 0;
            }
        }
    }
    int main()
    {
        cin>>n>>m;
        for(int i = 0;i < n;i ++)
        {
            for(int j = 0;j < m;j ++)
            {
                cin>>mp[i][j];
                if(mp[i][j] == 'S')sx = i,sy = j;
            }
        }
        cin>>s;
        dfs(0);
        cout<<c;
    }
  • 相关阅读:
    java Semaphore的介绍和使用
    java CyclicBarrier的介绍和使用
    java CountDownLatch 使用介绍
    android模拟器不能上网设置
    计算几何题集
    BZOJ1004: [HNOI2008]Cards
    BZOJ1029: [JSOI2007]建筑抢修
    BZOJ1037: [ZJOI2008]生日聚会Party
    BZOJ1083: [SCOI2005]繁忙的都市
    Java开发笔记(一百一十四)利用Socket传输文本消息
  • 原文地址:https://www.cnblogs.com/8023spz/p/8364809.html
Copyright © 2011-2022 走看看