题意:中文题,看了学长的发现自己没注意到要先同一秒的先走完,BFS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 10010;
const int INF = 0x3f3f3f3f;
int m;
int map[8][8];
int dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
struct Node{
int x,y;
int cnt;
int dir;
}Q[500000];
void addQue(int x,int y,int cnt,int &rear){
map[x][y] = 0;
for (int i = 0; i < 4; i++){
int dx = x + dir[i][0];
int dy = y + dir[i][1];
if (dx >= 1 && dx <= 6 && dy >= 1 && dy <= 6){
Q[rear].x = dx;
Q[rear].y = dy;
Q[rear].cnt = cnt;
Q[rear++].dir = i;
}
}
}
void bfs(int x, int y){
int front=0, rear=0;
addQue(x, y, 0, rear);
int cur = 0; //当前第几秒
while(front < rear){
while(front < rear && Q[front].cnt==cur){ // 把这一秒的全走完
Node &t = Q[front++];
if(map[t.x][t.y]){
++map[t.x][t.y];
} else{
Node& now = Q[rear];
now.x = t.x + dir[t.dir][0];
now.y = t.y + dir[t.dir][1];
now.cnt = t.cnt + 1;
now.dir = t.dir;
if(now.x>=1&&now.x<=6&&now.y>=1&&now.y<=6)
++rear;
}
}
for(int i=1; i<=6; ++i){
for(int j=1; j<=6; ++j)
if(map[i][j]>4){
addQue(i,j,cur+1,rear);
}
}
++cur;
}
}
int main(){
int x,y;
while (scanf("%d",&map[1][1]) != EOF){
for (int i = 1; i <= 6; i++)
for (int j = (i==1?2:1); j <= 6; j++)
scanf("%d",&map[i][j]);
scanf("%d",&m);
for (int i = 0; i < m; i++){
scanf("%d%d",&x,&y);
if (++map[x][y] > 4)
bfs(x,y);
}
for (int i = 1; i <= 6; i++){
for (int j = 1; j <= 6; j++)
if (j != 1)
printf(" %d",map[i][j]);
else printf("%d",map[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}