There is an N × N square mesh-shaped grid of wires, as shown in a figure below. Nodes of the grid are at points (X, Y), where X and Y are integers from 0 to N−1. An electric current flows through the grid, between the nodes at (0, 0) and (N−1, N−1).
Initially, all the wires conduct the current, but the wires burn out at a rate of one per second. The burnouts are described by three arrays of integers, A, B and C, each of size M. For each moment T (0 ≤ T < M), in the T-th second the wire between nodes (A[T], B[T]) and:
- (A[T], B[T] + 1), if C[T] = 0 or
- (A[T] + 1, B[T]), if C[T] = 1
burns out. You can assume that the arrays describe existing wires, and that no wire burns out more than once. Your task is to determine when the current stops flowing between the nodes at (0,0) and (N−1,N−1).
Write a function:
int solution(int N, vector<int> &A, vector<int> &B, vector<int> &C);
that, given integer N and arrays A, B and C, returns the number of seconds after which the current stops flowing between the nodes at (0, 0) and (N−1, N−1). If the current keeps flowing even after all M wires burn out, the function should return −1.
For example, given N = 4, M = 9 and the following arrays:
A[0] = 0 B [0] = 0 C[0] = 0 A[1] = 1 B [1] = 1 C[1] = 1 A[2] = 1 B [2] = 1 C[2] = 0 A[3] = 2 B [3] = 1 C[3] = 0 A[4] = 3 B [4] = 2 C[4] = 0 A[5] = 2 B [5] = 2 C[5] = 1 A[6] = 1 B [6] = 3 C[6] = 1 A[7] = 0 B [7] = 1 C[7] = 0 A[8] = 0 B [8] = 0 C[8] = 1your function should return 8, because just after the eighth wire burns out, there is no connection between the nodes at (0, 0) and (N−1, N−1). This situation is shown in the following figure:
Given N = 4, M = 1 and the following arrays:
A[0] = 0 B [0] = 0 C[0] = 0your function should return −1, because burning out a single wire cannot break the connection between the nodes at (0, 0) and (N−1, N−1).
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..400];
- M is an integer within the range [0..2*N*(N-1)];
- each element of arrays A and B is an integer within the range [0..N-1];
- each element of array C is an integer that can have one of the following values: 0, 1.
#include <algorithm>
#include <vector>
#include <stack>
bool Explore(vector< vector<long long> >& mesh, int seconds, int N)
{
bool found = false;
stack< pair<int, int> > path;
int x = 0;
int y = 0;
pair<int, int> newPos;
while (true)
{
//cout << x << "," << y << endl;
// Check if we are at the end node.
if (x == N - 1 && y == N - 1)
{
found = true;
break;
}
// Set this node as 'processed'.
mesh[x][y] |= 1;
if (x != N - 1 && ((mesh[x + 1][y] & 1) == 0) &&
(((mesh[x][y] >> 3) & 0xFFFFF) > seconds || (mesh[x][y] & 2) == 0)) // Right
{
path.push(make_pair(x, y));
x += 1;
continue;
}
else if (y != N - 1 && ((mesh[x][y + 1] & 1) == 0) &&
((mesh[x][y] >> 23) > seconds || (mesh[x][y] & 4) == 0)) // Up
{
path.push(make_pair(x, y));
y += 1;
continue;
}
else if (x != 0 && ((mesh[x - 1][y] & 1) == 0) &&
(((mesh[x - 1][y] >> 3) & 0xFFFFF) > seconds || (mesh[x - 1][y] & 2) == 0)) // Left
{
path.push(make_pair(x, y));
x -= 1;
continue;
}
else if (y != 0 && ((mesh[x][y - 1] & 1) == 0) &&
((mesh[x][y - 1] >> 23) > seconds || (mesh[x][y - 1] & 4) == 0)) // Down
{
path.push(make_pair(x, y));
y -= 1;
continue;
}
// Check if we are back at (or never left) the start node, and have nowhere to go.
if (x == 0 && y == 0)
{
found = false;
break;
}
// Move to previous position and take a different role, if available.
newPos = path.top();
x = newPos.first;
y = newPos.second;
path.pop();
}
return found;
}
int wire_burnouts(int N, const vector<int>& A, const vector<int>& B, const vector<int>& C)
{
if (N == 1)
return -1;
vector< vector<long long> > mesh(N, vector<long long>(N, 0));
size_t vecSize = A.size();
long long highPos;
long long lowPos;
// Apply each burnout to the grid along with the time it happens.
for (size_t i = 0; i < vecSize; ++i)
{
if (C[i] == 1)
{
lowPos = i;
lowPos <<= 3;
mesh[A[i]][B[i]] |= lowPos;
mesh[A[i]][B[i]] |= 2; // Right
}
else
{
highPos = i;
highPos <<= 23;
mesh[A[i]][B[i]] |= highPos;
mesh[A[i]][B[i]] |= 4; // Up
}
// If either corner completely burns out this would immediately stop the current.
//if ((mesh[0][0] & 6) == 6 || ((mesh[N - 2][N - 1] & 2) == 2 && (mesh[N - 1][N - 2] & 4) == 4))
// return i + 1;
}
size_t start = 0;
size_t end = vecSize - 1;
size_t mid = vecSize / 2;
size_t breakPos;
bool currentOK;
while (true)
{
// Check if current can still flow.
if (currentOK = Explore(mesh, mid, N))
{
if (mid != end)
start = mid + 1;
else
break;
}
else
{
breakPos = mid;
if (mid != start)
{
end = mid - 1;
}
else
break;
}
mid = start + ((end - start) / 2);
// Clear all processed flags for next pass.
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
mesh[i][j] &= 0xFFFFFFFFFFFFFFFE;
}
if (currentOK && mid == vecSize - 1)
return -1;
else
return breakPos + 1;
}
In file included from user.c:21: func.c:2:21: error: algorithm: No such file or directory func.c:3:18: error: vector: No such file or directory func.c:4:17: error: stack: No such file or directory In file included from user.c:21: func.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Explore’ func.c:76: warning: type defaults to ‘int’ in declaration of ‘vector’ func.c:76: error: expected ‘;’, ‘,’ or ‘)’ before ‘<’ token func.c:142: warning: integer constant is too large for ‘long’ type
#include <algorithm>
#include <vector>
#include <stack>
bool Explore(vector< vector<long long> >& mesh, int seconds, int N)
{
bool found = false;
stack< pair<int, int> > path;
int x = 0;
int y = 0;
pair<int, int> newPos;
while (true)
{
//cout << x << "," << y << endl;
// Check if we are at the end node.
if (x == N - 1 && y == N - 1)
{
found = true;
break;
}
// Set this node as 'processed'.
mesh[x][y] |= 1;
if (x != N - 1 && ((mesh[x + 1][y] & 1) == 0) &&
(((mesh[x][y] >> 3) & 0xFFFFF) > seconds || (mesh[x][y] & 2) == 0)) // Right
{
path.push(make_pair(x, y));
x += 1;
continue;
}
else if (y != N - 1 && ((mesh[x][y + 1] & 1) == 0) &&
((mesh[x][y] >> 23) > seconds || (mesh[x][y] & 4) == 0)) // Up
{
path.push(make_pair(x, y));
y += 1;
continue;
}
else if (x != 0 && ((mesh[x - 1][y] & 1) == 0) &&
(((mesh[x - 1][y] >> 3) & 0xFFFFF) > seconds || (mesh[x - 1][y] & 2) == 0)) // Left
{
path.push(make_pair(x, y));
x -= 1;
continue;
}
else if (y != 0 && ((mesh[x][y - 1] & 1) == 0) &&
((mesh[x][y - 1] >> 23) > seconds || (mesh[x][y - 1] & 4) == 0)) // Down
{
path.push(make_pair(x, y));
y -= 1;
continue;
}
// Check if we are back at (or never left) the start node, and have nowhere to go.
if (x == 0 && y == 0)
{
found = false;
break;
}
// Move to previous position and take a different role, if available.
newPos = path.top();
x = newPos.first;
y = newPos.second;
path.pop();
}
return found;
}
int wire_burnouts(int N, const vector<int>& A, const vector<int>& B, const vector<int>& C)
{
if (N == 1)
return -1;
vector< vector<long long> > mesh(N, vector<long long>(N, 0));
size_t vecSize = A.size();
long long highPos;
long long lowPos;
// Apply each burnout to the grid along with the time it happens.
for (size_t i = 0; i < vecSize; ++i)
{
if (C[i] == 1)
{
lowPos = i;
lowPos <<= 3;
mesh[A[i]][B[i]] |= lowPos;
mesh[A[i]][B[i]] |= 2; // Right
}
else
{
highPos = i;
highPos <<= 23;
mesh[A[i]][B[i]] |= highPos;
mesh[A[i]][B[i]] |= 4; // Up
}
// If either corner completely burns out this would immediately stop the current.
//if ((mesh[0][0] & 6) == 6 || ((mesh[N - 2][N - 1] & 2) == 2 && (mesh[N - 1][N - 2] & 4) == 4))
// return i + 1;
}
size_t start = 0;
size_t end = vecSize - 1;
size_t mid = vecSize / 2;
size_t breakPos;
bool currentOK;
while (true)
{
// Check if current can still flow.
if (currentOK = Explore(mesh, mid, N))
{
if (mid != end)
start = mid + 1;
else
break;
}
else
{
breakPos = mid;
if (mid != start)
{
end = mid - 1;
}
else
break;
}
mid = start + ((end - start) / 2);
// Clear all processed flags for next pass.
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
mesh[i][j] &= 0xFFFFFFFFFFFFFFFE;
}
if (currentOK && mid == vecSize - 1)
return -1;
else
return breakPos + 1;
}
#include <algorithm>
#include <vector>
#include <stack>
bool Explore(vector< vector<long long> >& mesh, int seconds, int N)
{
bool found = false;
stack< pair<int, int> > path;
int x = 0;
int y = 0;
pair<int, int> newPos;
while (true)
{
//cout << x << "," << y << endl;
// Check if we are at the end node.
if (x == N - 1 && y == N - 1)
{
found = true;
break;
}
// Set this node as 'processed'.
mesh[x][y] |= 1;
if (x != N - 1 && ((mesh[x + 1][y] & 1) == 0) &&
(((mesh[x][y] >> 3) & 0xFFFFF) > seconds || (mesh[x][y] & 2) == 0)) // Right
{
path.push(make_pair(x, y));
x += 1;
continue;
}
else if (y != N - 1 && ((mesh[x][y + 1] & 1) == 0) &&
((mesh[x][y] >> 23) > seconds || (mesh[x][y] & 4) == 0)) // Up
{
path.push(make_pair(x, y));
y += 1;
continue;
}
else if (x != 0 && ((mesh[x - 1][y] & 1) == 0) &&
(((mesh[x - 1][y] >> 3) & 0xFFFFF) > seconds || (mesh[x - 1][y] & 2) == 0)) // Left
{
path.push(make_pair(x, y));
x -= 1;
continue;
}
else if (y != 0 && ((mesh[x][y - 1] & 1) == 0) &&
((mesh[x][y - 1] >> 23) > seconds || (mesh[x][y - 1] & 4) == 0)) // Down
{
path.push(make_pair(x, y));
y -= 1;
continue;
}
// Check if we are back at (or never left) the start node, and have nowhere to go.
if (x == 0 && y == 0)
{
found = false;
break;
}
// Move to previous position and take a different role, if available.
newPos = path.top();
x = newPos.first;
y = newPos.second;
path.pop();
}
return found;
}
int wire_burnouts(int N, const vector<int>& A, const vector<int>& B, const vector<int>& C)
{
if (N == 1)
return -1;
vector< vector<long long> > mesh(N, vector<long long>(N, 0));
size_t vecSize = A.size();
long long highPos;
long long lowPos;
// Apply each burnout to the grid along with the time it happens.
for (size_t i = 0; i < vecSize; ++i)
{
if (C[i] == 1)
{
lowPos = i;
lowPos <<= 3;
mesh[A[i]][B[i]] |= lowPos;
mesh[A[i]][B[i]] |= 2; // Right
}
else
{
highPos = i;
highPos <<= 23;
mesh[A[i]][B[i]] |= highPos;
mesh[A[i]][B[i]] |= 4; // Up
}
// If either corner completely burns out this would immediately stop the current.
//if ((mesh[0][0] & 6) == 6 || ((mesh[N - 2][N - 1] & 2) == 2 && (mesh[N - 1][N - 2] & 4) == 4))
// return i + 1;
}
size_t start = 0;
size_t end = vecSize - 1;
size_t mid = vecSize / 2;
size_t breakPos;
bool currentOK;
while (true)
{
// Check if current can still flow.
if (currentOK = Explore(mesh, mid, N))
{
if (mid != end)
start = mid + 1;
else
break;
}
else
{
breakPos = mid;
if (mid != start)
{
end = mid - 1;
}
else
break;
}
mid = start + ((end - start) / 2);
// Clear all processed flags for next pass.
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
mesh[i][j] &= 0xFFFFFFFFFFFFFFFE;
}
if (currentOK && mid == vecSize - 1)
return -1;
else
return breakPos + 1;
}
#include <algorithm>
#include <vector>
#include <stack>
bool Explore(vector< vector<long long> >& mesh, int seconds, int N)
{
bool found = false;
stack< pair<int, int> > path;
int x = 0;
int y = 0;
pair<int, int> newPos;
while (true)
{
//cout << x << "," << y << endl;
// Check if we are at the end node.
if (x == N - 1 && y == N - 1)
{
found = true;
break;
}
// Set this node as 'processed'.
mesh[x][y] |= 1;
if (x != N - 1 && ((mesh[x + 1][y] & 1) == 0) &&
(((mesh[x][y] >> 3) & 0xFFFFF) > seconds || (mesh[x][y] & 2) == 0)) // Right
{
path.push(make_pair(x, y));
x += 1;
continue;
}
else if (y != N - 1 && ((mesh[x][y + 1] & 1) == 0) &&
((mesh[x][y] >> 23) > seconds || (mesh[x][y] & 4) == 0)) // Up
{
path.push(make_pair(x, y));
y += 1;
continue;
}
else if (x != 0 && ((mesh[x - 1][y] & 1) == 0) &&
(((mesh[x - 1][y] >> 3) & 0xFFFFF) > seconds || (mesh[x - 1][y] & 2) == 0)) // Left
{
path.push(make_pair(x, y));
x -= 1;
continue;
}
else if (y != 0 && ((mesh[x][y - 1] & 1) == 0) &&
((mesh[x][y - 1] >> 23) > seconds || (mesh[x][y - 1] & 4) == 0)) // Down
{
path.push(make_pair(x, y));
y -= 1;
continue;
}
// Check if we are back at (or never left) the start node, and have nowhere to go.
if (x == 0 && y == 0)
{
found = false;
break;
}
// Move to previous position and take a different role, if available.
newPos = path.top();
x = newPos.first;
y = newPos.second;
path.pop();
}
return found;
}
int wire_burnouts(int N, const vector<int>& A, const vector<int>& B, const vector<int>& C)
{
if (N == 1)
return -1;
vector< vector<long long> > mesh(N, vector<long long>(N, 0));
size_t vecSize = A.size();
long long highPos;
long long lowPos;
// Apply each burnout to the grid along with the time it happens.
for (size_t i = 0; i < vecSize; ++i)
{
if (C[i] == 1)
{
lowPos = i;
lowPos <<= 3;
mesh[A[i]][B[i]] |= lowPos;
mesh[A[i]][B[i]] |= 2; // Right
}
else
{
highPos = i;
highPos <<= 23;
mesh[A[i]][B[i]] |= highPos;
mesh[A[i]][B[i]] |= 4; // Up
}
// If either corner completely burns out this would immediately stop the current.
//if ((mesh[0][0] & 6) == 6 || ((mesh[N - 2][N - 1] & 2) == 2 && (mesh[N - 1][N - 2] & 4) == 4))
// return i + 1;
}
size_t start = 0;
size_t end = vecSize - 1;
size_t mid = vecSize / 2;
size_t breakPos;
bool currentOK;
while (true)
{
// Check if current can still flow.
if (currentOK = Explore(mesh, mid, N))
{
if (mid != end)
start = mid + 1;
else
break;
}
else
{
breakPos = mid;
if (mid != start)
{
end = mid - 1;
}
else
break;
}
mid = start + ((end - start) / 2);
// Clear all processed flags for next pass.
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
mesh[i][j] &= 0xFFFFFFFFFFFFFFFE;
}
if (currentOK && mid == vecSize - 1)
return -1;
else
return breakPos + 1;
}