SRM322 D2H BattleshipChecker

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

  • 長さ$1$の船が$4$つ
  • 長さ$2$の船が$3$つ
  • 長さ$3$の船が$2$つ
  • 長さ$4$の船が$1$つ

以上の船があり,なおかつ船がひとつもない行と列の数を返す.愚直に全部あるか確かめる.斜めもあるので注意する.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int x[20][20];

bool check(vector<string> s) {
  int n = s.size();
  map<int, int> m;
  memset(x, -1, sizeof(x));

  int id = 0;
  rep(i, n) {
      rep(j, n) {
          if(s[i][j] != 'X') continue;
          if(x[i][j] != -1) continue;

          x[i][j] = id;
          int w = 1, h = 1;
          REP(k, j+1, n) {
              if(s[i][k] == 'X') {
                  w++;
                  x[i][k] = id;
                  continue;
              } else break;
          }

          REP(k, i+1, n) {
              if(s[k][j] == 'X') {
                  h++;
                  x[k][j] = id;
                  continue;
              } else break;
          }

          if(max(w, h) > 4) return false;

          m[max(w, h)]++;
          id++;
      }
  }

  bool flag = m[1] == 4 && m[2] == 3 && m[3] == 2 && m[4] == 1;
  if(!flag) return false;

  int dy[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
  int dx[8] = {-1, 0, 1,-1, 1,-1, 0, 1};

  rep(i, n) {
      rep(j, n) {
          if(x[i][j] == -1) continue;

          rep(k, 8) {
              int ni = i + dy[k];
              int nj = j + dx[k];

              if(0 <= ni && ni < n && 0 <= nj && nj < n) {
                  if(x[ni][nj] != -1 && x[ni][nj] != x[i][j]) {
                      return false;
                  }
              }
          }
      }
  }


  return true;
}

class BattleshipChecker {
  public:
  string checkBoard(vector <string> board) {

      if(!check(board)) {
          return "REJECTED";
      }

      int n = board.size(), cnt = 0;
      rep(i, n) {
          bool flag = true;
          rep(j, n) {
              if(board[i][j] != 'X') continue;
              flag = false;
          }

          if(flag) cnt++;
          flag = true;

          rep(j, n) {
              if(board[j][i] != 'X') continue;
              flag = false;
          }

          if(flag) cnt++;
      }

      stringstream ss;
      ss << "ACCEPTED, ";
      ss << cnt;
      ss << " POINTS";

      return ss.str();
  }
};
Nov 18th, 2016