Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.io.IOException;
- public class Sneaking {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int lineNum = Integer.parseInt(reader.readLine());
- double NikolazPosition = 0;
- double SamPosition = 0;
- List<String> line = Arrays.stream(reader.readLine().split("")).collect(Collectors.toList());
- String[][] matrix = new String[lineNum][line.size()];
- int rowCount = 0;
- int colCount = line.size();
- lineNum--;
- while(lineNum-- >= 0){
- for (int col = 0; col < line.size(); col++) {
- matrix[rowCount][col] = line.get(col);
- if (line.get(col).equals("N")) {
- NikolazPosition = Double.parseDouble(rowCount + "." + col);
- }
- if (line.get(col).equals("S")) {
- SamPosition = Double.parseDouble(rowCount + "." + col);
- }
- }
- rowCount ++;
- line = Arrays.stream(reader.readLine().split("")).collect(Collectors.toList());
- }
- List<String> commands = line;
- while(commands.size()+1 > 0 ){
- if(SamPosition < 0){
- System.out.println("Sam died at " + (int)SamPosition*(-1) + ", " + (int)((SamPosition*(-10))%10));
- PrintMatrix(rowCount,colCount,matrix);
- return;
- }
- else if((int)SamPosition == (int)NikolazPosition){
- System.out.println("Nikoladze killed!");
- matrix[(int)(NikolazPosition)][(int)((NikolazPosition*10)%10)] = "X";
- PrintMatrix(rowCount,colCount,matrix);
- return;
- }
- else {
- String currCommand = commands.get(0);
- int rount = 0;
- MoveEnemy(rowCount, colCount, matrix);
- SamPosition = SamMove(colCount, currCommand, SamPosition, matrix);
- commands.remove(0);
- }
- }
- }
- private static void PrintMatrix(int lineCount, int colCount, String[][] newMatrix) {
- for (int row = 0; row < lineCount ; row++) {
- for (int col = 0; col < colCount; col++) {
- System.out.print(newMatrix[row][col]);
- }
- System.out.println();
- }
- }
- private static void MoveEnemy(int lineCount, int colCount, String[][] matrix) {
- for (int row = 0; row < lineCount; row++) {
- for (int col = 0; col < colCount; col++) {
- if( matrix[row][col].equals("b")){
- if(col+1 >= colCount){
- matrix[row][col]= "d";
- }
- else{
- matrix[row][col] = ".";
- matrix[row][col+1]= "b";
- }
- break;
- }
- else if( matrix[row][col].equals("d")){
- if(col-1 < 0){
- matrix[row][col] = "b";
- }
- else{
- matrix[row][col] = ".";
- matrix[row][col-1]= "d";
- }
- break;
- }
- }
- }
- }
- private static double SamMove( int colCount, String direction, double SamCurrPos, String[][] Matrix) {
- double CurrPos = 0;
- int xPos = (int)SamCurrPos;
- int yPos = (int)((SamCurrPos*10)%10);
- for(int col =0; col < colCount; col++){
- if(Matrix[xPos][col].equals("b")){
- if(col < yPos){
- Matrix[xPos][yPos] = "X";
- return Double.parseDouble("-" + xPos + "." + yPos);
- }
- }
- else if(Matrix[xPos][col].equals("d")) {
- if (col > yPos) {
- Matrix[xPos][yPos] = "X";
- return Double.parseDouble("-" + xPos + "." + yPos);
- }
- }
- }
- Matrix[xPos][yPos] = ".";
- switch(direction){
- case "U":
- for (int col = 0; col < colCount; col++) {
- if(Matrix[xPos-1][col].equals("b")){
- if(col < yPos){
- Matrix[xPos-1][yPos] = "X";
- CurrPos = Double.parseDouble("-" + (xPos-1) + "." + yPos);
- break;
- }
- else if(col == yPos){
- Matrix[xPos+1][yPos] = "S";
- break;
- }
- }
- else if(Matrix[xPos-1][col].equals("d")){
- if(col > yPos){
- Matrix[xPos-1][yPos] = "X";
- CurrPos = Double.parseDouble("-" + (xPos-1) + "." + yPos);
- break;
- }
- else if(col == yPos){
- Matrix[xPos-1][yPos] = "S";
- break;
- }
- }
- else{
- Matrix[xPos-1][yPos] = "S";
- CurrPos = Double.parseDouble((xPos-1) + "." + yPos);
- break;
- }
- }
- break;
- case "D":
- for (int col = 0; col < colCount; col++) {
- if(Matrix[xPos+1][col].equals("b")){
- if(col < yPos){
- Matrix[xPos+1][yPos] = "X";
- CurrPos = Double.parseDouble("-" + (xPos+1) + "." + yPos);
- break;
- }
- else if(col == yPos){
- Matrix[xPos-1][yPos] = "S";
- break;
- }
- }
- else if(Matrix[xPos+1][col].equals("d")){
- if(col > yPos){
- Matrix[xPos+1][yPos] = "X";
- CurrPos = Double.parseDouble("-" + (xPos+1) + "." + yPos);
- break;
- }
- else if(col == yPos){
- Matrix[xPos+1][yPos] = "S";
- break;
- }
- }
- else{
- Matrix[xPos+1][yPos] = "S";
- CurrPos = Double.parseDouble((xPos+1) + "." + yPos);
- break;
- }
- }
- break;
- case "L":
- for (int col = 0; col < colCount; col++) {
- if (Matrix[xPos][col].equals("b")) {
- if (col < yPos) {
- Matrix[xPos][yPos-1] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos-1));
- break;
- }
- } else if (Matrix[xPos][col].equals("d")) {
- if (col > yPos) {
- Matrix[xPos][yPos-1] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos-1));
- break;
- }
- } else {
- Matrix[xPos][yPos-1] = "S";
- CurrPos = Double.parseDouble(xPos + "." + (yPos-1));
- break;
- }
- }
- break;
- case "R":
- for (int col = 0; col < colCount; col++) {
- if (Matrix[xPos][col].equals("b")) {
- if (col < yPos) {
- Matrix[xPos][yPos+1] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos+1));
- break;
- }
- } else if (Matrix[xPos][col].equals("d")) {
- if (col > yPos) {
- Matrix[xPos + 1][yPos+1] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos+1));
- break;
- }
- } else {
- Matrix[xPos][yPos+1] = "S";
- CurrPos = Double.parseDouble(xPos + "." + (yPos+1));
- break;
- }
- }
- break;
- case "W":
- for (int col = 0; col < colCount; col++) {
- if (Matrix[xPos][col].equals("b")) {
- if (col < yPos) {
- Matrix[xPos][yPos] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos));
- break;
- }
- } else if (Matrix[xPos][col].equals("d")) {
- if (col > yPos) {
- Matrix[xPos][yPos] = "X";
- CurrPos = Double.parseDouble("-" + xPos + "." + (yPos));
- break;
- }
- } else {
- Matrix[xPos][yPos] = "S";
- CurrPos = Double.parseDouble(xPos + "." + yPos);
- break;
- }
- }
- break;
- }
- return CurrPos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement