阿里客户端笔试第二题,,

import java.util.ArrayList;
import java.util.Scanner;

public class Main{
	public static class Point{
		public int x;
		public int y;
		
		public Point(int x, int y){
			this.x = x;
			this.y = y;
		}
		
		public boolean isNear(Point p){
			if (Math.abs(this.x - p.x)<=1 && Math.abs(this.y - p.y)<=1) {
				return true;
			}
			return false;
		}
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int n = in.nextInt();
			int[][] a = new int[n][n];
			for(int i=0; i<n; i++){
				for(int j=0; j<a[i].length; j++){
					if (in.next().equals("0")) {
						a[i][j] = 0;
					}else {
						a[i][j] = 1;
					}
				}
			}
			
			System.out.println(getNum(a));
		}
	}
	
	private static int getNum(int[][] a){
		ArrayList<Point> points = new ArrayList<Point>();
		
		for(int i=0; i<a.length; i++){
			for(int j=0; j<a[i].length; j++){
				if (a[i][j]!=0) {
					points.add(new Point(i, j));
				}
			}
		}
		
		int res = 0;
		for(int i=0; i<points.size(); i++){
			boolean flag = false;
			for(int j=0; j<i; j++){
				if (points.get(i).isNear(points.get(j))) {
					flag = true;
                                        break;    
				}
			}
			if (!flag) {
				res++;
			}
		}
		return res;
	}
}
    

#阿里巴巴#
全部评论
大佬,分享下思路
点赞 回复 分享
发布于 2017-08-25 21:36

相关推荐

02-23 12:32
已编辑
门头沟学院 嵌入式工程师
King987:学历没有问题,然后既然有实习经历的话,把这个放在上面多写一点,哪怕你自己包装一下,只要能圆回来就行,既然有实习经历的话,肯定主要看实习经历之类的。然后也会主要问这里多准备准备
点赞 评论 收藏
分享
图源网络侵权删
dddooola:这个事情我去年8月就看到有人b站(up:程序员正义)在举报了,可能一直在查,这次是查明后的处分了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务