Skip to content

Instantly share code, notes, and snippets.

@blahgeek
Created April 4, 2013 12:30
Show Gist options
  • Save blahgeek/5309980 to your computer and use it in GitHub Desktop.
Save blahgeek/5309980 to your computer and use it in GitHub Desktop.
/// @file main.cpp @version 1.0 @date 04/04/2013
/// @author [email protected]
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
int row, col;
int value;
Node * down, * right;
Node(int x, int y):
row(x), col(y), down(this), right(this){}
};
struct CrossMatrix{
Node * root;
CrossMatrix(): root(new Node(0, 0)) {}
void insert(int x, int y, int n){
Node * p = root;
for(int i = 0 ; i < x; i += 1, p = p->down){
if(p->down != root) continue;
p->down = new Node(i+1, 0);
p->down->down = root;
}
while(p->right->col < y && (p->right->col != 0))
p = p->right;
Node * tmp = p->right;
p->right = new Node(x, y);
Node * target = p->right;
p->right->right = tmp;
p = root;
for(int i = 0 ; i < y; i += 1, p = p->right){
if(p->right != root) continue;
p->right = new Node(0, i+1);
p->right->right = root;
}
while(p->down->row < x && (p->down->row != 0))
p = p->down;
target->down = p->down;
p->down = target;
target->value = n;
}
void print(){
Node * p = root->down;
while(p != root){
Node * pp = p->right;
int last = p->col;
while(pp != p){
for(int i = 1 ; i < (pp->col - last) ; i += 1)
cout << 0 << " ";
last = pp->col;
cout << pp->value << " ";
pp = pp->right;
}
cout << endl;
p = p->down;
}
}
};
int main ( int argc, char *argv[] )
{
CrossMatrix m;
m.insert(3, 4, 100);
m.insert(5, 8, 100);
m.insert(5, 7, 100);
m.insert(9, 2, 100);
m.print();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment