Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SocialDistance;
- public class SocialDistance {
- public static int[][] crearMatrizRodeados(char[][] layout){
- int i;
- int j;
- int rodeados[][] = new int[layout.length][layout[0].length];
- for(i = 0; i< layout.length; i++){
- for(j= 0; j<layout[0].length; j++){
- rodeados[i][j]=0;
- }
- }
- return rodeados;
- }
- public static int[][] contarRodeados(char[][] layout){
- int i;
- int j;
- int [][] rodeados;
- rodeados= crearMatrizRodeados(layout);
- for(i=0; i<layout.length; i++){
- for(j=0; j<layout[i].length; j++){
- if( j>0 && layout[i][j-1]== '#'){
- rodeados[i][j]++;
- }
- if(j<layout[i].length-1 && layout[i][j+1]== '#'){
- rodeados[i][j]++;
- }
- if(i>0) {
- if (j > 0 && layout[i - 1][j - 1] == '#') {
- rodeados[i][j]++;
- }
- if (layout[i - 1][j] == '#') {
- rodeados[i][j]++;
- }
- if (j < layout[i].length - 1 && layout[i - 1][j + 1] == '#') {
- rodeados[i][j]++;
- }
- }
- if(i< layout.length-1) {
- if (j > 0 && layout[i +1][j - 1] == '#') {
- rodeados[i][j]++;
- }
- if (layout[i + 1][j] == '#') {
- rodeados[i][j]++;
- }
- if (j < layout[i].length - 1 && layout[i + 1][j + 1] == '#') {
- rodeados[i][j]++;
- }
- }
- }
- }
- return rodeados;
- }
- /**
- * 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 rodeados[][];
- boolean cambio;
- if (layout == null) {
- throw new IllegalArgumentException("Disposición nula\n");
- }
- int rowLength0 = layout[0].length;
- for (j = 1; j < layout.length; j++) {
- if (layout[j].length != rowLength0) {
- throw new IllegalArgumentException("Ragged array\n");
- }
- }
- for (i = 0; i < layout.length; i++) {
- for (j = 0; j < layout[i].length; j++) {
- if (layout[i][j] != 'A' && layout[i][j] != '.') {
- throw new IllegalArgumentException("Caracteres invalidos\n");
- }
- }
- }
- do {
- cambio = false;
- rodeados = contarRodeados(layout);
- for (i = 0; i < layout.length; i++) {
- for (j = 0; j < layout[i].length; j++) {
- if (layout[i][j] == 'A' && rodeados[i][j] == 0) {
- layout[i][j] = '#';
- cambio = true;
- }
- }
- }
- rodeados = contarRodeados(layout);
- for (i = 0; i < layout.length; i++) {
- for (j = 0; j < layout[i].length; j++) {
- if (layout[i][j] == '#' && rodeados[i][j] >= 4) {
- layout[i][j] = 'A';
- cambio = true;
- }
- }
- }
- }while(cambio==true);
- return layout;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement