TopCoder: SRM578 DIV2 500

Problem Statement

     Crow Keith is looking at the goose cage in the zoo. The bottom of the cage is divided into a grid of square cells. There are some birds sitting on those cells (with at most one bird per cell). Some of them are geese and all the others are ducks. Keith wants to know which birds are geese. He knows the following facts about them:
  • There is at least one goose in the cage.
  • Each bird within Manhattan distance dist of any goose is also a goose.
You are given a vector <string> field and the int dist. The array field describes the bottom of the cage. Each character of each element of field describes one of the cells. The meaning of individual characters follows.
  • The character 'v' represents a cell that contains a bird.
  • The character '.' represents an empty cell.
Return the number of possible sets of geese in the cage, modulo 1,000,000,007. Note that for some of the test cases there can be no possible sets of geese.

Definition

    
Class: GooseInZooDivTwo
Method: count
Parameters: vector <string>, int
Returns: int
Method signature: int count(vector <string> field, int dist)
(be sure your method is public)
    
 

Notes

- The Manhattan distance between cells (a,b) and (c,d) is |a-c| + |b-d|, where || denotes absolute value. In words, the Manhattan distance is the smallest number of steps needed to get from one cell to the other, given that in each step you can move to a cell that shares a side with your current cell.

Constraints

- field will contain between 1 and 50 elements, inclusive.
- Each element of field will contain between 1 and 50 characters, inclusive.
- Each element of field will contain the same number of characters.
- Each character of each element of field will be 'v' or '.'.
- dist will be between 0 and 100, inclusive.

Examples

0)  
    
{"vvv"}
0
Returns: 7
There are seven possible sets of positions of geese: "ddg", "dgd", "dgg", "gdd", "gdg", "ggd", "ggg" ('g' are geese and 'd' are ducks).
1)  
    
{"."}
100
Returns: 0
The number of geese must be positive, but there are no birds in the cage.
2)  
    
{"vvv"}
1
Returns: 1
 
3)  
    
{"v.v..................v............................"

,".v......v..................v.....................v"

,"..v.....v....v.........v...............v......v..."

,".........vvv...vv.v.........v.v..................v"

,".....v..........v......v..v...v.......v..........."

,"...................vv...............v.v..v.v..v..."

,".v.vv.................v..............v............"

,"..vv.......v...vv.v............vv.....v.....v....."

,"....v..........v....v........v.......v.v.v........"

,".v.......v.............v.v..........vv......v....."

,"....v.v.......v........v.....v.................v.."

,"....v..v..v.v..............v.v.v....v..........v.."

,"..........v...v...................v..............v"

,"..v........v..........................v....v..v..."

,"....................v..v.........vv........v......"

,"..v......v...............................v.v......"

,"..v.v..............v........v...............vv.vv."

,"...vv......v...............v.v..............v....."

,"............................v..v.................v"

,".v.............v.......v.........................."

,"......v...v........................v.............."

,".........v.....v..............vv.................."

,"................v..v..v.........v....v.......v...."

,"........v.....v.............v......v.v............"

,"...........v....................v.v....v.v.v...v.."

,"...........v......................v...v..........."

,"..........vv...........v.v.....................v.."

,".....................v......v............v...v...."

,".....vv..........................vv.v.....v.v....."

,".vv.......v...............v.......v..v.....v......"

,"............v................v..........v....v...."

,"................vv...v............................"

,"................v...........v........v...v....v..."

,"..v...v...v.............v...v........v....v..v...."

,"......v..v.......v........v..v....vv.............."

,"...........v..........v........v.v................"

,"v.v......v................v....................v.."

,".v........v................................v......"

,"............................v...v.......v........."

,"........................vv.v..............v...vv.."

,".......................vv........v.............v.."

,"...v.............v.........................v......"

,"....v......vv...........................v........."

,"....vv....v................v...vv..............v.."

,".................................................."

,"vv........v...v..v.....v..v..................v...."

,".........v..............v.vv.v.............v......"

,".......v.....v......v...............v............."

,"..v..................v................v....v......"

,".....v.....v.....................v.v......v......."}
3
Returns: 797922654

这题当场没有做出来,事后想了下只要把可以互相影响(dist之内)的v点归为一类,那么只要该类里的某个v点为goose,其他点也为goose,否则都是duck,这样返回的值应该为2的类数量次方,再减1(不能全为duck),我用了vector来记录这些个类,每个类都是个vector,里面为点Node,这样最后返回的为int(pow(2, vector.size()))-1;

所有数据都能过,除了最后一个例子过不了,用system test run了一下,可惜其他的一些数据都能过,而且都是些小数据,这样我很难找出到底问题出在哪。期待大牛能够解答。

View Code
  1  #include <functional>

  2  #include <algorithm>

  3  #include <stdexcept>

  4  #include <iostream>

  5  #include <sstream>

  6  #include <fstream>

  7  #include <iomanip>

  8  #include <cstdlib>

  9  #include <cstring>

 10  #include <utility>

 11  #include <cctype>

 12  #include <vector>

 13  #include <string>

 14  #include <bitset>

 15  #include <queue>

 16  #include <stack>

 17  #include <ctime>

 18  #include <list>

 19  #include <map>

 20  #include <set>

 21  #include <cmath>

 22 

 23  using namespace std;

 24 

 25  #define pb push_back

 26  #define INF 100000000000

 27  #define L(s) (int)((s).size())

 28  #define FOR(i,a,b) for (int _n(b), i(a); i<=_n; i++)

 29  #define rep(i,n) FOR(i,1,(n))

 30  #define rept(i,n) FOR(i,0,(n)-1)

 31  #define C(a) memset((a), 0, sizeof(a))

 32  #define ll long long

 33  #define VI vector<int>

 34  #define ppb pop_back

 35  #define mp make_pair

 36  #define MOD 1000000007

 37  int toInt(string s){ istringstream sin(s); int t; sin>>t;return t;}

 38 

 39 class GooseInZooDivTwo {

 40 public:

 41 struct Node {

 42     int x;

 43     int y;

 44     Node(int a, int b) : x(a), y(b) { }

 45 };

 46 

 47 vector<Node> flood(vector<string> &field, vector<vector<bool>> &visit, int x, int y, int dist, int m, int n)

 48 {

 49     vector<Node> ret;

 50     queue<Node> S;

 51     visit[x][y] = true;

 52     S.push(Node(x, y));

 53     while (!S.empty())

 54     {

 55         Node cur = S.front();

 56         ret.pb(S.front());

 57         S.pop();

 58         for (int i = max(0, cur.x-dist); i <= min(m-1, cur.x+dist); i++)

 59         {

 60             for (int j = max(0, cur.y-dist); j <= min(n-1, cur.y+dist); j++)

 61             {

 62                 if (field[i][j] == 'v' && !visit[i][j] && (abs(i-cur.x)+abs(j-cur.y) <= dist))

 63                 {

 64                     S.push(Node(i, j));

 65                     visit[i][j] = true;

 66                 }

 67             }

 68         }

 69     }

 70     return ret;

 71 }

 72 

 73 int count(vector <string> field, int dist) {

 74     int m = L(field);

 75     if (!m) return 0;

 76     int n = L(field[0]);

 77     vector<vector<bool>> visit(m, vector<bool>(n, false));

 78     vector<vector<Node>> ret;

 79     rept(i, m)

 80     {

 81         rept(j, n)

 82         {

 83             if (field[i][j] == 'v' && !visit[i][j])

 84             {

 85                 ret.pb(flood(field, visit, i, j, dist, m, n));

 86             }

 87         }

 88     }

 89     if (!L(ret)) return 0;

 90     return (int(pow(2.0, L(ret)))-1)%MOD;

 91 }

 92 

 93 // BEGIN CUT HERE

 94     public:

 95     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }

 96     private:

 97     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }

 98     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }

 99     void test_case_0() { string Arr0[] = {"vvv"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 7; verify_case(0, Arg2, count(Arg0, Arg1)); }

100     void test_case_1() { string Arr0[] = {"."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 0; verify_case(1, Arg2, count(Arg0, Arg1)); }

101     void test_case_2() { string Arr0[] = {"vvv"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; verify_case(2, Arg2, count(Arg0, Arg1)); }

102     void test_case_3() { string Arr0[] = {"v.v..................v............................"

103 ,".v......v..................v.....................v"

104 ,"..v.....v....v.........v...............v......v..."

105 ,".........vvv...vv.v.........v.v..................v"

106 ,".....v..........v......v..v...v.......v..........."

107 ,"...................vv...............v.v..v.v..v..."

108 ,".v.vv.................v..............v............"

109 ,"..vv.......v...vv.v............vv.....v.....v....."

110 ,"....v..........v....v........v.......v.v.v........"

111 ,".v.......v.............v.v..........vv......v....."

112 ,"....v.v.......v........v.....v.................v.."

113 ,"....v..v..v.v..............v.v.v....v..........v.."

114 ,"..........v...v...................v..............v"

115 ,"..v........v..........................v....v..v..."

116 ,"....................v..v.........vv........v......"

117 ,"..v......v...............................v.v......"

118 ,"..v.v..............v........v...............vv.vv."

119 ,"...vv......v...............v.v..............v....."

120 ,"............................v..v.................v"

121 ,".v.............v.......v.........................."

122 ,"......v...v........................v.............."

123 ,".........v.....v..............vv.................."

124 ,"................v..v..v.........v....v.......v...."

125 ,"........v.....v.............v......v.v............"

126 ,"...........v....................v.v....v.v.v...v.."

127 ,"...........v......................v...v..........."

128 ,"..........vv...........v.v.....................v.."

129 ,".....................v......v............v...v...."

130 ,".....vv..........................vv.v.....v.v....."

131 ,".vv.......v...............v.......v..v.....v......"

132 ,"............v................v..........v....v...."

133 ,"................vv...v............................"

134 ,"................v...........v........v...v....v..."

135 ,"..v...v...v.............v...v........v....v..v...."

136 ,"......v..v.......v........v..v....vv.............."

137 ,"...........v..........v........v.v................"

138 ,"v.v......v................v....................v.."

139 ,".v........v................................v......"

140 ,"............................v...v.......v........."

141 ,"........................vv.v..............v...vv.."

142 ,".......................vv........v.............v.."

143 ,"...v.............v.........................v......"

144 ,"....v......vv...........................v........."

145 ,"....vv....v................v...vv..............v.."

146 ,".................................................."

147 ,"vv........v...v..v.....v..v..................v...."

148 ,".........v..............v.vv.v.............v......"

149 ,".......v.....v......v...............v............."

150 ,"..v..................v................v....v......"

151 ,".....v.....v.....................v.v......v......."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 797922654; verify_case(3, Arg2, count(Arg0, Arg1)); }

152 

153 // END CUT HERE

154 

155 };

156 

157 // BEGIN CUT HERE

158 int main() {

159     GooseInZooDivTwo ___test;

160     ___test.run_test(-1);

161 }

162 // END CUT HERE

 

你可能感兴趣的:(topcoder)