zoukankan      html  css  js  c++  java
  • 蓝桥杯 穿越雷区 (Java广搜+字符串处理)

    蓝桥杯 穿越雷区

    X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
    某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?

    已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
    例如:
    A + - + -
    - + - - +
    - + + + -
    + - + - +
    B + - + -

    坦克车只能水平或垂直方向上移动到相邻的区。

    数据格式要求:

    输入第一行是一个整数n,表示方阵的大小, 4<=n<100
    接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
    A,B都只出现一次。

    要求输出一个整数,表示坦克从A区到B区的最少移动步数。
    如果没有方案,则输出-1

    例如:
    用户输入:
    5
    A + - + -
    - + - - +
    - + + + -
    + - + - +
    B + - + -

    则程序应该输出:
    10

    资源约定:
    峰值内存消耗(含虚拟机) < 512M
    CPU消耗  < 2000ms


    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
    注意:主类的名字必须是:Main,否则按无效代码处理。


    题解:这一题就是一个很中规中矩的广搜 个人觉得难点在于怎么处理给出的图中的空格。

    注意点:

    1、要是用cin.nextLine()那就全部输入都用,包括最开始输入的n。

    2、我们可以用 String s[] = cin.nextLine().split(" ") 将每一行的字符串中空格去掉,变成一个个没有空格的子字符串,保存在字符串数组s[]中。

    3、StringBuffer类可以在字符串后面任意添加字符串。

    4、node 用之前一定要重新new.

     1 import java.util.ArrayDeque;
     2 import java.util.Scanner;
     3 class node{
     4     int x;
     5     int y;
     6     int step;
     7     int flag;
     8 }
     9 public class Main {
    10     static int [][] vis = new int [110][110];
    11     static int sx,sy,ex,ey,n,ans = Integer.MAX_VALUE;
    12     static char [][] mp = new char[110][110];
    13     static int dir[][] = {{0,1},{1,0},{-1,0},{0,-1}};
    14     static node head,tail;
    15     static ArrayDeque<node> q = new ArrayDeque<node>();
    16     static void bfs() {
    17         q.offer(head);
    18         while(!q.isEmpty()) {
    19             node head = new node();//必须写
    20             head = q.poll();
    21             if(head.x==ex&&head.y==ey) {
    22                 ans = Math.min(ans, head.step);
    23                 return;
    24             }
    25             for(int i=0;i<4;i++) {
    26                 int tx = head.x + dir[i][0];
    27                 int ty = head.y + dir[i][1];
    28                 if(tx<0||tx>=n||ty<0||ty>=n||vis[tx][ty]==1)
    29                     continue;
    30                 if(head.flag==-1) {
    31                     if(mp[tx][ty]=='-')
    32                         continue;
    33                 }
    34                 if(head.flag==1)
    35                     if(mp[tx][ty]=='+')
    36                         continue;
    37                 vis[tx][ty] = 1;
    38                 tail = new node();//必写
    39                 tail.x = tx;
    40                 tail.y = ty;
    41                 tail.flag = -head.flag;
    42                 tail.step = head.step + 1;
    43                 q.offer(tail);
    44             }
    45         }
    46     }
    47     public static void main(String[] args) {
    48         Scanner cin = new Scanner(System.in);
    49         n = Integer.parseInt(cin.nextLine());
    50         for(int i=0;i<n;i++) {
    51             String s[] = cin.nextLine().split(" ");
    52             StringBuffer c = new StringBuffer();
    53             for(int j=0;j<s.length;j++)
    54                 c.append(s[j]);
    55             for(int j=0;j<c.length();j++)
    56                 mp[i][j] = c.charAt(j);
    57          }
    58         for(int i=0;i<n;i++) {
    59             for(int j=0;j<n;j++) {
    60                 if(mp[i][j]=='A') {
    61                     sx = i;
    62                     sy = j;
    63                 }
    64                 if(mp[i][j]=='B') {
    65                     ex = i;
    66                     ey = j;
    67                 }
    68             }
    69         }
    70         head = new node();
    71         head.x = sx;
    72         head.y = sy;
    73         head.flag = -1;
    74         head.step = 0;
    75         bfs();
    76         head.x = sx;
    77         head.y = sy;
    78         head.flag = 1;
    79         head.step = 0;
    80         bfs();
    81         if(ans!=Integer.MAX_VALUE)
    82             System.out.println(ans);
    83         else
    84             System.out.println(-1);
    85     }
    86 }
  • 相关阅读:
    MVC ORM 架构
    Kubernetes 第八章 Pod 控制器
    Kubernetes 第七章 Configure Liveness and Readiness Probes
    Kubernetes 第六章 pod 资源对象
    Kubernetes 第五章 YAML
    Kubernetes 核心组件
    Kubernetes 架构原理
    Kubernetes 第四章 kubectl
    Kubernetes 第三章 kubeadm
    yum 配置及yum 源配置
  • 原文地址:https://www.cnblogs.com/1013star/p/10734170.html
Copyright © 2011-2022 走看看