Java 机器人拯救公主挑战赛

Java Bot saves Princess Challenge

桃子公主被困在方形网格的四个角之一中。您位于网格的中心,可以在四个方向中的任何一个方向上一次移动一步。你能救出公主吗?

输入格式

第一行包含一个奇数N(3 <= N < 100),表示网格的大小。接下来是 NxN 网格。每个单元格都用“-”表示(ascii 值:45)。机器人位置由 'm' 表示,公主位置由 'p' 表示。

网格使用矩阵约定编制索引

输出格式

一次性打印出拯救公主的动作。这些移动必须由换行符“\n”分隔。有效的移动是左或右或上或下。

这是我的代码:

package challenges;

import java.util.*;

public class Solution {
static void displayPathtoPrincess(int n, int p,String [][] grid){
int botRow=0,botCol=0;

for(int r=0;r<n;r++){
   for (int c = 0; c < grid.length; c++) {
        if(grid[r][c].equals('m')) {
            botRow=r;
            botCol=c;
            continue;
        }
   }
        if(grid[0][0].equals('P')) {
            while(botRow>0) {
                botRow--;
                System.out.println("UP\n");
            }
            while(botCol>0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        }
        else if(grid[0][p-1].equals('P')) {
            while(botRow>0) {
                System.out.println("UP\n");
                botRow--;
            }
            while(botCol<p-1) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }
        else if(grid[n-1][0].equals('P')) {
            while(botRow<n-1) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while(botCol>0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        }
        else if(grid[n-1][p-1].equals('P')) {
            while(botRow<n-1) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while(botCol<p-1) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }   
   }
}
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();
    n=m;
    int j=0;
    String grid[][] = new String[m][n];
    for(int i = 0; i < m; i++) {
       while(j<n){
            grid[i][j] = in.next();             
        j++;
       }     
    }

displayPathtoPrincess(m,n,grid);

}
}

它给出了空指针Exception.can任何人请告诉我我在这里做错了什么?

在接受输入时,每次退出 while 循环时都必须使 j =0,因为

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();
    n=m;
    int j=0;
    String grid[][] = new String[m][n];
    for(int i = 0; i < m; i++) {
       j = 0;
       while(j<n){
            grid[i][j] = in.next();             
        j++;
       }     
    }

由于这个错误,我仍然是 0。 你应该写打印语句来检查你为什么会出错。

好的,没有冒犯,但这段代码一团糟。

我知道我回答的不一定是你想要的,但可能对你以后有很大帮助。

我会改变什么? (在您更改所有这些之后,错误将更有可能变得明显或清楚到可以寻求帮助)

首先:变量类型。

这只是一个小细节,但我不喜欢你做的方式;如果每个单元格都由一个字符表示,为什么要使用字符串?

每次您创建一个变量(或一个数组,或任何其他东西)时,请考虑您需要它存储什么,并以一种它将存储您需要的内容的方式创建变量。如果您需要它来存储 truefalse,则不会创建字符串变量并存储 "true ""false",您将使用布尔值。

每次都应用这个,你会进步得更快。

第二:使用函数。

函数是从实现细节中抽象出来的好方法。

我的意思是,您可以轻松看出您的代码与以下内容之间的区别:

static void displayPathtoPrincess(int n, int p,char [][] grid){

    Point bot;
    Point princess;

    getInitialPositions(bot, princess, grid);

    Point dif = getRelativePrincessPosition(bot, princess);

    while (!bot.equals(princess)) {
        switch (dif.y) {
            case UP:
                move (UP_MOVEMENT);
                break;
            case DOWN:
                move (DOWN_MOVEMENT);
                break;
        }
        switch (dif.x) {
            case LEFT:
                move(LEFT_MOVEMENT);
                break;
            case RIGHT:
                move(RIGHT_MOVEMENT);
                break;
        }
    }
}

(然后实现必要的方法,并创建常量,这很容易做到)

第三:每次都使用合适的循环。

如果你知道你想从 j = 0 开始,同时 j < n,每次迭代增加 j;那不叫 while,叫 for。 (此外,正如其他人在他们的回答中评论的那样,您忘记了 重新启动 j;所以它只进入一次。

最后:让我们现在开始处理您的错误。

我认为您的代码可能有很多问题,无法为您提供所需的输出,但 NullPointerException 更具体。

因为你没有在 main 中使用适当的循环,对于 j,你忘记了 restart 它的价值,你没有填满整个数组。

当您尝试读取您未填写的值时(在您找到机器人位置的 for 中),该值为 null,并且某些行的值为也将是 null;因此 NullPointerException(因为您尝试访问空数组的值)。

这是解决方案..

import java.util.*;

public class Solution {
static void displayPathtoPrincess(int n, int p, String[][] grid) {
    int botRow = 0, botCol = 0;

    for (int r = 0; r < n; r++) {
        for (int c = 0; c < grid.length; c++) {
            if (grid[r][c].equalsIgnoreCase("m")) {
                botRow = r;
                botCol = c;
            }
        }
        if (grid[0][0].equalsIgnoreCase("P")) {
            while (botRow > 0) {
                botRow--;
                System.out.println("UP\n");
            }
            while (botCol > 0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        } else if (grid[0][p - 1].equalsIgnoreCase("P")) {
            while (botRow > 0) {
                botRow--;
                System.out.println("UP\n");
            }
            while (botCol < p - 2) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        } else if (grid[n - 1][0].equalsIgnoreCase("P")) {
            while (botRow < n - 2) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while (botCol > 0) {
                botCol--;
                System.out.println("LEFT\n");
            }
        } else if (grid[n - 1][p - 1].equalsIgnoreCase("P")) {
            while (botRow < n - 2) {
                botRow++;
                System.out.println("DOWN\n");
            }
            while (botCol < p - 2) {
                botCol++;
                System.out.println("RIGHT\n");
            }
        }
    }
}
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int m,n;
    m = in.nextInt();

    int j;
    String grid[][] = new String[m][m];
    for(int i = 0; i < m; i++) {

       for(j=0;j<m;j++){
            grid[i][j] = in.next();             

       }     
    }

displayPathtoPrincess(m,m,grid);

}
}

试试这个解决方案:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(br.readLine());

        char grid[][] = new char[m][m];
        for(int i = 0; i < m; i++)
        {
            String line = br.readLine();
            grid[i] = (line.trim()).toCharArray();
        }

        displayPathtoPrincess(m, grid);
    }

    static void displayPathtoPrincess(int m, char[][] grid)
    {
        int botRow = 0, botCol = 0, princessRow = 0, princessCol = 0;

        // Get bot and princess coordinates
        for (int r = 0; r < m; r++)
        {
            for (int c = 0; c < grid.length; c++)
            {
                if (grid[r][c] == 'm' || grid[r][c] == 'M')
                {
                    botRow = r;
                    botCol = c;
                }
                else if (grid[r][c] == 'p' || grid[r][c] == 'P')
                {
                    princessRow = r;
                    princessCol = c;
                }
            }
        }

        // Move the bot up or down till bot reaches same row
        if( princessRow < botRow )
        {
            while(botRow != princessRow)
            {
                botRow--;
                System.out.println("UP");
            }
        }
        else if( princessRow > botRow )
        {
            while(botRow != princessRow)
            {
                botRow++;
                System.out.println("DOWN");
            }
        }

        // Move the bot left or right till bot reaches same column
        if( princessCol < botCol )
        {
            while(botCol != princessCol)
            {
                botCol--;
                System.out.println("LEFT");
            }
        }
        else if( princessCol > botCol )
        {
            while(botCol != princessCol)
            {
                botCol++;
                System.out.println("RIGHT");
            }
        }

    }
}

您可能希望查看 Java 中的以下代码:

import java.util.Scanner;

public class Solution {

    /*
     * 
     */
    static void printPath(int n, String moves) {
        for (int i = n / 2; i >= 0; i -= 2) {
            System.out.println(moves);
        }           
    }
    
    /*
     * 
     */
    static void displayPathtoPrincess(int n, String [] grid){

        // **** princess @ top left  ****
        
        if (grid[0].substring(0, 1).equals("p")) {
            printPath(n, "UP\nLEFT");
        }
        
        // **** princess @ top right ****
        
        else if (grid[0].substring(n - 1, n).equals("p") ) {
            printPath(n, "UP\nRIGHT");
        }
        
        // **** princess @ bottom right ****
            
        else if (grid[n - 1].substring(n - 1, n).equals("p")) {
            printPath(n, "DOWN\nRIGHT");
        }
        
        // **** princess @ bottom left ****
        
        else {
            printPath(n, "DOWN\nLEFT");
        }
    }

    /*
     * Test code
     */
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        int m;
        m = in.nextInt();
        String grid[] = new String[m];
        
        for(int i = 0; i < m; i++) {
            grid[i] = in.next();
        }

        // **** ****
        
        in.close();
        
        // **** ****
        
        displayPathtoPrincess(m,grid);
    }
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static void nextMove(int n, int r, int c, String  [] grid){

int xcord=0;
int ycord=0;
for ( int i = 0 ; i <n-1; i++){
    String s = grid[i];
    xcord=i;
    for(int x =0; x<=n-1;x++){
        if(s.charAt(x)=='p'){
            ycord=x;
            
            if(xcord==r){
                if(ycord>c){System.out.println("RIGHT");return;}
                else {System.out.println("LEFT");return;}
                
            }else if(xcord<r){System.out.println("UP");return;}
           
            else{ System.out.println("DOWN");return;}
              
            
        }
        
        
    }
  

 }

 System.out.println(xcord);
System.out.println(ycord);
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n,r,c;
    n = in.nextInt();
    r = in.nextInt();
    c = in.nextInt();
    in.useDelimiter("\n");
    String grid[] = new String[n];


    for(int i = 0; i < n; i++) {
        grid[i] = in.next();
    }

  nextMove(n,r,c,grid);

  }
}