简体   繁体   中英

Iterating through an array and replacing characters

I have an array that contains dashes and dots. The goal is to fire a "laser" into this array at a certain location that the user decides. In this case row 5 was where they shot. I need to now iterate through that row and change all the dots to dashes ( - ) or ( | ) depending on if its going east to west or north to south. When it hits a dash, it is supposed to act as a mirror and reflect off and continue in another direction. Can anyone help with the loop to do this? I keep putting the dashes in the wrong place. Any help is appreciated.

0 . . . . . / . . . \ 
1 . . \ . . . . . . . 
2 . . . . . . . / . . 
3 . . . . . . . . . . 
4 . . . . . . . . \ . 
5>. . . . . . . . . . 
6 . . . . . . . . . . 
7 . . . . . \ . . / . 
8 . . \ . . . . . . / 
9 . . . . . . . . . . 

Possible final

0 . . . . . / - - - \ 
1 - - \ . . | . . . |
2 . . | . . | . / . | 
3 . . | . . | . . . | 
4>- - - - - - - - \ | 
5 . . | . . | . . | |
6 . . | . . | . . | | 
7 . . | . . \ - - / | 
8 . . \ - - - - - - / 
9 . . . . . . . . . . 

Basically this could be the end of the grid after the laser is shot through row 4

You could do it using an infinite loop that breaks once your laser stops for whatever reason. Using variables for direction, your position and your next position, you could make code to check the next position in the direction you're going, then going in that direction until you either hit a / or \\, or are about to go out of bounds. The code would be something to this effect (separated x and y values just for clarity):

char lasermap[][] = /* your laser grid */
int x_direction = 0; // UP = 1, NONE = 0, DOWN = -1
int y_direction = 0; // RIGHT = 1 NONE = 0, LEFT = -1
int x = 0;
int y = 0;
int nextx = 0;
int nexty = 0;
for (;;) {
    nextx = x + x_direction;
    nexty = y + y_direction;
    if (nextx < 0 || nexty < 0) {
        break;
    } else if (lasermap[nextx][nexty] == '\\') {
        if (x_direction == -1) {
            x_direction = 0;
            y_direction = 1;
        } else if (x_direction == 1) {
            x_direction = 0;
            y_direction = -1;
        } else if (y_direction == -1) {
            x_direction = 1;
            y_direction = 0;
        }
        else if (y_direction == 1) {
            x_direction = -1;
            y_direction = 0;
        }
    } else if (lasermap[nextx][nexty] == '/') {
        if (x_direction == -1) {
            x_direction = 0;
            y_direction = -1;
        } else if (x_direction == 1) {
            x_direction = 0;
            y_direction = 1;
        } else if (y_direction == -1) {
            x_direction = -1;
            y_direction = 0;
        } else if (y_direction == 1) {
            x_direction = 1;
            y_direction = 0;
        }
    }
    x = nextx;
    y = nexty;
}
import java.util.*;
class Try
{
    public static void main(String args[])
    {
        int n;
        Scanner s = new Scanner(System.in); // input 
        n = s.nextInt();
        String s1[] = new String[n+1]; // input strings
        String s2[] = new String[n+1]; // output strings
        for(int i = 0;i<n + 1;i++)
        {
            s1[i] = new String();
            s1[i] = s.nextLine();
        }

        for(int i = 0;i<n + 1;i++)
        {
            char[] temp = s1[i].toCharArray(); // getting indivdual characters
            for(int j = 0;j<temp.length;j++)  
            {
                if(temp[j] == '>')   // if laser found
                {
                    for(int k = j;k < temp.length && temp[k] != '\\'&& temp[k] != '/';k++)
                    {
                        temp[k] = '-';   // getting dash
                    }
                }

                if(temp[j] == '<')
                {
                    for(int k = j;k >= 0 && temp[k] != '\\'&& temp[k] != '/';k--)
                    {
                        temp[k] = '|'; // getting pipes
                    }
                }
            }
            s2[i] = new String(temp);
        }

        for(int i = 0;i<n+1;i++)
        {
            System.out.println(s2[i]); // printing result
        }
    }
}

this will possible help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM