zoukankan      html  css  js  c++  java
  • 牛客寒假算法基础集训营4 Applese 走迷宫(bfs)

    Applese 走迷宫

    链接:https://ac.nowcoder.com/acm/contest/330/C

    题目描述


    精通程序设计的 Applese 双写了一个游戏。

    在这个游戏中,它被困在了一个 n×mn×m 的迷宫中,它想要逃出这个迷宫。

    在迷宫中,有一些方格是水池,只有当 Applese 处于水属性的时候才可以通过;有一些方格是岩浆,只有当 Applese 是火属性的时候可以通过;有一些方格是墙壁,无论如何都无法通过;另一些格子是空地(包括起点和终点),可以自由通过。

    在一些空地上有神秘道具可以让 Applese 转换自己的属性(从水属性变为火属性或从火属性变为水属性,需要一个单位的时间)。

    已知 Applese 在一个单位的时间内可以朝四个方向行走一格,且开始处于水属性,位于空地的道具拾取后只能在该处立即使用(或者不使用),且可以多次使用。求它走出迷宫需要的最少时间。

    输入描述:

    第一行两个正整数 n, m 表示迷宫的大小。
    接下来 n 行,每行长度为 m 的字符串。描述地图。
    其中 'S' 表示起点,'T' 表示终点,'.' 表示空地,'w'表示岩浆,'~'表示水池,'@' 表示道具,'#'表示障碍。
    保证地图中的起点和终点只有一个,道具都位于空地。

    输出描述:

    输出一个整数,表示 Applese 走出迷宫的最短时间。特别地,如果 Applese 走不出迷宫,输出 "-1"。
    示例1

    输入

    5 5
    .w@..
    .S#..
    ~w#..
    .w..~
    @w.~T

    输出

    18

    备注:

    1n,m100
    正解:


     1 import java.util.ArrayDeque;
     2 import java.util.Scanner;
     3  
     4 class node{
     5     int sta;
     6     int x;
     7     int y;
     8     int step;
     9 }
    10 public class Main {
    11     static final int maxn = 105;
    12     static int [][][] vis = new int[2][105][105];
    13     static char [][] mp = new char [105][105];
    14     static String [] s = new String [105];
    15     static int [][] dir = {{0,1},{1,0},{-1,0},{0,-1}};
    16     static ArrayDeque<node> q = new ArrayDeque<node>();
    17     static int n,m;
    18     static node head,tail;
    19     static void bfs() {
    20         while(!q.isEmpty()) {
    21             head = new node();
    22             head = q.poll();
    23             if(mp[head.x][head.y]=='T') {
    24                 System.out.println(head.step);
    25                 return;
    26             }
    27             for(int i=0;i<4;i++) {
    28                 int tx = head.x+dir[i][0];
    29                 int ty = head.y+dir[i][1];
    30                 int tsta = head.sta;
    31                 int step = head.step + 1;
    32                 if(tx<0||tx>=n||ty<0||ty>=m||vis[tsta][tx][ty]==1||mp[tx][ty]=='#')
    33                     continue;
    34                 if(tsta==0&&mp[tx][ty]=='w')
    35                     continue;
    36                 if(tsta==1&&mp[tx][ty]=='~')
    37                     continue;
    38                     vis[tsta][tx][ty] = 1;
    39                     tail = new node();
    40                     tail.x = tx;
    41                     tail.y = ty;
    42                     tail.sta = tsta;
    43                     tail.step = step;
    44                     q.offer(tail);
    45             }
    46             if(mp[head.x][head.y]=='@') {
    47                 if(vis[head.sta^1][head.x][head.y]==1)
    48                     continue;
    49                 vis[head.sta^1][head.x][head.y]=1;
    50                 tail = new node();
    51                 tail.x = head.x;
    52                 tail.y = head.y;
    53                 tail.step = head.step + 1;
    54                 tail.sta = head.sta^1;
    55                 q.offer(tail);
    56             }
    57         }
    58         System.out.println("-1");
    59     }
    60     public static void main(String[] args) {
    61         Scanner cin = new Scanner(System.in);
    62         n = cin.nextInt();
    63         m = cin.nextInt();
    64         for(int i=0;i<n;i++)
    65             s[i] = cin.next();
    66         for(int i=0;i<n;i++) {
    67             for(int j=0;j<m;j++) {
    68                 mp[i][j] = s[i].charAt(j);
    69                 if(mp[i][j] == 'S') {
    70                     head = new node();
    71                     head.sta = 0;
    72                     head.x = i;
    73                     head.y = j;
    74                     head.step = 0;
    75                     vis[head.sta][head.x][head.y] = 1;
    76                     q.offer(head);
    77                 }
    78             }
    79         }
    80         bfs();
    81     }
    82 }

    下面写法不知道为什么错。。。

     1 import java.util.ArrayDeque;
     2 import java.util.Scanner;
     3  
     4 class node{
     5     int sta;
     6     int x;
     7     int y;
     8     int step;
     9 }
    10 public class Main {
    11     static final int maxn = 105;
    12     static int [][][] vis = new int[2][105][105];
    13     static char [][] mp = new char [105][105];
    14     static String [] s = new String [105];
    15     static int [][] dir = {{0,1},{1,0},{-1,0},{0,-1}};
    16     static ArrayDeque<node> q = new ArrayDeque<node>();
    17     static int n,m;
    18     static node head,tail;
    19     static void bfs() {
    20         while(!q.isEmpty()) {
    21             head = new node();
    22             head = q.poll();
    23             if(mp[head.x][head.y]=='T') {
    24                 System.out.println(head.step);
    25                 return;
    26             }
    27             for(int i=0;i<4;i++) {
    28                 int tx = head.x+dir[i][0];
    29                 int ty = head.y+dir[i][1];
    30                 int tsta = head.sta;
    31                 int step = head.step + 1;
    32                 if(tx<0||tx>=n||ty<0||ty>=m||vis[tsta][tx][ty]==1||mp[tx][ty]=='#')
    33                     continue;
    34                 if(tsta==0&&mp[tx][ty]=='w')
    35                     continue;
    36                 if(tsta==1&&mp[tx][ty]=='~')
    37                     continue;
    38                 if(mp[tx][ty]=='@') {
    39                     if(vis[head.sta^1][tx][ty]==0) {
    40                         vis[head.sta^1][tx][ty]=1;
    41                         tail = new node();
    42                         tail.x = tx;
    43                         tail.y = ty;
    44                         tail.step = head.step + 2;
    45                         tail.sta = head.sta^1;
    46                         q.offer(tail); 
    47                     }
    48                      
    49                     vis[head.sta][tx][ty]=1;
    50                     tail = new node();
    51                     tail.x = tx;
    52                     tail.y = ty;
    53                     tail.step = head.step + 1;
    54                     tail.sta = head.sta;
    55                     q.offer(tail);
    56                 }
    57                 else {
    58                     vis[tsta][tx][ty] = 1;
    59                     tail = new node();
    60                     tail.x = tx;
    61                     tail.y = ty;
    62                     tail.sta = tsta;
    63                     tail.step = step;
    64                     q.offer(tail);
    65                 }                  
    66             }
    67         }
    68         System.out.println("-1");
    69     }
    70     public static void main(String[] args) {
    71         Scanner cin = new Scanner(System.in);
    72         n = cin.nextInt();
    73         m = cin.nextInt();
    74         for(int i=0;i<n;i++)
    75             s[i] = cin.next();
    76         for(int i=0;i<n;i++) {
    77             for(int j=0;j<m;j++) {
    78                 mp[i][j] = s[i].charAt(j);
    79                 if(mp[i][j] == 'S') {
    80                     head = new node();
    81                     head.sta = 0;
    82                     head.x = i;
    83                     head.y = j;
    84                     head.step = 0;
    85                     vis[head.sta][head.x][head.y] = 1;
    86                     q.offer(head);
    87                 }
    88             }
    89         }
    90         bfs();
    91     }
    92 }
     
  • 相关阅读:
    .net core json 操作
    TypeScript 解构和展开
    asp.net core 学习路线
    NVM的安装和NPM下载速度慢的问题
    OzCode 最牛Visual Studio 调试工具
    破解EntityFramework Core
    动态WebAPI实现原理
    sqlyog 下载
    小程序生成二维码,海报
    .net core 拦截器的使用
  • 原文地址:https://www.cnblogs.com/1013star/p/10369914.html
Copyright © 2011-2022 走看看