CodeChef submission 61897 (C++ 4.0.0-8) plaintext list. Status: AC, problem B1, contest . By triplem (Stephen Merriman), 2009-08-01 05:49:04.
#include <iostream> #include <list> #include <queue> using namespace std; char grid[1000][1001]; int dist[1000][1000]; int delta[8][2] = {{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}}; list<int> ds; typedef struct State { int x, y, val; bool seen; bool operator<(const State& s) const { return val<s.val; } }; priority_queue<State> pq; State states[1000][1000]; int main() { int sx,sy,ex,ey; for (int i=0; i<N; i++) { for (int j=0; j<M; j++) { states[i][j].x=i; states[i][j].y=j; states[i][j].val=10000; states[i][j].seen=false; if (grid[i][j]=='@') { sx=i;sy=j;grid[i][j]='.'; } else if (grid[i][j]=='$') { ex=i;ey=j;grid[i][j]='.'; } else if (grid[i][j]=='D') { ds.push_back(i*M+j); states[i][j].val=0; } } } int score = 0; while (true) { score++; int sz = ds.size(); if (sz==0) break; for (int i=0; i<sz; i++) { int next = ds.front(); int x = next/M; int y = next%M; ds.pop_front(); for (int j=0; j<8; j++) { int xx = x + delta[j][0]; int yy = y + delta[j][1]; if (xx>=0 && yy>=0 && xx<N && yy<M && states[xx][yy].val==10000) { states[xx][yy].val=score; ds.push_back(xx*M+yy); } } } } pq.push(states[sx][sy]); states[sx][sy].seen=true; int curans = 10000; while (true) { State next = pq.top(); pq.pop(); curans<?=next.val; if (next.x==ex && next.y==ey) break; for (int i=0; i<8; i++) { int xx = next.x + delta[i][0]; int yy = next.y + delta[i][1]; if (xx>=0 && yy>=0 && xx<N && yy<N && !states[xx][yy].seen) { states[xx][yy].seen=true; pq.push(states[xx][yy]); } } } }
Comments

