zoukankan      html  css  js  c++  java
  • codeforce908B

    B. New Year and Buggy Bot
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

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

    题意:一张图,给定起点终点,给一操控字符串仅含有3,2,1,0,
    3,2,1,0可以映射成UP,DOWN,LEFT,RIGHT,问有多少映射方案可以走到终点。

    分析:可以用next_permutation()函数列出不同方案啊!!!!!
    自己好笨。。。竟然写了个dfs暴力。


     1 #include<cstdio>
     2 #include<cstring>
     3 char map[53][53];
     4 char s[200];
     5 struct Node{
     6     int x,y;
     7 }a[25][4],d[4];
     8 
     9 int vis[4],T,p[4];
    10 void dfs(int num)
    11 {
    12     if(num==4)
    13     {
    14         a[T][0]=d[p[0]];a[T][1]=d[p[1]];
    15         a[T][2]=d[p[2]];a[T][3]=d[p[3]];
    16         T++;
    17         return;
    18     }
    19     for(int i=0;i<4;i++)
    20     {
    21         if(vis[i]==0)
    22         {
    23             p[num]=i;vis[i]=1;
    24             dfs(num+1);
    25             vis[i]=0;
    26         }
    27     }
    28 }
    29 
    30 int main()
    31 {
    32     d[0].x=1;d[0].y=0;d[1].x=0;d[1].y=1;
    33     d[2].x=-1;d[2].y=0;d[3].x=0;d[3].y=-1;
    34     dfs(0);
    35     int N,M,Sx,Sy,Ex,Ey;
    36     scanf("%d%d",&N,&M);
    37     for(int i=0;i<N;i++)
    38         scanf("%s",map[i]);
    39     for(int i=0;i<N;i++)
    40     {
    41         for(int j=0;j<M;j++)
    42         {
    43             if(map[i][j]=='S') {Sx=i;Sy=j;}
    44             if(map[i][j]=='E') {Ex=i;Ey=j;}
    45         }
    46     }
    47     scanf("%s",s);
    48     int ans=0;
    49     for(int i=0;i<24;i++)
    50     {
    51         int j=0,flag=0,tx=Sx,ty=Sy;
    52         while(s[j])
    53         {
    54             tx+=a[i][s[j]-'0'].x;
    55             ty+=a[i][s[j]-'0'].y;
    56             if(tx<0||ty<0||tx>=N||ty>=M||map[tx][ty]=='#') break;
    57             if(tx==Ex&&ty==Ey) {flag=1;break;}
    58             j++;
    59         }
    60         if(flag)ans++;
    61     }
    62     printf("%d
    ",ans);
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    10分钟学会SpringBoot入门
    单链表常见的4道笔试题(Java版)
    Java面试、跳槽必刷200+真面试题,让你披荆斩棘走进大厂
    金三银四JAVA面试总结:Java+并发+Spring+MySQL+分布式+Redis+算法+JVM等
    最新整理的spring面试题从基础到高级,干货满满
    面试阿里百分百问的Jvm,别问有没有必要学,真的很有必要朋友
    面试官:你们前后端分离的接口规范是什么?
    “金九银十”已过,总结我的天猫、蚂蚁、头条面试经历(Java岗)
    350道面试题分享,拿下京东offer工资double
    2019大厂Java岗面试题全曝光,刷完这1020道,金三银四大厂等你
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8179674.html
Copyright © 2011-2022 走看看