竞赛讨论区 > 人间迷惑,哪里错了????
头像
Ζεύς
发布于 2020-06-20 17:10
+ 关注

人间迷惑,哪里错了????

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main
{

    public static boolean ok = false;
    public static int move[][] =
    {
            { 1, 0 },
            { 0, 1 },
            { -1, 0 },
            { 0, -1 } };

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        ok=false;
        while (sc.hasNext())
        {
            int q, p;
            q = sc.nextInt();
            p = sc.nextInt();
            char xy[][] = new char[q][p];

    
            int x = 0, y = 0;
            int x1 = 0;
            int y1 = 0;
            ok = false;
            for (int i = 0; i < xy.length; i++)
            {

                char g[] = sc.next().toCharArray();
                for (int j = 0; j < g.length; j++)
                {
                    xy[i][j]=g[j];
                    
                }
                for (int j = 0; j < g.length; j++)
                {
                    if (g[j] == 's')
                    {
                        x = i;
                        y = j;
                    }
                    if (g[j] == 'g')
                    {
                        x1 = i;
                        y1 = j;
                    }
                }
                xy[i] = g;
            }
            xy[x][y] = '#';
    
            find(x, y, x1, y1, xy);

            if (ok == true)
            {
                System.out.println("YES");
            } else
            {
                System.out.println("NO");
            }

        }

    }

    public static void find(int xt, int yt, int x1, int y1, char xy[][])
    {

        if (ok == true)
        {
            return;
        }

        if (xt == x1 && yt == y1)
        {
            ok = true;
            return;
        }
        for (int i = 0; i < 4; i++)
        {

            int xx = xt + move[i][0];
            int yy = yt + move[i][1];
            if (xx < 0 || xx > xy.length - 1 || yy < 0 || yy > xy[0].length - 1 ||xy[xx][yy]=='#')
            {
                
                continue;
            }

    xy[xx][yy] = '#';
    find(xx, yy, x1, y1, xy);
    xy[xx][yy] = '.';

    

        }
        return;
    }

}

全部评论

(0) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐