Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SocialDistance {
- /**
- * Given the layout of a class with available seats marked with an ’A’ and
- * invalid sites marked with a ’. ’, returns the resulting layout with the
- * sites occupied by the students marked with a ’#’ following two rules :
- * - Students occupy an empty seat if there are no other adjacent students .
- * - A student leaves a seat empty if he/ she has 4 or more adjacent students .
- * @param layout The initial layout .
- * @return The resulting layout .
- * @throws IllegalArgumentException if the initial layout is invalid (is null ,
- * is ragged , includes characters other than ’.’ or ’A ’)).
- */
- public static char [][] seatingPeople ( char [][] layout ) {
- int i;
- int j;
- int rodeado = 0;
- if (layout==null){
- throw new IllegalArgumentException("Disposición nula\n");
- }
- int rowLength = layout[0].length;
- for (char[] fila : layout) {
- if (fila.length != rowLength) {
- throw new IllegalArgumentException("Ragged array\n");
- }
- }
- for(i=0,j=0; layout[i][j] != 0; i++){
- for(j=0; layout[i][j] != 0; j++){
- if(layout[i][j] != 'A' && layout[i][j] != '.'){
- throw new IllegalArgumentException("Caracteres invalidos\n");
- }
- }
- }
- for(i=0,j=0; layout[i][j] != 0; i++){
- for(j=0; layout[i][j] != 0; j++){
- if(layout[i][j] == 'A'){
- layout[i][j] = '#';
- }
- }
- }
- for(i=0,j=0; layout[i][j] != 0; i++){
- for(j=0; layout[i][j] != 0; j++){
- if(layout[i][j] == '#'){
- if(layout[i][j-1]== '#'){
- rodeado++;
- }
- if(layout[i][j+1]== '#'){
- rodeado++;
- }
- if(layout[i-1][j]== '#'){
- rodeado++;
- }
- if(layout[i-1][j-1]== '#'){
- rodeado++;
- }
- if(layout[i-1][j+1]== '#'){
- rodeado++;
- }
- if(layout[i+1][j]== '#'){
- rodeado++;
- }
- if(layout[i+1][j-1]== '#'){
- rodeado++;
- }
- if(layout[i+1][j+1]== '#'){
- rodeado++;
- }
- if(rodeado>=4){
- layout[i][j] = 'A';
- }
- }
- }
- }
- for(i=0,j=0; layout[i][j] != 0; i++){
- for(j=0; layout[i][j] != 0; j++){
- if(layout[i][j] == 'A'){
- if(layout[i][j-1]== '#'){
- rodeado++;
- }
- if(layout[i][j+1]== '#'){
- rodeado++;
- }
- if(layout[i-1][j]== '#'){
- rodeado++;
- }
- if(layout[i-1][j-1]== '#'){
- rodeado++;
- }
- if(layout[i-1][j+1]== '#'){
- rodeado++;
- }
- if(layout[i+1][j]== '#'){
- rodeado++;
- }
- if(layout[i+1][j-1]== '#'){
- rodeado++;
- }
- if(layout[i+1][j+1]== '#'){
- rodeado++;
- }
- if(rodeado<4){
- layout[i][j] = '#';
- }
- }
- }
- }
- return layout;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement