以下程序会倒致Stack overflow 为什么?请高手指点
// Farm Irrigation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "vector"
#include "string"
using namespace std;
int M,N;
vector<void*> table;
class block
{
//mark
// 1
//0 2
// 3
// that type a means 01
// b means 12
// c means 23 and so...
public:
int m_id;
block *l;
block *r;
block *u;
block *d;
string pip;
block(char ch,int id)
{
l = NULL;
r = NULL;
u = NULL;
d = NULL;
m_id = id;
construct(ch);
}
int construct(char ch)
{
switch(ch)
{
case 'A':
pip = "01";
break;
case 'B':
pip = "12";
break;
case 'C':
pip = "03";
break;
case 'D':
pip = "02";
break;
case 'E' :
pip = "13";
break;
case 'F':
pip = "02";
break;
case 'G':
pip = "012";
break;
case 'H':
pip = "301";
break;
case 'I':
pip = "230";
break;
case 'J':
pip = "123";
break;
case 'K':
pip = "0123";
break;
}
bool mark = false;
return 0;
}
~block()
{
if(l)
delete l;
if(r)
delete r;
if(u)
delete u;
if(d)
delete d;
table[m_id] = NULL;
}
};
block* get_left(int id);
block* get_top(int id);
bool insert(char ch,int id);
int main(int argc, char* argv[])
{
ifstream fin("input.txt");
while(fin >> M>> N)
{
if( M == -1 || N == -1)
break;
char ch;
block *p;
table.clear();
for( int i = 0 ; i < M*N-1; i++)
{
int id = i;
fin >> ch;
p = new block(ch,i);
table.push_back(p);
if(((block*)table[id])->pip.find('0') != string::npos)
{
p = get_left(id);
if(p && (p->pip.find('2') != string::npos))
{
p->r = (block*)table[id];
((block*)table[id])->l = p;
}
}
if( ((block*)table[id])->pip.find('1') != string::npos)
{
p = get_top(id);
if(p && (p->pip.find('3') != string::npos))
{
p->d = (block*)table[id];
((block*)table[id])->u = p;
}
}
}
int c = 0;
for(vector<block*>::iterator iter = (block**)table.begin(); iter != (block**)table.end(); iter ++)
{
if(*iter)
{
c++;
delete ((block*)*iter);
}
}
cout << c;
}
return 0;
}
bool insert(char ch,int id)
{
block *p;
if(((block*)table[id])->pip.find('0') != string::npos)
{
p = get_left(id);
if(p && (p->pip.find('2') != string::npos))
{
p->r = (block*)table[id];
((block*)table[id])->l = p;
}
}
if( ((block*)table[id])->pip.find('1') != string::npos)
{
p = get_top(id);
if(p && (p->pip.find('3') != string::npos))
{
p->d = (block*)table[id];
((block*)table[id])->u = p;
}
}
return true;
}
block* get_top(int id)
{
if( id+1 > N)
{
id -= N;
}
return (block*)table[id];
}
block* get_left(int id)
{
if( (id)%N >0)
id --;
else
return NULL;
return (block*)table[id];
}
******************************************测试数据如下:******************************
2 2
DK
HF
3 3
ADC
FJK
IHE
-1 -1