Hackerrank: a map with Holes

Source: Internet
Author: User

Give you a map of N * n. Each grid in the map has a value indicating the depth of the region. We call a grid in a map empty. if and only when the grid is not at the edge of the map and each grid adjacent to it has a smaller depth than it. Two grids are called adjacent if they have one edge.

You need to find all the holes in the map and useXDescription.

Input Format

The first line contains an integer N, indicating the map size. In the next n rows, each line contains n positive numbers without blank spaces. Each number (1-9) indicates the depth of the corresponding area.

Output Format

Output n rows, indicating the final map result. Each empty space should contain charactersXReplace.

Constraints

1 <= n <= 100

1 ≤ n ≤100

 

Question: A simple simulation.

Question:

 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6  7 public class Solution { 8     public static void main(String[] args) { 9         Scanner in = new Scanner(System.in);10         int n = in.nextInt();11         int[][] ar = new int[n][n];12         for(int i = 0;i < n;i++){13             String temp = in.next();14             for(int j = 0;j < n;j++)15                 ar[i][j] = temp.charAt(j) - ‘0‘; 16         }17         18         for(int i = 0;i < n;i++){19             StringBuffer sb = new StringBuffer();20             for(int j = 0;j < n;j++){21                 boolean isHole = true;22                 //up23                 if(i-1<0||i+1>=n||j-1<0||j+1>=n)24                     isHole = false;25                 else{26                     if(ar[i-1][j] >= ar[i][j])27                         isHole = false;28                     if(ar[i+1][j] >= ar[i][j])29                         isHole = false;30                     if(ar[i][j-1] >= ar[i][j])31                         isHole = false;32                     if(ar[i][j+1] >= ar[i][j])33                         isHole = false;34                 }35                 36                 if(isHole)37                     sb.append(‘X‘);38                 else {39                     sb.append(ar[i][j]); 40                 }41             }42             System.out.println(sb.toString());43         }44       }45 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.