using System; namespace Ice { class Program { static int addFlowSource(int[,] d, int [,,] fd,int[,] fs, int[,] ft,int[] x,int[] y ,int columns , int rows){ bool[,] visited = new bool[columns,rows]; x[0]=0; while(x[0] fs[x[0],y[0]]){ int newFlow = addFlow(d,fd,fs,ft,visited,columns, rows,x[0],y[0],d[x[0],y[0]]-fs[x[0],y[0]]); if(newFlow!=0){ fs[x[0],y[0]]+= newFlow; return newFlow; } } y[0]++; } x[0]++; } return 0; } static int addFlow(int[,] d, int [,,] fd,int [,] fs, int[,] ft,bool[,] visited,int columns, int rows, int x_1, int y_1, int flow){ visited[x_1,y_1]=true; int newFlow = 0; int[] neigboursX = {x_1+1,x_1+1,x_1,x_1-1,x_1-1,x_1-1,x_1,x_1+1}; int[] neigboursY = {y_1,y_1+1,y_1+1,y_1+1,y_1,y_1-1,y_1-1,y_1-1}; if(32-d[x_1,y_1] > ft[x_1,y_1]){ newFlow = Math.Min(flow,32-d[x_1,y_1]-ft[x_1,y_1]); ft[x_1,y_1]+=newFlow; //visited[x_1,y_1]=false; return newFlow; } for(int i=0; i<8;i++){ //while(index[x_1,y_1]<8){ //int i=index[x_1,y_1]; int x_2 = neigboursX[i]; int y_2 = neigboursY[i]; if(x_2>=0 && x_2=0 && y_2fd[x_1,y_1,i]){ newFlow = Math.Min(flow,c_forward-fd[x_1,y_1,i]); newFlow = addFlow(d,fd,fs,ft,visited,columns,rows,x_2,y_2,newFlow); if(newFlow!=0){ fd[x_1,y_1,i]+= newFlow; //visited[x_1,y_1]=false; return newFlow; } } //backward int c_backward = fd[x_2,y_2,(i+4)%4]; if(c_backward>0){ newFlow = Math.Min(flow,c_backward); newFlow = addFlow(d,fd,fs,ft,visited,columns,rows,x_2,y_2,newFlow); if(newFlow!=0){ fd[x_2,y_2,(i+4)%4]-= newFlow; //visited[x_1,y_1]=false; return newFlow; } } } } //visited[x_1,y_1]=false; return 0; } static int maxFlow(int[,] d, int[,,] fd, int[,] fs, int[,] ft,int[] x,int[] y, int columns,int rows){ //for(int i=0;i0){} int sum=0; foreach(int s in fs){ sum+= s; } return sum; } static void printReachable(int[,] d, int[,,] fd,int[,] fs, int[,] ft,bool[,] visited, int columns, int rows, int x, int y){ Console.WriteLine($"{y} {x}"); visited[x,y]=true; int[] neigboursX = {x+1,x+1,x,x-1,x-1,x-1,x,x+1}; int[] neigboursY = {y,y+1,y+1,y+1,y,y-1,y-1,y-1}; for(int i=0;i<8;i++){ int x_2 = neigboursX[i]; int y_2 = neigboursY[i]; if(0<=x_2 && x_20) // backward printReachable(d,fd,fs,ft,visited,columns,rows,x_2,y_2); } } } static void printReachableSource(int[,] d, int[,,] fd,int[,] fs, int[,] ft,bool[,] visited, int columns, int rows){ for(int x =0; x