Skip to content

Commit 8c601d8

Browse files
committed
tccc 03 round 4 div 1 250
1 parent cc6972b commit 8c601d8

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

ChessMetric.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
using namespace std;
9+
long long A[100][100];
10+
long long B[100][100];
11+
12+
class ChessMetric {
13+
public:
14+
long long howMany(int n, vector <int> start, vector <int> end, int numMoves)
15+
{
16+
int s[16][2] = {1,1,-1,1,1,-1,-1,-1,1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1,0,1,0,-1,1,0,-1,0};
17+
18+
memset(A, 0, sizeof(A));
19+
memset(B, 0, sizeof(B));
20+
int i = end[0], j = end[1];
21+
for(int k = 0; k < 16; k++)
22+
{
23+
int x = i+s[k][0], y = j+s[k][1];
24+
if(x < n && y < n && x >= 0 && y >= 0){
25+
A[x][y] = 1ll;
26+
// cout<<x<<" "<<y<<endl;
27+
}
28+
}
29+
30+
31+
if(numMoves == 1) return A[start[0]][start[1]];
32+
33+
for(int ss = 2; ss <= numMoves; ss++){
34+
if(ss%2) memset(A, 0, sizeof(A));
35+
else memset(B, 0, sizeof(B));
36+
37+
for(int i = 0; i < n; i++)
38+
for(int j = 0; j < n; j++)
39+
for(int k = 0; k < 16; k++)
40+
{
41+
int x = i+s[k][0], y = j+s[k][1];
42+
if(x < n && y < n && x >= 0 && y >= 0){
43+
// cout<<x<<" "<<y<<endl;
44+
if(ss%2 && B[x][y]) A[i][j] += B[x][y];
45+
else if(!(ss%2) && A[x][y]) B[i][j] += A[x][y];
46+
}
47+
}
48+
}
49+
50+
if(numMoves%2)
51+
return A[start[0]][start[1]];
52+
return B[start[0]][start[1]];
53+
54+
}
55+
56+
// BEGIN CUT HERE
57+
public:
58+
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(); if ((Case == -1) || (Case == 4)) test_case_4(); }
59+
private:
60+
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(); }
61+
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
62+
void test_case_0() { int Arg0 = 3; int Arr1[] = {0,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,0}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; long long Arg4 = 1LL; verify_case(0, Arg4, howMany(Arg0, Arg1, Arg2, Arg3)); }
63+
void test_case_1() { int Arg0 = 3; int Arr1[] = {0,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,2}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; long long Arg4 = 1LL; verify_case(1, Arg4, howMany(Arg0, Arg1, Arg2, Arg3)); }
64+
void test_case_2() { int Arg0 = 3; int Arr1[] = {0,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2,2}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; long long Arg4 = 0LL; verify_case(2, Arg4, howMany(Arg0, Arg1, Arg2, Arg3)); }
65+
void test_case_3() { int Arg0 = 3; int Arr1[] = {0,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {0,0}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; long long Arg4 = 5LL; verify_case(3, Arg4, howMany(Arg0, Arg1, Arg2, Arg3)); }
66+
void test_case_4() { int Arg0 = 100; int Arr1[] = {0,0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {0,99}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 50; long long Arg4 = 243097320072600LL; verify_case(4, Arg4, howMany(Arg0, Arg1, Arg2, Arg3)); }
67+
68+
// END CUT HERE
69+
70+
};
71+
72+
// BEGIN CUT HERE
73+
int main()
74+
{
75+
ChessMetric ___test;
76+
___test.run_test(-1);
77+
}
78+
// END CUT HERE

ChessMetric.txt

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
PROBLEM STATEMENT
2+
Suppose you had an n by n chess board and a super piece called a kingknight. Using only one move the kingknight denoted 'K' below can reach any of the spaces denoted 'X' or 'L' below:
3+
.......
4+
..L.L..
5+
.LXXXL.
6+
..XKX..
7+
.LXXXL.
8+
..L.L..
9+
.......
10+
11+
In other words, the kingknight can move either one space in any direction (vertical, horizontal or diagonally) or can make an 'L' shaped move. An 'L' shaped move involves moving 2 spaces horizontally then 1 space vertically or 2 spaces vertically then 1 space horizontally. In the drawing above, the 'L' shaped moves are marked with 'L's whereas the one space moves are marked with 'X's. In addition, a kingknight may never jump off the board.
12+
13+
Given the size of the board, the start position of the kingknight and the end position of the kingknight, your method will return how many possible ways there are of getting from start to end in exactly numMoves moves. start and finish are vector <int>s each containing 2 elements. The first element will be the (0-based) row position and the second will be the (0-based) column position. Rows and columns will increment down and to the right respectively. The board itself will have rows and columns ranging from 0 to size-1 inclusive.
14+
15+
Note, two ways of getting from start to end are distinct if their respective move sequences differ in any way. In addition, you are allowed to use spaces on the board (including start and finish) repeatedly during a particular path from start to finish. We will ensure that the total number of paths is less than or equal to 2^63-1 (the upper bound for a long long).
16+
17+
DEFINITION
18+
Class:ChessMetric
19+
Method:howMany
20+
Parameters:int, vector <int>, vector <int>, int
21+
Returns:long long
22+
Method signature:long long howMany(int size, vector <int> start, vector <int> end, int numMoves)
23+
24+
25+
NOTES
26+
-For C++ users: long long is a 64 bit datatype and is specific to the GCC compiler.
27+
28+
29+
CONSTRAINTS
30+
-size will be between 3 and 100 inclusive
31+
-start will contain exactly 2 elements
32+
-finish will contain exactly 2 elements
33+
-Each element of start and finish will be between 1 and size-1 inclusive
34+
-numMoves will be between 1 and 50 inclusive
35+
-The total number of paths will be at most 2^63-1.
36+
37+
38+
EXAMPLES
39+
40+
0)
41+
3
42+
{0,0}
43+
{1,0}
44+
1
45+
46+
Returns: 1
47+
48+
Only 1 way to get to an adjacent square in 1 move
49+
50+
1)
51+
3
52+
{0,0}
53+
{1,2}
54+
1
55+
56+
Returns: 1
57+
58+
A single L-shaped move is the only way
59+
60+
2)
61+
3
62+
{0,0}
63+
{2,2}
64+
1
65+
66+
Returns: 0
67+
68+
Too far for a single move
69+
70+
3)
71+
3
72+
{0,0}
73+
{0,0}
74+
2
75+
76+
Returns: 5
77+
78+
Must count all the ways of leaving and then returning to the corner
79+
80+
4)
81+
100
82+
{0,0}
83+
{0,99}
84+
50
85+
86+
Returns: 243097320072600

0 commit comments

Comments
 (0)