Ponding Water

设每个位置的初始高度是a[i][j],最终可以达到的高度是h[i][j]。首先,对于最外圈的每个位置,a[i][j] = h[i][j]。然后将最外圈每个位置加入优先队列(高度小的元素在对首),取一个位置t出列,遍历t的相邻位置x,如果a[x]<h[t]。那么a[x]能够达到的最大高度就是h[t],否则h[x] = a[x]。按照这种思路bfs即可。

注意结构体的初始化。可以利用结构体的构造函数,从而减小代码量。

   1: #include <iostream>
   2: #include <cstdio>
   3: #include <queue>
   4: #include <algorithm>
   5: #include <cstring>
   6: using namespace std;
   7: struct node{
   8:     int r,c,h;
   9:     node(){};
  10:     node(int xx, int yy, int hh):r(xx),c(yy),h(hh){}
  11:     bool operator < (const node &s) const
  12:     {
  13:         return h>s.h; // 最小值优先
  14:     }
  15: };
  16: const int maxn = 105;
  17: int row,col;
  18: int a[maxn][maxn],h[maxn][maxn];
  19: bool vis[maxn][maxn];
  20: int dx[] = {0,0,1,-1};
  21: int dy[] = {1,-1,0,0};
  22: priority_queue<node> q;
  23: int sum;
  24: int main()
  25: {
  26:     //freopen("test.txt","r",stdin);
  27:     while(scanf("%d%d",&row,&col) != EOF)
  28:     {
  29:         memset(vis,0,sizeof(vis));
  30:         for(int i=0;i<row;++i)
  31:             for(int j=0;j<col;++j)
  32:                 scanf("%d",&a[i][j]);
  33:         memcpy(h,a,sizeof(a));
  34:         for(int i=0;i<col;++i)
  35:         {
  36:             q.push(node(0,i,a[0][i]));
  37:             vis[0][i] = 1;
  38:             q.push(node(row-1,i,a[row-1][i]));
  39:             vis[row-1][i] = 1;
  40:         }
  41:         for(int i=1;i<row-1;++i)
  42:         {
  43:             q.push(node(i,0,a[i][0]));
  44:             vis[i][0] = 1;
  45:             q.push(node(i,col-1,a[i][col-1]));
  46:             vis[i][col-1] = 1;
  47:         }
  48:         int tr,tc;
  49:         //sum = 0;
  50:         while(!q.empty())
  51:         {
  52:             node temp = q.top();
  53:             q.pop();
  54:             for(int i=0;i<4;++i)
  55:             {
  56:                 tr = temp.r + dx[i];
  57:                 tc = temp.c + dy[i];
  58:                 if(tr>0 && tr<row-1 && tc >0 && tc < col-1 && !vis[tr][tc])
  59:                 {
  60:                     if(a[tr][tc] < h[temp.r][temp.c])
  61:                         h[tr][tc] = h[temp.r][temp.c];
  62:                     /*{
  63:                         h[tr][tc] = h[temp.r][temp.c];
  64:                         cout<<tr<<' '<<tc<<' '<<a[tr][tc]<<"->"<<h[tr][tc]<<endl;
  65:                     }*/
  66:                     
  67:                     vis[tr][tc] = 1;
  68:                     q.push(node(tr,tc,h[tr][tc]));
  69:                 }
  70:             }
  71:  
  72:         }
  73:         sum = 0;
  74:         //for(int i=0;i<row;++i)
  75:         //{
  76:         //    for(int j=0;j<col;++j)
  77:         //        cout<<a[i][j]<<' ';
  78:         //    cout<<endl;
  79:         //}
  80:         //cout<<endl;
  81:         //for(int i=0;i<row;++i)
  82:         //{
  83:         //    for(int j=0;j<col;++j)
  84:         //        cout<<h[i][j]<<' ';
  85:         //    cout<<endl;
  86:         //}
  87:         for(int i=0;i<row;++i)
  88:             for(int j=0;j<col;++j)
  89:                 sum+=h[i][j] - a[i][j];
  90:         printf("%d\n",sum);
  91:  
  92:  
  93:     }
  94: }
posted on 2012-03-14 15:56  bovine  阅读(263)  评论(0编辑  收藏  举报