本文主要是介绍回收站选址(CCF 201912-2)解题思路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分析
把x,y坐标拼接成一个字符串(x,y)作为Set的key,保存到Set中,遍历Set,取出坐标,然后判断上下左右四个点是否在Set中,如果在,进而判断,四个角是否在Set中,用一个数组,保存回收站的分数,key是(0-4),值是选址个数。
import java.util.*;public class RecycleBin {public static class Pos {int x;int y;public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public Pos(int x, int y) {this.x = x;this.y = y;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Pos pos = (Pos) o;return x == pos.x && y == pos.y;}@Overridepublic int hashCode() {return Objects.hash(x, y);}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int num = scanner.nextInt();Set<Pos> set = new HashSet<>();int[] arr = new int[5];for(int i = 0; i < num; i++){int x = scanner.nextInt();int y = scanner.nextInt();Pos pos = new Pos(x,y);set.add(pos);}for(Pos pos : set){int x = pos.getX();int y = pos.getY();Pos pUp = new Pos(x,y+1);Pos pDown = new Pos(x,y-1);Pos pLeft = new Pos(x+1,y);Pos pRight = new Pos(x-1,y);Pos pRightUp = new Pos(x+1,y+1);Pos pRightDown = new Pos(x+1,y-1);Pos pLeftUp = new Pos(x-1,y+1);Pos pLeftDown = new Pos(x-1,y-1);if(set.contains(pUp)&&set.contains(pDown)&&set.contains(pLeft) &&set.contains(pRight)){int count = 0;if(set.contains(pRightUp)){count++;}if(set.contains(pRightDown)){count++;}if(set.contains(pLeftUp)){count++;}if(set.contains(pLeftDown)){count++;}arr[count]++;}}for(int i : arr){System.out.println(i);}}
}
这篇关于回收站选址(CCF 201912-2)解题思路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!