package work;
import java.io.FileNotFoundException;
public class Infect2 {
static node [] queue=new node[26];//队列里数组node[x,y,step]
static int head=0;//头指针
static int tail=0;//尾指针
static int [][]crodw={
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1}
};
static node curpoint ;//记录出列的node
static int []Dx={-1,0,1,0};
static int []Dy={0,1,0,-1};
static node m; //每次传染起点node元素
static int count=0;
static int N=crodw.length;
public static class node{
int x;
int y;
int step;
public node(int x,int y,int step){
this.x=x;
this.y=y;
this.step=step;
}
}
//进
public static void inqueue(node n){
queue[tail++]=n;
}
//出
public static node dequeue(){
node ret=queue[head];
head++;
return ret;
}
public static void main(String[] args) throws FileNotFoundException {
crodw[0][3]=2;
m=new node (0,0,crodw[0][3]);
inqueue(m);
infect();
System.out.println("共传染人数:"+(count+1));
System.out.println("用时间"+(curpoint.step-1));
}
public static void infect(){
if(head<tail){
curpoint=dequeue();//出列node
int x=curpoint.x;
int y=curpoint.y;
for(int i=0;i<4;i++){
int dx=x+Dx[i];
int dy=y+Dy[i];
if(dx>=0&&dx<N&&dy<N&&dy>=0){
if(crodw[dx][dy]==1){
crodw[dx][dy]=2;
node n=new node (dx,dy,curpoint.step+1);//满足传染的node
inqueue(n);//入列
count++;
}
}
}
infect();
}
}
}
、、input
0 1 0 1 1 0 0 0
0 1 0 0 0 0 0 1
0 0 0 1 0 0 0 0
0 1 0 1 1 0 0 0
0 1 0 1 1 1 0 1
0 0 0 1 1 1 1 0
1 0 0 0 1 0 0 1
0 1 0 1 0 0 1 0
、、output
共传染人数:25
用时间9