POJ1054

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1054

 解题思路:穷举前两个点,确定一条线,查找是否符合条件,如果符合,记录跳数。

 

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4.     class Point implements Comparable {
  5.         int row;
  6.         int col;
  7.         public Point(int x, int y) {
  8.             this.row = x;
  9.             this.col = y;
  10.         }
  11.         public int compareTo(Object o) {
  12.             int result = 0;
  13.             result = this.row - ((Point) o).row;
  14.             if (result == 0) {
  15.                 return this.col - ((Point) o).col;
  16.             }
  17.             return result;
  18.         }
  19.     }
  20.     int width;
  21.     int length;
  22.     int num;
  23.     Point[] points;
  24.     int t_w;
  25.     int t_l;
  26.     int t_r;
  27.     int t_c;
  28.     int t_p;
  29.     int max;
  30.     int t_max;
  31.     boolean[][] tian;
  32.     public Main() {
  33.         Scanner scan = new Scanner(System.in);
  34.         width = scan.nextInt();
  35.         length = scan.nextInt();
  36.         tian = new boolean[width][length];
  37.         num = scan.nextInt();
  38.         points = new Point[num];
  39.         for (int i = 0; i < num; i++) {
  40.             t_r = scan.nextInt() - 1;
  41.             t_c = scan.nextInt() - 1;
  42.             points[i] = new Point(t_r, t_c);
  43.             tian[t_r][t_c] = true;
  44.         }
  45.         Arrays.sort(points);
  46.         search();
  47.         if (max >= 3) {
  48.             System.out.println(max);
  49.         } else {
  50.             System.out.println(0);
  51.         }
  52.     }
  53.     public void search() {
  54.         max = 0;
  55.         for (int i = 0; i < num; i++) {
  56.             for (int j = i + 1; j < num; j++) {
  57.                 t_w = points[j].row - points[i].row;
  58.                 t_l = points[j].col - points[i].col;
  59.                 t_r = points[i].row - t_w;
  60.                 t_c = points[i].col - t_l;
  61.                 if (t_r >= 0 && t_r < width && t_c >= 0 && t_c < length) {
  62.                     continue;
  63.                 }
  64.                 t_r = points[j].row;
  65.                 t_c = points[j].col;
  66.                 t_max = getLength(2);
  67.                 if (max < t_max) {
  68.                     max = t_max;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     public int getLength(int sum) {
  74.         t_r += t_w;
  75.         t_c += t_l;
  76.         while (t_r >= 0 && t_r < width && t_c >= 0 && t_c < length) {
  77.             if (hasPoint(t_r, t_c)) {
  78.                 sum++;
  79.             } else {
  80.                 return 0;
  81.             }
  82.             t_r += t_w;
  83.             t_c += t_l;
  84.         }
  85.         return sum;
  86.     }
  87.     public boolean hasPoint(int row, int col) {
  88.         if (tian[row][col]) {
  89.             return true;
  90.         } else {
  91.             return false;
  92.         }
  93.     }
  94.     public static void main(String[] args) {
  95.         new Main();
  96.     }
  97. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值