import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static int remainingLeaves(int width,int height,int[][] leaves,String winds){
//Calcul directions vents
char[] dir=winds.toCharArray();
for(int ind=0; ind<dir.length; ind++){
if(dir[ind]=='R'){
System.err.println("-Right");
for(int i=0; i<width; i++){
for(int j=height-1; j>0; j--)
leaves[i][j]=leaves[i][j-1];
leaves[i][0]=0;
}
}
else if(dir[ind]=='L'){
System.err.println("-Left");
for(int i=0; i<width; i++){
for(int j=0; j<height-1; j++)
leaves[i][j]=leaves[i][j+1];
leaves[i][height-1]=0;
}
}
else if(dir[ind]=='U'){
System.err.println("-Top");
for(int i=0; i<width-1; i++)
leaves[i]=leaves[i+1];
leaves[width-1]=new int[width];
}
else if(dir[ind]=='D'){
System.err.println("-Bottom");
for(int i=width-1; i>0; i--)
leaves[i]=leaves[i-1];
leaves[0]=new int[width];
}
}
//Compter les feuilles et affichage final
int remaining=0;
for(int i=0; i<width;i++){
for(int j=0; j<height;j++){
System.err.print(leaves[i][j]);
remaining+=(int)leaves[i][j];
}
System.err.println();
}
return remaining;
}
public static void main(String[] args) {
int width=4;
int height=4;
int[][] leaves = new int[][]{{0,0,0,0},
{0,0,2,0},
{0,1,1,1},
{0,2,3,0}};
String winds="RRD";
//Affichage initial
for(int i=0; i<width;i++){
for(int j=0; j<height;j++)
System.err.print(leaves[i][j]);
System.err.println();
}
System.out.println("remaining leaves : " + remainingLeaves(width,height,leaves,winds));
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: