diamond coordinate

Aug 29, 2015



/*
 * Author:  zinber
 * Created Time:  2015/8/29 15:06:27
 * File Name: diamond.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;

vector< vector<int> > vec;
#define wrapRowAndCol(row,col) ((row << 8) | col)
#define getRow(wrap) ((wrap >> 8) & 0xff)
#define getCol(wrap) (wrap & 0xff)

void genDiamond(int row, int col, int width){
    vec.resize(width);
    int dir[4][2] = {
        { 0,-1 },
        { 0,1  },
        { -1,0 },
        { 1,0  },
    };
    set<int> st,tmpSt;
    st.insert(wrapRowAndCol(row,col));
    tmpSt = st;
    int tmpRow = 0;
    int tmpCol = 0;
    int wrap = 0;
    for(int i = 0; i < width; i++){
        for(set<int>::iterator it = st.begin(); it != st.end(); it++){
           cout<<*it<<endl;
            tmpRow = getRow(*it);
            tmpCol = getCol(*it);
           for( int j = 0; j < 4; j++){
                wrap = wrapRowAndCol(tmpRow + dir[j][0],tmpCol + dir[j][1]);
                if (tmpSt.find(wrap) == tmpSt.end()){
                    vec[i].push_back(wrap);
                    tmpSt.insert(wrap);
                }
           }
        }
        st = tmpSt; 
    }
}
int main() {
    genDiamond(5,5,3);
    for(int i = 0; i < vec.size(); i++){
        for(int j = 0; j < vec[i].size(); j++){
            cout<<"row:"<<getRow(vec[i][j])<<" col:"<<getCol(vec[i][j])<<endl;
        }
    }
    return 0;
}