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
/*******************************************************************************/
此题的原型应是“十滴水”这个游戏,
http://www.3366.com/game/1000154.shtml
原先的想法是利用优先队列,按照时间的先后将水滴依次放进队列
但是一直WA,原因在于
①同一时间到达同一个水滴的问题;
INPUT
4 4 5 100
1 1 4
1 2 3
1 3 4
1 4 4
2 3 4
2 4
OUTPUT
1 4
1 4
0 2
0 1
0 1
②需要多个不同时间到达才能炸开的处理问题。
例子:
INPUT
5 5 4 100
1 1 3
1 2 4
1 4 4
1 5 3
1 3
OUTPUT
0 4
0 1
0 1
0 4
在此需要提及的一点是多校的Contest Clarifications里admin说c是x轴的方向范围,r是y轴的方向范围,但是多校的数据里全是c==r的情况
可能多校结束之后又添了数据,但是r,c搞错了,变成了r是x轴的范围,c是y轴的范围
以下是原先的错误代码:
/*注意:同时到达同一个水滴的情况*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<math.h> #include<vector> #include<map> #include<set> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; struct node { int time,id; bool operator < (const node &a) const { return time>a.time;//最小值优先 } }; int x[105],y[105],z[105],zz[105],v[105][105],s[105][105]; bool vv[105]; int main() { int i,j,r,c,n,t,a,b,k,ans; node w; while(~scanf("%d%d%d%d",&r,&c,&n,&t)) { priority_queue<node> q; memset(v,0,sizeof(v)); memset(s,0,sizeof(s)); memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); memset(z,0,sizeof(z)); memset(zz,0,sizeof(zz)); memset(vv,false,sizeof(vv)); vector<int> p[105][5]; for(i=1;i<=n;i++) { scanf("%d%d%d",&x[i],&y[i],&z[i]); v[x[i]][y[i]]=i; } for(i=1;i<=n;i++) { for(j=x[i]+1;j<=r;j++) if(v[j][y[i]]) p[i][1].push_back(v[j][y[i]]); for(j=x[i]-1;j>=0;j--) if(v[j][y[i]]) p[i][2].push_back(v[j][y[i]]); for(j=y[i]+1;j<=c;j++) if(v[x[i]][j]) p[i][3].push_back(v[x[i]][j]); for(j=y[i]-1;j>=0;j--) if(v[x[i]][j]) p[i][4].push_back(v[x[i]][j]); } scanf("%d%d",&a,&b); if(v[a][b]) { zz[v[a][b]]=0; vv[v[a][b]]=true; v[a][b]=0; //printf("%d\n",v[a][b]); } x[0]=a,y[0]=b; for(j=a+1;j<=r;j++) if(v[j][b]) p[0][1].push_back(v[j][b]); for(j=a-1;j>=0;j--) if(v[j][b]) p[0][2].push_back(v[j][b]); for(j=b+1;j<=c;j++) if(v[a][j]) p[0][3].push_back(v[a][j]); for(j=b-1;j>=0;j--) if(v[a][j]) p[0][4].push_back(v[a][j]); w.id=0;w.time=0;ans=0; q.push(w); while(!q.empty()) { k=q.top().id; ans=q.top().time;printf("%d %d %d##\n",ans,x[k],y[k]); for(i=1;i<5;i++) { for(j=0;j<p[k][i].size();j++) { if(!v[x[p[k][i][j]]][y[p[k][i][j]]]&&ans+abs(x[p[k][i][j]]-x[k])+abs(y[p[k][i][j]]-y[k])==s[x[p[k][i][j]]][y[p[k][i][j]]]) break; if(v[x[p[k][i][j]]][y[p[k][i][j]]]&&ans+abs(x[p[k][i][j]]-x[k])+abs(y[p[k][i][j]]-y[k])<=t) {printf("***%d %d %d %d***\n",p[k][i][j],x[p[k][i][j]],y[p[k][i][j]],z[p[k][i][j]]); z[p[k][i][j]]++; if(z[p[k][i][j]]>4) { w.id=p[k][i][j]; w.time=ans+abs(x[w.id]-x[k])+abs(y[w.id]-y[k]);printf("%d %d %d!!\n",k,w.id,abs(x[w.id]-x[k])+abs(y[w.id]-y[k])); s[x[w.id]][y[w.id]]=w.time; q.push(w); zz[w.id]=w.time; vv[w.id]=true; v[x[p[k][i][j]]][y[p[k][i][j]]]=0; } break; } }//printf("~~\n"); }printf("@@\n"); q.pop(); } for(i=1;i<=n;i++) if(vv[i]) printf("0 %d\n",zz[i]); else printf("1 %d\n",z[i]); } return 0; }以下是AC代码:
/*注意:同时到达同一个水滴的情况*/ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<math.h> #include<vector> #include<map> #include<set> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 105; int a[N][N],b[4][2]={{0,1},{0,-1},{1,0},{-1,0}},r,c,n,t,id[N][N],ans[N][2],temp[N][2]; struct node { int time,x,y,b_x,b_y; node(){} node(int _time,int _x,int _y,int _b_x,int _b_y):time(_time),x(_x),y(_y),b_x(_b_x),b_y(_b_y){} }; queue<node> q; void BFS(int d,int e) { int i,j,x,y,time=1,cnt; node top; while(!q.empty()) q.pop(); if(a[d][e]) { a[d][e]=0; ans[id[d][e]][0]=0; ans[id[d][e]][1]=0; } for(i=0;i<4;i++) { x=d+b[i][0]; y=e+b[i][1]; if(x>0&&x<=r&&y>0&&y<=c) q.push(node(1,x,y,b[i][0],b[i][1])); } while(!q.empty()&&time<=t) { //cout << "time = " << time << endl; cnt=0; while(!q.empty()&&(top=q.front()).time==time) { q.pop(); //cout << top.x << " " << top.y << endl; if(a[top.x][top.y]>0) { a[top.x][top.y]++; temp[cnt][0]=top.x; temp[cnt++][1]=top.y; } else { x=top.x+top.b_x; y=top.y+top.b_y; if(x>0&&x<=r&&y>0&&y<=c) q.push(node(top.time+1,x,y,top.b_x,top.b_y)); } } //cout << endl; for(i=0;i<cnt;i++) if(a[temp[i][0]][temp[i][1]]>4) { ans[id[temp[i][0]][temp[i][1]]][0]=0; ans[id[temp[i][0]][temp[i][1]]][1]=time; a[temp[i][0]][temp[i][1]]=0; for(j=0;j<4;j++) { x=temp[i][0]+b[j][0]; y=temp[i][1]+b[j][1]; if (x>0&&x<=r&&y>0&&y<=c) q.push(node(time+1,x,y,b[j][0],b[j][1])); } } time++; } for(i=1;i<=r;i++) for (j=1;j<=c;j++) if(a[i][j]) { ans[id[i][j]][0]=1; ans[id[i][j]][1]=a[i][j]; } for(i=0;i<n;i++) printf("%d %d\n",ans[i][0],ans[i][1]); } int main() { int i,x,y,z,d,e; while(~scanf("%d%d%d%d",&r,&c,&n,&t)) { memset(a,0,sizeof(a)); for(i=0;i<n;i++) { scanf("%d%d%d",&x,&y,&z); a[x][y]=z; id[x][y]=i; } scanf("%d%d",&d,&e); BFS(d,e); } return 0; }