traverse in vector<vector<pair<int, int> > > - c++

I came across a situation that I couldn't explain why. I have a vector<vector<pair<int, int> > > parent that indicates parent of a specific cell. I have following code:
// (1, 0), (1, 2), (1, 2)
// (1, 2), (-1, -1), (1, 2)
// (-1, -1), (2, 1), (-1, -1)
vector<vector<pair<int, int> > > nums = {
{make_pair(1, 0), make_pair(1, 2), make_pair(1, 2)},
{make_pair(1, 2), make_pair(-1, -1), make_pair(1, 2)},
{make_pair(-1, -1), make_pair(2, 1), make_pair(-1, -1)}};
int r = 0;
int c = 1;
while (nums[r][c] != make_pair(r, c)) {
cout << nums[r][c].first << " " << nums[r][c].second << endl; // 1, 2
r = nums[r][c].first; // 1
c = nums[r][c].second; // -1
cout << "r: " << r << " c: " << c << endl;
}
I 'm not sure why in the first iteration of while loop for c = nums[r][c].second; it returns -1 instead of 2.

In first iteration,
r=num[0][1].first = 1.
Therefore
c=num[1][1].second = - 1.

Related

How to map char values to int using enum

I am trying to map some chars in a string to some integer values using enum. Please tell where am I going wrong?
enum moves{U,R,D,L};
class Solution {
public:
bool judgeCircle(string moves) {
// moves is a string having values like ULLDDRR, ULRD, UULLDDRR
int X[] = {0,1,0,-1};
int Y[] = {1,0,-1,0};
// while iterating the string if I get a 'U' , I want to use it as an index
//with U representing the 0th index, R as index=1 and so on.. as specified
//in the enum
int x=0 , y=0;
enum moves ind;
for( int i = 0 ; i < moves.length() ; i++ ) {
ind = moves[i]; // but this line here gives error
x += X[ind];
y += Y[ind];
}
if(!x && !y)
return true;
else
return false;
}
};
I would drop the idea with an enum because I feel it has no use for the actual problem – to map characters to navigation moves. For this, I would use a std::map or a std::unordered_map. (Considering, that there are 4 entries only, the performance difference is probably hard to measure.)
While I was preparing a sample code, πάντα ῥεῖ gave a similar hint. Though, I would even recommend to bundle x and y of moves together:
#include <map>
#include <iomanip>
#include <iostream>
// bundle x and y for a move (which needs both of them)
struct Move {
int dx, dy;
};
// a type to map chars to moves
using MoveMap = std::map<char, Move>;
// a pre-defined move map
static const MoveMap mapMoves = {
{ 'U', { 0, 1 } },
{ 'R', { 1, 0 } },
{ 'D', { 0, -1 } },
{ 'L', { -1, 0 } }
};
/* a function to use move map
*
* id ... one of U R D L
* x, y ... coordinates (update)
* return: true if successful, (false e.g. for wrong id)
*/
bool move(char id, int &x, int &y)
{
const MoveMap::const_iterator iter = mapMoves.find(id);
return iter != mapMoves.end()
? x += iter->second.dx, y += iter->second.dy, true
: false;
}
// check it out:
int main()
{
int x = 0, y = 0;
const char test[] = "ULLDDRR, ULRD, UULLDDRR";
for (char id : test) {
std::cout << "(" << x << ", " << y << "): "
<< "Move '" << id << "' -> ";
if (move(id, x, y)) {
std::cout << "(" << x << ", " << y << ")\n";
} else std::cout << "failed\n";
}
return 0;
}
Output:
(0, 0): Move 'U' -> (0, 1)
(0, 1): Move 'L' -> (-1, 1)
(-1, 1): Move 'L' -> (-2, 1)
(-2, 1): Move 'D' -> (-2, 0)
(-2, 0): Move 'D' -> (-2, -1)
(-2, -1): Move 'R' -> (-1, -1)
(-1, -1): Move 'R' -> (0, -1)
(0, -1): Move ',' -> failed
(0, -1): Move ' ' -> failed
(0, -1): Move 'U' -> (0, 0)
(0, 0): Move 'L' -> (-1, 0)
(-1, 0): Move 'R' -> (0, 0)
(0, 0): Move 'D' -> (0, -1)
(0, -1): Move ',' -> failed
(0, -1): Move ' ' -> failed
(0, -1): Move 'U' -> (0, 0)
(0, 0): Move 'U' -> (0, 1)
(0, 1): Move 'L' -> (-1, 1)
(-1, 1): Move 'L' -> (-2, 1)
(-2, 1): Move 'D' -> (-2, 0)
(-2, 0): Move 'D' -> (-2, -1)
(-2, -1): Move 'R' -> (-1, -1)
(-1, -1): Move 'R' -> (0, -1)
(0, -1): Move '' -> failed
Live Demo on coliru

How to access edge_descriptor with given vertex_descriptor in boost::grid_graph

I'm trying to change the weight of edges in my grid_graph but fail to access the edge_descriptor with:
std::pair<bEdgeDescriptor, bool> ed_right = boost::edge(bVertexDescriptor {{i - 1, j, k}}, bVertexDescriptor {{i, j, k}}, grid);
where grid is boost::grid_graph<3>
Then I find out the grid_graph does not support this. In order to make use of the astar_search, is there any convenient way to access edge_descriptor with given vertex_descriptor in boost::grid_graph but not adjacency_list?
You can get the in/out edges for any particular node:
http://www.boost.org/doc/libs/1_63_0/libs/graph/doc/grid_graph.html#indexing
// Get the out-edge associated with vertex and out_edge_index
Traits::edge_descriptor
out_edge_at(Traits::vertex_descriptor vertex,
Traits::degree_size_type out_edge_index,
const Graph& graph);
// Get the out-edge associated with vertex and in_edge_index
Traits::edge_descriptor
in_edge_at(Traits::vertex_descriptor vertex,
Traits::degree_size_type in_edge_index,
const Graph& graph);
Demo using a 3d 4x4x4 grid with non-wrapping dimensions (this show-cases that the degree of nodes on the edges is lower):
Live On Coliru
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
using Grid = boost::grid_graph<3>;
using Traits = boost::graph_traits<Grid>;
using vertex_descriptor = Grid::vertex_descriptor;
using edge_descriptor = Grid::edge_descriptor;
static inline std::ostream& operator<<(std::ostream& os, vertex_descriptor const& vd) {
return os << "(" << vd[0] << ", " << vd[1] << ", " << vd[2] << ")";
}
void print_in_edges(vertex_descriptor vd, Grid const& grid) {
for (Traits::degree_size_type ei = 0; ei < in_degree(vd, grid); ++ei) {
auto ed_left = in_edge_at(vd, ei, grid);
std::cout << "Detected in edge: " << ed_left.first << " -> " << ed_left.second << "\n";
}
}
void print_out_edges(vertex_descriptor vd, Grid const& grid) {
for (Traits::degree_size_type ei = 0; ei < out_degree(vd, grid); ++ei) {
auto ed_left = out_edge_at(vd, ei, grid);
std::cout << "Detected out edge: " << ed_left.first << " -> " << ed_left.second << "\n";
}
}
int main() {
Grid grid({ { 4, 4, 4 } }, false);
print_in_edges({{ 2, 2, 2 } }, grid);
print_out_edges({{ 2, 2, 2 } }, grid);
std::cout << "----\n";
print_in_edges({{ 0, 0, 0 } }, grid);
print_out_edges({{ 0, 0, 0 } }, grid);
}
Prints:
Detected in edge: (1, 2, 2) -> (2, 2, 2)
Detected in edge: (3, 2, 2) -> (2, 2, 2)
Detected in edge: (2, 1, 2) -> (2, 2, 2)
Detected in edge: (2, 3, 2) -> (2, 2, 2)
Detected in edge: (2, 2, 1) -> (2, 2, 2)
Detected in edge: (2, 2, 3) -> (2, 2, 2)
Detected out edge: (2, 2, 2) -> (1, 2, 2)
Detected out edge: (2, 2, 2) -> (3, 2, 2)
Detected out edge: (2, 2, 2) -> (2, 1, 2)
Detected out edge: (2, 2, 2) -> (2, 3, 2)
Detected out edge: (2, 2, 2) -> (2, 2, 1)
Detected out edge: (2, 2, 2) -> (2, 2, 3)
----
Detected in edge: (1, 0, 0) -> (0, 0, 0)
Detected in edge: (0, 1, 0) -> (0, 0, 0)
Detected in edge: (0, 0, 1) -> (0, 0, 0)
Detected out edge: (0, 0, 0) -> (1, 0, 0)
Detected out edge: (0, 0, 0) -> (0, 1, 0)
Detected out edge: (0, 0, 0) -> (0, 0, 1)

How to count connected cells in a grid?

I'm trying to solve the Hackerrank problem "Connected Cells in a Grid". The task is to find the largest region (connected cells consisting of ones) in the grid.
My approach was to add the number of ones I find only if the element hasn't been visited yet, then I take the maximum of several paths. It doesn't seem to be working for the following test case:
5
5
1 1 0 0 0
0 1 1 0 0
0 0 1 0 1
1 0 0 0 1
0 1 0 1 1
Is there something wrong with my approach?
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 10
bool visited[MAX][MAX];
int maxRegion(vector<vector<int>> const& mat, int i, int j) {
int result;
if ((i == 0 && j == 0) || visited[i][j]) {
result = 0;
}
else if (i == 0) {
result = mat[i][j-1] + maxRegion(mat, i, j-1);
}
else if (j == 0) {
result = mat[i-1][j] + maxRegion(mat, i-1, j);
}
else {
result = mat[i-1][j-1] +
max({maxRegion(mat, i-1, j),
maxRegion(mat, i, j-1),
maxRegion(mat, i-1, j-1)});
}
visited[i][j] = true;
return result;
}
I think it's very natural to formulate this program as a connected components problem. Specifically, I've used boost::graph for this.
The idea is to build a graph whose each entry in the matrix is a node, and there are edges between horizontal and vertical 1 entries. Once such a graph is built, all that is needed is to run the connected components algorithm, and find the biggest component.
The following code does so:
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
using namespace std;
using namespace boost;
int main()
{
vector<vector<int>> v{{1, 1, 0, 0, 0}, {0, 1, 1, 0, 0}, {0, 0, 1, 0, 1}, {1, 0, 0, 0, 1}, {0, 1, 0, 1, 1}};
typedef adjacency_list <vecS, vecS, undirectedS> graph;
graph g(v.size() * v.size());
// Populate the graph edges
for(size_t i = 0; i < v.size() - 1; ++i)
for(size_t j = 0; j < v[i].size() - 1; ++j)
{
if(v[i][j] == 1 && v[i + 1][j] == 1)
add_edge(i * v.size() + j, (i + 1) * v.size() + j, g);
else if(v[i][j] == 1 && v[i][j + 1] == 1)
add_edge(i * v.size() + j, i * v.size() + j + 1, g);
}
// Run the connected-components algorithm.
vector<int> component(num_vertices(g));
int num = connected_components(g, &component[0]);
// Print out the results.
std::vector<int>::size_type i;
for(i = 0; i != component.size(); ++i)
cout << "Vertex (" << i / v.size() << ", " << i % v.size() << ") is in component " << component[i] << endl;
cout << endl;
}
The output is
Vertex (0, 0) is in component 0
Vertex (0, 1) is in component 0
Vertex (0, 2) is in component 1
Vertex (0, 3) is in component 2
Vertex (0, 4) is in component 3
Vertex (1, 0) is in component 4
Vertex (1, 1) is in component 0
Vertex (1, 2) is in component 0
Vertex (1, 3) is in component 5
Vertex (1, 4) is in component 6
Vertex (2, 0) is in component 7
Vertex (2, 1) is in component 8
Vertex (2, 2) is in component 0
Vertex (2, 3) is in component 9
Vertex (2, 4) is in component 10
Vertex (3, 0) is in component 11
Vertex (3, 1) is in component 12
Vertex (3, 2) is in component 13
Vertex (3, 3) is in component 14
Vertex (3, 4) is in component 15
Vertex (4, 0) is in component 16
Vertex (4, 1) is in component 17
Vertex (4, 2) is in component 18
Vertex (4, 3) is in component 19
Vertex (4, 4) is in component 20
Note that the program encodes i, j (for the case where the dimension is 5) by 5 i + j. This is easily invertible.
You can represent the matrix as an undirected graph and use DFS or BFS to find the connected component with the most nodes: every cell containing 1 can become a node, and there is an edge between two nodes if the corresponding cells are adjacent.
If you still need some guidance with the solution, here is mine in Python - passed all tests :) (visit my github to see other challenges that I've solved there in C++ as well)
def getBiggestRegion(grid, n, m):
max_region = 0
region_size = 0
for i in xrange(n):
for j in xrange(m):
if grid[i][j] == 1:
region_size = mark_region(grid, i, j, n, m)
#region_size += 1
if region_size > max_region:
max_region = region_size
return max_region
def push_if_valid(stack, i, j, n, m):
if 0 <= i < n and 0 <= j < m:
stack.append((i, j))
dirs = [[1,0], [0,1], [-1,0], [0,-1], [-1,-1], [-1, 1], [1,1], [1, -1]]
def mark_region(grid, i, j, n, m):
stack = []
stack.append((i, j))
region_size = 0
while stack:
curr = stack.pop()
ci = curr[0]
cj = curr[1]
if grid[ci][cj] == 1:
grid[ci][cj] = 2
region_size += 1
#this for loop is for going in all the directions
#North, South, East, West, NW, SW, SE, NE
#in my C++ Pacman sol, I have the actual lines instead
for dir in dirs:
push_if_valid(stack, ci + dir[0], cj + dir[1], n, m)
return region_size
n = int(raw_input().strip())
m = int(raw_input().strip())
grid = []
for grid_i in xrange(n):
grid_t = list(map(int, raw_input().strip().split(' ')))
grid.append(grid_t)
print(getBiggestRegion(grid, n, m))

How to initialize static member array with a result of a function?

I'm translating such fragment of this Python file to C++:
SIDE = 3
LINES = []
for y in range(SIDE):
row = tuple((x, y) for x in range(SIDE))
LINES.append(row)
for x in range(SIDE):
col = tuple((x, y) for y in range(SIDE))
LINES.append(col)
LINES.append(tuple((x, x) for x in range(SIDE)))
LINES.append(tuple((SIDE - x - 1, x) for x in range(SIDE)))
LINES holds (x, y) coordinates of possible lines in Tic Tac Toe game. So for SIDE = 3 it holds:
[((0, 0), (1, 0), (2, 0)),
((0, 1), (1, 1), (2, 1)),
((0, 2), (1, 2), (2, 2)),
((0, 0), (0, 1), (0, 2)),
((1, 0), (1, 1), (1, 2)),
((2, 0), (2, 1), (2, 2)),
((0, 0), (1, 1), (2, 2)),
((2, 0), (1, 1), (0, 2))]
SIDE value can change.
What I've tried
Performance is crucial (that's why I reached for C++), so I would like to calculate LINES only once. Thus, I've chosen to implement LINES as a static member of the class TicTacToeState.
I started with such code:
static char init_lines() {
return 'a';
}
class TicTacToeState {
static char LINES;
};
char TicTacToeState::LINES = init_lines();
It works. How to change LINES to an array? Maybe vector will be better? With pairs?
Maybe static member is not the best choice, maybe there is an easier way?
How would you translate it to C++?
We know the size of LINES, it's always 2 * SIDE + 2.
Special requirement
All C++ code must be in one .cpp file, no headers. Why? Because this is fragment of a library for bot competitions and it's typical that you can submit only one file.
In C++ you can initialize static array members using group initialization
static int a[10] = {5}; //this will initialize first position item with 5 and rest with 0s
static char b[2] = {'b', 'b'};
static int c[2][2] = { {1,1}, {1,2} };
int main()
{
cout<< a[0] << endl; //output: 5
cout<< a[1] << endl; //output: 0
cout<< b[0] << endl; //output: b
cout<< c[0][1] << endl; //output: 1
}
Although the fact is you need to know size of the array not like in Python's list that are dynamically
If you need to insert to the table values calculated dynamically the best way to do this is to create factory method
static int** fact(int width, int height)
{
int** a;
a = new int*[width]; //we can do it when it is DYNAMIC array!
a[0] = new int[height];
a[1] = new int[height];
for(int i = 0; i < width; i++)
for(int k = 0; k < height; k++)
a[i][k] = i*k;
return a;
}
static int** c = fact(2, 2); //you can call it with your SIDE var
int main()
{
cout<< c[1][1] << endl; //output: 1
}
Of course you can process it in loops
The same approach will be proper when you will decide to use std Vector class which is equvalent of Python's dynamic list
I suppose you could do this using a lambda function like this:
#include <vector>
#include <iostream>
const auto SIDE = 3U;
struct coord
{
unsigned x;
unsigned y;
coord(unsigned x, unsigned y): x(x), y(y) {}
};
static const auto lines = [] // lambda function
{
// returned data structure
std::vector<std::vector<coord>> lines;
for(auto y = 0U; y < SIDE; ++y)
{
lines.emplace_back(); // add a new line to back()
for(auto x = 0U; x < SIDE; ++x)
lines.back().emplace_back(x, y); // add a new coord to that line
}
for(auto x = 0U; x < SIDE; ++x)
{
lines.emplace_back();
for(auto y = 0U; y < SIDE; ++y)
lines.back().emplace_back(x, y);
}
lines.emplace_back();
for(auto i = 0U; i < SIDE; ++i)
lines.back().emplace_back(i, i);
lines.emplace_back();
for(auto i = 0U; i < SIDE; ++i)
lines.back().emplace_back(SIDE - i - 1, i);
return lines;
}(); // NOTE: () is important to run the lambda function
int main()
{
for(auto const& line: lines)
{
std::cout << "(";
for(auto const& coord: line)
std::cout << "(" << coord.x << ", " << coord.y << ")";
std::cout << ")\n";
}
}
Output:
((0, 0)(1, 0)(2, 0))
((0, 1)(1, 1)(2, 1))
((0, 2)(1, 2)(2, 2))
((0, 0)(0, 1)(0, 2))
((1, 0)(1, 1)(1, 2))
((2, 0)(2, 1)(2, 2))
((0, 0)(1, 1)(2, 2))
((2, 0)(1, 1)(0, 2))

Replace whole row using MatSetValuesStencil with INSERT_VALUES

I am using Petsc Ksp routines.
I construct an operator using MatSetValuesStencil, where in each call of this function I specify one row matrix values of length 5.
There is a case where I sometimes need to completely replace a row from a 5 length stencil to a 3 length one. Will INSERT_VALUES mode leave the two values on non changed positions or it will discard them to zero?
The elements of the matrix that are not specified in the arguments idxm and idxn of the function MatSetValuesStencil(...) are left unchanged, even if INSERT_VALUES is used.
Here is a little code starting from ksp_ex29 to test it :
static char help[] = "Does INSERT_VALUES changes the whole row ? No.\n\n";
#include <petscdm.h>
#include <petscdmda.h>
#include <petscksp.h>
extern PetscErrorCode ComputeMatrix42(DM da,Mat jac);
extern PetscErrorCode ComputeMatrix(DM da,Mat jac);
#undef __FUNCT__
#define __FUNCT__ "main"
int main(int argc,char **argv)
{
DM da;
PetscErrorCode ierr;
Mat matrix;
PetscInitialize(&argc,&argv,(char*)0,help);
ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, DM_BOUNDARY_PERIODIC,DMDA_STENCIL_STAR,-3,-3,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);CHKERRQ(ierr);
DMCreateMatrix(da,&matrix);
ComputeMatrix(da,matrix);
PetscPrintf(PETSC_COMM_WORLD,"A matrix of negative terms : \n");
MatView(matrix, PETSC_VIEWER_STDOUT_WORLD );
ComputeMatrix42(da,matrix);
PetscPrintf(PETSC_COMM_WORLD,"The diagonal, i-1 and i+1 are set to 42 : \n");
MatView(matrix, PETSC_VIEWER_STDOUT_WORLD );
ierr = DMDestroy(&da);CHKERRQ(ierr);
ierr = MatDestroy(&matrix);CHKERRQ(ierr);
ierr = PetscFinalize();
return 0;
}
#undef __FUNCT__
#define __FUNCT__ "ComputeMatrix"
PetscErrorCode ComputeMatrix(DM da,Mat jac)
{
PetscErrorCode ierr;
PetscInt i,j,mx,my,xm,ym,xs,ys;
PetscScalar v[5];
MatStencil row, col[5];
PetscFunctionBeginUser;
ierr = DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
ierr = DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr);
for (j=ys; j<ys+ym; j++) {
for (i=xs; i<xs+xm; i++) {
row.i = i; row.j = j;
v[0] = -1; col[0].i = i; col[0].j = j-1;
v[1] = -1; col[1].i = i-1; col[1].j = j;
v[2] = -13; col[2].i = i; col[2].j = j;
v[3] = -1; col[3].i = i+1; col[3].j = j;
v[4] = -1; col[4].i = i; col[4].j = j+1;
ierr = MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "ComputeMatrix42"
PetscErrorCode ComputeMatrix42(DM da,Mat jac)
{
PetscErrorCode ierr;
PetscInt i,j,mx,my,xm,ym,xs,ys;
PetscScalar v[3];
MatStencil row, col[3];
PetscFunctionBeginUser;
ierr = DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
ierr = DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr);
for (j=ys; j<ys+ym; j++) {
for (i=xs; i<xs+xm; i++) {
row.i = i; row.j = j;
v[0] = 42; col[0].i = i-1; col[0].j = j;
v[1] = 42; col[1].i = i; col[1].j = j;
v[2] = 42; col[2].i = i+1; col[2].j = j;
ierr = MatSetValuesStencil(jac,1,&row,3,col,v,INSERT_VALUES);CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
Compile it with the following makefile :
include ${PETSC_DIR}/conf/variables
include ${PETSC_DIR}/conf/rules
main: main.o chkopts
-${CLINKER} -o main main.o ${PETSC_LIB}
${RM} main.o
Output :
A matrix of negative terms :
Mat Object: 1 MPI processes
type: seqaij
row 0: (0, -13) (1, -1) (2, -1) (3, -1) (6, -1)
row 1: (0, -1) (1, -13) (2, -1) (4, -1) (7, -1)
row 2: (0, -1) (1, -1) (2, -13) (5, -1) (8, -1)
row 3: (0, -1) (3, -13) (4, -1) (5, -1) (6, -1)
row 4: (1, -1) (3, -1) (4, -13) (5, -1) (7, -1)
row 5: (2, -1) (3, -1) (4, -1) (5, -13) (8, -1)
row 6: (0, -1) (3, -1) (6, -13) (7, -1) (8, -1)
row 7: (1, -1) (4, -1) (6, -1) (7, -13) (8, -1)
row 8: (2, -1) (5, -1) (6, -1) (7, -1) (8, -13)
The diagonal, i-1 and i+1 are set to 42 :
Mat Object: 1 MPI processes
type: seqaij
row 0: (0, 42) (1, 42) (2, 42) (3, -1) (6, -1)
row 1: (0, 42) (1, 42) (2, 42) (4, -1) (7, -1)
row 2: (0, 42) (1, 42) (2, 42) (5, -1) (8, -1)
row 3: (0, -1) (3, 42) (4, 42) (5, 42) (6, -1)
row 4: (1, -1) (3, 42) (4, 42) (5, 42) (7, -1)
row 5: (2, -1) (3, 42) (4, 42) (5, 42) (8, -1)
row 6: (0, -1) (3, -1) (6, 42) (7, 42) (8, 42)
row 7: (1, -1) (4, -1) (6, 42) (7, 42) (8, 42)
row 8: (2, -1) (5, -1) (6, 42) (7, 42) (8, 42)
I am using PETSC 3.5.2.