【codechef】Lighthouses

https://www.codechef.com/problems/LIGHTHSE

好好的一道分类讨论题被我想复杂了。。。。。。。。。。。。。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
  int x;
  int y;
  int i;
} coord;

int main(void)
{
  int t;
  scanf("%d",&t);
  while (t--) {
      int n;
      scanf("%d",&n);
      int i;
      coord c;
      coord wn, ws, en, es, nw, ne, sw, se;
      scanf("%d %d",&c.x,&c.y); c.i = 1;
      wn = ws = en = es = nw = ne = sw = se = c;
      for (i = 2; i <= n; i++) {
        scanf("%d %d",&c.x,&c.y); c.i = i;
        if (c.x <= wn.x) {
          if (c.x == wn.x) {
            if (c.y > wn.y) wn = c;
            else if (c.y < ws.y) ws = c;
          } else
            wn = ws = c;
        }
        if (c.x >= en.x) {
          if (c.x == en.x) {
            if (c.y > en.y) en = c;
            else if (c.y < es.y) es = c;
          } else
            en = es = c;
        }
        if (c.y >= nw.y) {
          if (c.y == nw.y) {
            if (c.x > ne.x) ne = c;
            else if (c.x < nw.x) nw = c;
          } else
            nw = ne = c;
        }
        if (c.y <= sw.y) {
          if (c.y == sw.y) {
            if (c.x > se.x) se = c;
            else if (c.x < sw.x) sw = c;
          } else
            sw = se = c;
        }
      } // for
      
      // check for one lighthouse solution
      if (wn.i == nw.i) {
        printf("1\n%d SE\n",wn.i);
        continue;
      }
      if (ws.i == sw.i) {
        printf("1\n%d NE\n",ws.i);
        continue;
      }
      if (en.i == ne.i) {
        printf("1\n%d SW\n",en.i);
        continue;
      }
      if (es.i == se.i) {
        printf("1\n%d NW\n",es.i);
        continue;
      }
      // two lighthouse solution
      if (wn.y > en.y) printf("2\n%d SE\n%d NW\n",wn.i,en.i);
      else printf("2\n%d NE\n%d SW\n",wn.i,en.i);
    }
  return 0;
}


你可能感兴趣的:(【codechef】Lighthouses)