Skip to content

Commit 517158e

Browse files
committed
tco 03 semi 4
1 parent 8c601d8 commit 517158e

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

AvoidRoads.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstdio>
8+
#include <cstring>
9+
using namespace std;
10+
typedef pair<int, int> pi;
11+
int cx[105][105], cy[105][105];
12+
long long t[105][105];
13+
int w, h;
14+
15+
class AvoidRoads {
16+
public:
17+
18+
long long numWays(int width, int height, vector <string> bad)
19+
{
20+
w = width; h = height;
21+
memset(cx, 0, sizeof(cx));
22+
memset(cy, 0, sizeof(cy));
23+
memset(t, 0, sizeof(t));
24+
25+
for(int i = 0; i < bad.size(); i++){
26+
int a, b, aa, bb;
27+
sscanf(bad[i].c_str(), "%d%d%d%d", &a, &b, &aa, &bb);
28+
// cout<<a<<b<<aa<<bb<<endl;
29+
if(a > aa) swap(a, aa);
30+
if(b > bb) swap(b, bb);
31+
if(a < aa) cx[a][b] = 1;
32+
if(b < bb) cy[a][b] = 1;
33+
}
34+
/* for(int i = 0; i < ban.size(); i++){
35+
pi x = ban[i].first, y = ban[i].second;
36+
cout<<x.first<<" "<<x.second<<"\t"<<y.first<<" "<<y.second<<endl;
37+
}
38+
*/ if(cx[0][0] == 0) t[1][0] = 1;
39+
if(cy[0][0] == 0) t[0][1] = 1;
40+
41+
for(int k = 2; k <= h; k++){
42+
for(int i = 0, j = k; i <= w && j >= 0; i++, j--){
43+
long long sum = 0;
44+
if(i > 0 && cx[i-1][j] == 0) sum += t[i-1][j];
45+
if(j > 0 && cy[i][j-1] == 0) sum += t[i][j-1];
46+
t[i][j] = sum;
47+
// cout<<i<<" "<<j<<" = "<<sum<<endl;
48+
}
49+
}
50+
51+
for(int k = 1; k <= w; k++){
52+
for(int i = k, j = h; i <= w && j >= 0; i++, j--){
53+
long long sum = 0;
54+
if(i > 0 && cx[i-1][j] == 0) sum += t[i-1][j];
55+
if(j > 0 && cy[i][j-1] == 0) sum += t[i][j-1];
56+
t[i][j] = sum;
57+
// cout<<i<<j<<" = "<<sum<<endl;
58+
}
59+
}
60+
61+
// return tt(w, h);
62+
return t[w][h];
63+
}
64+
65+
// BEGIN CUT HERE
66+
public:
67+
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(); }
68+
private:
69+
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(); }
70+
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; } }
71+
void test_case_0() { int Arg0 = 6; int Arg1 = 6; string Arr2[] = {"0 0 0 1","6 6 5 6"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); long long Arg3 = 252LL; verify_case(0, Arg3, numWays(Arg0, Arg1, Arg2)); }
72+
void test_case_1() { int Arg0 = 1; int Arg1 = 1; string Arr2[] = {}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); long long Arg3 = 2LL; verify_case(1, Arg3, numWays(Arg0, Arg1, Arg2)); }
73+
void test_case_2() { int Arg0 = 35; int Arg1 = 31; string Arr2[] = {}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); long long Arg3 = 6406484391866534976LL; verify_case(2, Arg3, numWays(Arg0, Arg1, Arg2)); }
74+
void test_case_3() { int Arg0 = 9; int Arg1 = 10; string Arr2[] = {"0 2 0 3", "1 2 1 3", "2 2 2 3", "3 2 3 3", "4 2 4 3", "5 2 5 3", "6 2 6 3", "7 2 7 3", "8 2 8 3", "9 2 9 3"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); long long Arg3 = 0LL; verify_case(3, Arg3, numWays(Arg0, Arg1, Arg2)); }
75+
76+
// END CUT HERE
77+
78+
};
79+
80+
// BEGIN CUT HERE
81+
int main()
82+
{
83+
AvoidRoads ___test;
84+
___test.run_test(-1);
85+
}
86+
// END CUT HERE

AvoidRoads.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
PROBLEM STATEMENT
2+
Problem contains images. Plugin users can view them in the applet.
3+
In the city, roads are arranged in a grid pattern. Each point on the grid represents a corner where two blocks meet. The points are connected by line segments which represent the various street blocks. Using the cartesian coordinate system, we can assign a pair of integers to each corner as shown below.
4+
5+
6+
7+
You are standing at the corner with coordinates 0,0. Your destination is at corner width,height. You will return the number of distinct paths that lead to your destination. Each path must use exactly width+height blocks. In addition, the city has declared certain street blocks untraversable. These blocks may not be a part of any path. You will be given a vector <string> bad describing which blocks are bad. If (quotes for clarity) "a b c d" is an element of bad, it means the block from corner a,b to corner c,d is untraversable. For example, let's say
8+
width = 6
9+
length = 6
10+
bad = {"0 0 0 1","6 6 5 6"}
11+
The picture below shows the grid, with untraversable blocks darkened in black. A sample path has been highlighted in red.
12+
13+
14+
15+
16+
DEFINITION
17+
Class:AvoidRoads
18+
Method:numWays
19+
Parameters:int, int, vector <string>
20+
Returns:long long
21+
Method signature:long long numWays(int width, int height, vector <string> bad)
22+
23+
24+
CONSTRAINTS
25+
-width will be between 1 and 100 inclusive.
26+
-height will be between 1 and 100 inclusive.
27+
-bad will contain between 0 and 50 elements inclusive.
28+
-Each element of bad will contain between 7 and 14 characters inclusive.
29+
-Each element of the bad will be in the format "a b c d" where, a,b,c,d are integers with no extra leading zeros, a and c are between 0 and width inclusive, b and d are between 0 and height inclusive,and a,b is one block away from c,d.
30+
-The return value will be between 0 and 2^63-1 inclusive.
31+
32+
33+
EXAMPLES
34+
35+
0)
36+
6
37+
6
38+
{"0 0 0 1","6 6 5 6"}
39+
40+
Returns: 252
41+
42+
Example from above.
43+
44+
1)
45+
1
46+
1
47+
{}
48+
49+
Returns: 2
50+
51+
Four blocks aranged in a square. Only 2 paths allowed.
52+
53+
2)
54+
35
55+
31
56+
{}
57+
58+
Returns: 6406484391866534976
59+
60+
Big number.
61+
62+
3)
63+
2
64+
2
65+
{"0 0 1 0", "1 2 2 2", "1 1 2 1"}
66+
67+
Returns: 0

0 commit comments

Comments
 (0)