Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java机器人拯救公主挑战_Java_Challenge Response - Fatal编程技术网

Java机器人拯救公主挑战

Java机器人拯救公主挑战,java,challenge-response,Java,Challenge Response,桃子公主被困在正方形网格的四个角之一。您位于网格的中心,可以在四个方向中的任何一个方向上一次移动一步。你能救公主吗 输入格式 第一行包含一个奇数整数N(30){ 博科--; System.out.println(“左\n”); } } else if(网格[0][p-1]。等于('p')){ while(botRow>0){ System.out.println(“UP\n”); 博特罗--; } while(botCol接受输入时,每次退出while循环时,必须使j=0 public stat

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

输入格式

第一行包含一个奇数整数N(30){ 博科--; System.out.println(“左\n”); } } else if(网格[0][p-1]。等于('p')){ while(botRow>0){ System.out.println(“UP\n”); 博特罗--; }
while(botCol接受输入时,每次退出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++;
       }     
    }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int m,n;
m=in.nextInt();
n=m;
int j=0;
字符串网格[][]=新字符串[m][n];
for(int i=0;i虽然(jOk,没有冒犯的意思,但是这个代码太乱了

我知道我将要回答的并不完全是你想要的,但它可能对你将来非常有帮助

我会改变什么?(在你改变所有这些之后,错误很可能会变得很明显,或者会很清楚地要求帮助)

第一:变量类型。 这只是一个很小的细节,但我不喜欢您这样做;如果每个单元格都由字符表示,为什么要使用字符串

每次创建变量(或数组,或任何东西)时,都要考虑需要它存储什么,并以某种方式创建变量,它将存储您需要的内容。如果需要它存储某个内容是否为真,则不会创建字符串变量并存储“真”或“假”,而是使用布尔值

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

第二:使用函数。 函数是从实现细节中抽象自己的好方法

我的意思是,您可以很容易地看到代码和以下内容之间的区别:

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开始,而jwhile,而是称为for(另外,正如其他人在他们的回答中所评论的,你忘了重新启动j;因此它只进入一次

最后:让我们来谈谈你的错误。 我相信您的代码可能有很多错误,无法提供所需的输出,但是NullPointerException是更具体的东西

因为没有在main中使用适当的循环,对于j,并且忘记重新启动它的值,所以没有填充整个数组

当您尝试读取未填充的值(在查找机器人位置的字段中)时,该值为null,某些行的值也将为null;因此出现NullPointerException(因为您尝试访问null数组的值)。

解决方案如下

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.util.*;
公共类解决方案{
静态void displaytophrincess(int n,int p,String[][]网格){
int botRow=0,botCol=0;
对于(int r=0;r0){
博特罗--;
System.out.println(“UP\n”);
}
while(botCol>0){
博科--;
System.out.println(“左\n”);
}
}else if(网格[0][p-1].equalsIgnoreCase(“p”)){
while(botRow>0){
博特罗--;
System.out.println(“UP\n”);
}
while(botCol0){
博科--;
System.out.println(“左\n”);
}
}else if(网格[n-1][p-1].equalsIgnoreCase(“p”)){
while(botRow
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.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共类解决方案
{
公共静态void main(字符串[]args)引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int m=Integer.parseInt(br.readLine());
字符网格[][]=新字符[m][m];
for(int i=0;iimport 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");
            }
        }

    }
}
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);

  }
}