Java實現 計蒜客 1251 仙島求藥

仙島求藥

少年李逍遙的嬸嬸病了,王小虎介紹他去一趟仙靈島,向仙女姐姐要仙丹救嬸嬸。叛逆但孝順的李逍遙闖進了仙靈島,克服了千險萬難來到島的中心,發現仙藥擺在了迷陣的深處。迷陣由 M \times NM×N 個方格組成,有的方格內有可以瞬秒李逍遙的怪物,而有的方格內則是安全。現在李逍遙想盡快找到仙藥,顯然他應避開有怪物的方格,並經過最少的方格,而且那裏會有神祕人物等待着他。現在要求你來幫助他實現這個目標。

輸入格式
第一行輸入兩個非零整數 MM 和 NN,兩者均不大於 2020。MM 表示迷陣行數, NN 表示迷陣列數。

接下來有 MM 行, 每行包含 NN 個字符,不同字符分別代表不同含義:

  1. ‘@’:少年李逍遙所在的位置;2) ‘.’:可以安全通行的方格;3) ‘#’:有怪物的方格;4) ‘*’:仙藥所在位置。

輸出格式
輸出一行,該行包含李逍遙找到仙藥需要穿過的最少的方格數目(計數包括初始位置的方塊)。如果他不可能找到仙藥, 則輸出 -1−1。

輸出時每行末尾的多餘空格,不影響答案正確性

樣例輸入1

8 8
.@##...#
#....#.#
#.#.##..
..#.###.
#.#...#.
..###.#.
...#.*..
.#...###

樣例輸出1
10
樣例輸入2

6 5
.*.#.
.#...
..##.
.....
.#...
....@

樣例輸出2
8
樣例輸入3

9 6
.#..#. 
.#.*.# 
.####. 
..#... 
..#... 
..#... 
..#... 
#.@.## 
.#..#.

樣例輸出3
-1

 

import java.util.LinkedList;
import java.util.Scanner;

public class 仙島求藥 {


 static int N = 3000;//N需開大點
    static   int r,c;
  static   char[] []  map = new char[N][N];//用於保存地圖
    static boolean  [] [] vis = new  boolean[N][N];//標識是否訪問過的點
    static  int [] []dir = {{-1,0},{1,0},{0,-1},{0,1}}; //四個方向座標
  static   class node{
        int x;
        int y;
        int step;
    }
    static  node []  q = new node[N];
public static     void bfs(int sx,int sy,int ex,int ey){
        int head=1,tail=1;
        boolean flag1 =true;
        vis[sx][sy]=true;
        node n = new node();
        n.x=sx;
        n.y=sy;
        n.step=0;
    q[tail]=n;
        tail++;
        while(head<tail){
            int x = q[head].x;
            int y = q[head].y;
            int step = q[head].step;
            if(x==ex&&y==ey) //終點則輸出
            {
                flag1=false;
                System.out.printf("%d\n",step);
                break;
            }
            for(int i=0;i<4;i++){
                int nx = x+ dir[i][0];
                int ny = y +dir[i][1];
                if(nx >= 0 && nx < r && ny >= 0 && ny < c && vis[nx][ny] == false && map[nx][ny] == '.'){
                    vis[nx][ny] = true;
                    node nn;
                    if (q[tail]==null){
                         nn =new node();
                    }
                    else {
                          nn = q[tail];
                    }

                    nn.x = nx;
                    nn.y = ny;
                    nn.step = step+1;
                    q[tail]=nn;
                    tail++;
                }
            }
            head++;
        }
        if(flag1)
            System.out.printf("-1\n");
    }

    public static void main(String[] args) {
        int sx = 0,sy=0,ex=0,ey=0;
      //  while(scanf("%d%d",&r,&c) != EOF && (r||c))
        Scanner sc = new Scanner(System.in);
          r = sc.nextInt();
          c = sc.nextInt();
        for (int k=0;k<r;k++){

String s = sc.next();
map[k]=s.toCharArray();
        }
            for(int i=0;i<r;i++)
                for(int j=0;j<c;j++)
                {
                    if(map[i][j]=='@'){
                        sx=i;
                        sy=j;
                    }
                    if(map[i][j]=='*'){
                        ex=i;
                        ey=j;
                        map[i][j]='.';//修改爲 點
                    }
                }
            bfs(sx,sy,ex,ey);

    }
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章