hdu4527
代码写得很搓,模拟~一层一层的往下走~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <iostream> 5 using namespace std; 6 int map[10][10]; 7 int in[10][10]; 8 int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 9 struct node{ 10 int x,y,z; 11 int k; 12 }; 13 bool operator<(node m,node n){ 14 return m.z>n.z; 15 } 16 int check(int x,int y){ 17 if(x>=1&&x<=6&&y>=1&&y<=6) return 1; 18 return 0; 19 } 20 priority_queue<node>que; 21 void insert_que(node tmp){ 22 if(map[tmp.x][tmp.y]>4&&!in[tmp.x][tmp.y]) { 23 tmp.k=-1; 24 que.push(tmp); 25 in[tmp.x][tmp.y]++; 26 } 27 } 28 void bfs(int x,int y){ 29 int i; 30 memset(in,0,sizeof(in)); 31 node now,tmp; 32 tmp.x=x; 33 tmp.y=y; 34 tmp.z=1; 35 insert_que(tmp); 36 while(!que.empty()){ 37 tmp=que.top(); 38 que.pop(); 39 in[tmp.x][tmp.y]--; 40 if(tmp.k==-1){ 41 for(i=0;i<4;i++){ 42 now.x=tmp.x+move[i][0]; 43 now.y=tmp.y+move[i][1]; 44 if(check(now.x,now.y)){ 45 now.z=tmp.z+1; 46 if(map[now.x][now.y]==0){ 47 now.k=i; 48 que.push(now); 49 in[now.x][now.y]++; 50 }else{ 51 ++map[now.x][now.y]; 52 insert_que(now); 53 } 54 } 55 } 56 map[tmp.x][tmp.y]=0; 57 }else{ 58 now.x=tmp.x+move[tmp.k][0]; 59 now.y=tmp.y+move[tmp.k][1]; 60 if(check(now.x,now.y)){ 61 now.z=tmp.z+1; 62 if(map[now.x][now.y]==0){ 63 now.k=tmp.k; 64 que.push(now); 65 in[now.x][now.y]++; 66 }else{ 67 ++map[now.x][now.y]; 68 insert_que(now); 69 } 70 } 71 } 72 } 73 } 74 int main(){ 75 int m,a,b; 76 int i,j; 77 while(~scanf("%d",&map[1][1])){ 78 for(j=2;j<=6;j++) scanf("%d",&map[1][j]); 79 for(i=2;i<=6;i++) 80 for(j=1;j<=6;j++) 81 scanf("%d",&map[i][j]); 82 scanf("%d",&m); 83 for(i=0;i<m;i++){ 84 scanf("%d%d",&a,&b); 85 map[a][b]++; 86 bfs(a,b); 87 } 88 for(i=1;i<=6;i++,puts("")){ 89 printf("%d",map[i][1]); 90 for(j=2;j<=6;j++){ 91 printf(" %d",map[i][j]); 92 } 93 } 94 puts(""); 95 } 96 return 0; 97 }