#include #include #include #include using namespace std; #define WIDTH 1000 int sandpile[2*WIDTH+1][2*WIDTH+1]; int dx[] = { +1, 0, -1, 0 }; int dy[] = { 0, +1, 0, -1 }; #define MAX (sizeof(dx)/sizeof(dx[0])) // unstable sites stack xx; stack yy; void check(int x, int y) { if (x < -WIDTH || x > +WIDTH || y < -WIDTH || y > +WIDTH) { cerr << "# out of bounds!" << endl; exit (1); } } int get(int x, int y) { check(x, y); return sandpile[x+WIDTH][y+WIDTH]; } int nb = 0; // number of nonempty cells void set(int x, int y, int v) { int o = get(x, y); if (o) { nb--; } sandpile[x+WIDTH][y+WIDTH] = v; if (v) { nb++; } if (o=MAX) { xx.push(x); yy.push(y); } } int main() { memset(sandpile, 0, sizeof(sandpile)); for (int n=0; n<=10000; n++) { cout << n << ' ' << nb << endl; set(0, 0, get(0, 0) + 1); while (!xx.empty()) { int x = xx.top(); xx.pop(); int y = yy.top(); yy.pop(); int v = get(x, y); int d = v / MAX; set(x, y, v - d * MAX); for (int k=0; k