扩散(2020国赛JAVA B组)
作者:
husheng
,
2022-05-07 15:38:11
,
所有人可见
,
阅读 174
import java.util.*;
public class Main {
static class node{
int x;
int y;
int time;
node(int x,int y,int time){
this.x=x;
this.y=y;
this.time=time;
}
}
static int[] x= {-1,1,0,0};
static int[] y= {0,0,-1,1};
static boolean[][] vis=new boolean[10000][10000];
static long ans=4;
public static void bfs() {
Queue<node> q=new LinkedList<node>();
q.offer(new node(0+3000,0+3000,0));
q.offer(new node(2020+3000,11+3000,0));
q.offer(new node(11+3000,14+3000,0));
q.offer(new node(2000+3000,2000+3000,0));
vis[0+3000][0+3000]=true;
vis[2020+3000][11+3000]=true;
vis[11+3000][14+3000]=true;
vis[2000+3000][2000+3000]=true;
while(!q.isEmpty()) {
node t=q.poll();
if(t.time==2020) {
return ;
}
for(int a=0;a<4;a++) {
int newx=t.x+x[a];
int newy=t.y+y[a];
if(vis[newx][newy]==false) {
ans++;
vis[newx][newy]=true;
q.offer(new node(newx,newy,t.time+1));
}
}
}
}
public static void main(String[] args) {
bfs();
System.out.println(ans);
}
}