#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 added = 0; int w = 1; void set(int x, int y, int v) { if (w==x) { cout << w-1 << ' ' << added << endl; w++; } int o = get(x, y); sandpile[x+WIDTH][y+WIDTH] = v; if (o=MAX) { xx.push(x); yy.push(y); } } int main() { for (int x=-WIDTH; x<=+WIDTH; x++) { for (int y=-WIDTH; y<=+WIDTH; y++) { sandpile[x+WIDTH][y+WIDTH] = 2; } } for (int n=1; w<=10000; n++) { set(0, 0, get(0, 0) + 1); added++; 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