4 4 5 10 2 1 4 2 3 3 2 4 4 3 1 2 4 3 4 4 4
0 5 0 3 0 2 1 3 0 1
第一种方法是枚举的时间,效率较低,比赛时跑了265ms。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<map> #include<set> #include<cmath> #define ll long long using namespace std; int jl[1110]; int r,c,n,t; struct node { int x,y,d; }; int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; int Map[110][110]; struct node2 { int water,time; }; node2 ans [110][110]; int in(int x,int y) { if(x>=1&&x<=r&&y>=1&&y<=c) return 1; return 0; } queue<node> q; queue<node> q2; int bfs( ) { node next,tmp,fen; int xx,yy; for(int ttt = 0; ttt<=t; ttt++) { // for(int i=1; i<=r; i++) // { // for(int j=1; j<=c; j++) // { // cout<<Map[i][j]<<" "; // } // cout<<endl; // } // cout<<endl; for(int i=0; i<n; i++) { xx = jl[i]/1000, yy = jl[i]%1000; if(Map[xx][yy]>4) { Map[xx][yy] = 0; ans[xx][yy].water = 0; ans[xx][yy].time = ttt; for(int j=0; j<4; j++) { fen.x = xx,fen.y = yy,fen.d = j; q.push(fen); } } else { ans[xx][yy].water = Map[xx][yy]; } } while(!q.empty()) { node tmp = q.front(); q.pop(); int xx = tmp.x + dir[tmp.d][0]; int yy = tmp.y + dir[tmp.d][1]; if(!in(xx,yy)) continue; if(Map[xx][yy]==0) { next.x = xx; next.y = yy; next.d = tmp.d; q2.push(next); } else Map[xx][yy] ++; } while(!q2.empty()) { q.push(q2.front()); q2.pop(); } } return 0; } int main() { while(cin>>r>>c>>n>>t) { memset(Map,0,sizeof(Map)); for(int i=0; i<n; i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); jl[i] = x*1000+y; ans[x][y].water = z; ans[x][y].time = 0; Map[x][y] += z; } int sx,sy; cin>>sx>>sy; while(!q.empty()) q.pop(); while(!q2.empty()) q.pop(); node tmp; for(int i=0; i<4; i++) { tmp.x = sx,tmp.y = sy; tmp.d = i; q.push(tmp); } bfs(); int ex,ey; node2 flag ; for(int i=0; i<n; i++) { ex = jl[i]/1000; ey = jl[i]%1000; flag = ans[ex][ey]; if(flag.water == 0) cout<<0<<" "<<flag.time<<endl; else cout<<1<<" "<<flag.water<<endl; } } return 0; }
第二种方法只需把当前这一秒所有的小水珠都处理完再判断是否爆裂。赛后跑了46ms。
据说是赛前该小了数据,要不然第一种方法就会T掉。
</pre><pre name="code" class="cpp">#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #include<map> #include<set> #include<cmath> #define ll long long using namespace std; int jl[1110]; int r,c,n,t; struct node { int x,y,d; int mov; }; int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; int Map[110][110]; struct node2 { int water,time; }; node2 ans [110][110]; int in(int x,int y) { if(x>=1&&x<=r&&y>=1&&y<=c) return 1; return 0; } queue<node> q; int bfs( ) { node next,tmp,fen; int xx,yy; while(!q.empty()&&q.front().mov<t) { node tmp = q.front(); q.pop(); int xx = tmp.x + dir[tmp.d][0]; int yy = tmp.y + dir[tmp.d][1]; // if(in(xx,yy)) continue; //如果后面还有判断条件 ,不要这样写 if(in(xx,yy)) { if(Map[xx][yy]==0) { next.x = xx; next.y = yy; next.d = tmp.d; next.mov = tmp.mov + 1; q.push(next); } else Map[xx][yy] ++; } if(q.empty()||q.front().mov != tmp.mov) { for(int i=0; i<n; ++i) { xx = jl[i]/1000, yy = jl[i]%1000; if(Map[xx][yy]>4) { Map[xx][yy] = 0; ans[xx][yy].water = 0; ans[xx][yy].time = tmp.mov + 1; for(int j=0; j<4; ++j) { fen.x = xx,fen.y = yy,fen.d = j; fen.mov = tmp.mov + 1; q.push(fen); } } else ans[xx][yy].water = Map[xx][yy]; } } } return 0; } int main() { while(scanf("%d%d%d%d",&r,&c,&n,&t)!=EOF) { memset(Map,0,sizeof(Map)); for(int i=0; i<n; ++i) { int x,y,z; scanf("%d%d%d",&x,&y,&z); jl[i] = x*1000+y; ans[x][y].water = z; ans[x][y].time = 0; Map[x][y] += z; } int sx,sy; scanf("%d%d",&sx,&sy); while(!q.empty()) q.pop(); node tmp; for(int i=0; i<4; ++i) { tmp.x = sx,tmp.y = sy; tmp.d = i; tmp.mov = 0; q.push(tmp); } bfs(); int ex,ey; node2 flag ; for(int i=0; i<n; ++i) { ex = jl[i]/1000; ey = jl[i]%1000; flag = ans[ex][ey]; if(flag.water == 0) printf("0 %d\n",flag.time); else printf("1 %d\n",flag.water); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013588639/article/details/47167943