#include //?? #include #define PORES res_tem[p].flow[0].row int N, P, B, flag, row_end, col_end, tt, S, BR = 500; int min_row, min_col, max_row, max_col; int scan_pow[101]; struct tables{ struct columns{ int flow; int pos; } col[101]; }tab_in[101], table[101]; struct begin_end{ int row1, col1, row2, col2; }be[251]; struct results{ struct flows{ int row, col; }flow[10001]; }res_tem[251], res_end[251]; void Set_tab_in(){ for(int i=0; i<=N; i++){ for(int j=0; j<=N; j++){ table[i].col[j].flow = tab_in[i].col[j].flow; table[i].col[j].pos = tab_in[i].col[j].pos; } } } void Set_res_end(){ for(int i=0; i<=P; i++){ if(i == BR) return; for(int j=1; j<=res_tem[i].flow[0].row; j++){ res_end[i].flow[j].row = res_tem[i].flow[j].row; res_end[i].flow[j].col = res_tem[i].flow[j].col; } res_end[i].flow[0].row = res_tem[i].flow[0].row; } } void Insert_cells(){ } void ScanRow(){ } void Trace(int p, int row, int col){ if(flag) return; if(GetTickCount() - tt > 2900){S++; BR = p; return;} int i, r, c; for(i=1; i<=4; i++){ if(flag) return; if(i == 1){r=0; c=1;} if(i == 2){r=1; c=0;} if(i == 3){r=0; c=-1;} if(i == 4){r=-1; c=0;} if(row + r > max_row + 1 || row + r < min_row - 1) continue; if(col + c > max_col + 1 || col + c < min_col - 1) continue; if(table[row+r].col[col+c].flow == 0 && row+r >= 0 && row+r < N && col+c >= 0 && col+c < N){ PORES++; res_tem[p].flow[PORES].row = row + r; res_tem[p].flow[PORES].col = col + c; table[row+r].col[col+c].flow = p; table[row+r].col[col+c].pos = PORES; Trace(p, row + r, col + c); } if(row + r == row_end && col + c == col_end){ PORES++; res_tem[p].flow[PORES].row = row + r; res_tem[p].flow[PORES].col = col + c; table[row+r].col[col+c].pos = PORES; flag++; return; } } if(flag) return; if(PORES > 0){ table[row].col[col].flow = 0; PORES--; return;} } void Solve(){ Set_tab_in(); for(int i=1; i<=P; i++){ if(S) break; flag = 0; if(be[i].row1 <= be[i].row2){ max_row = be[i].row2; min_row = be[i].row1; row_end = be[i].row2; col_end = be[i].col2; res_tem[i].flow[1].row = be[i].row1; res_tem[i].flow[1].col = be[i].col1; res_tem[i].flow[0].row = 1; if(be[i].col1 <= be[i].col2){ min_col = be[i].col1; max_col = be[i].col2; } else {min_col = be[i].col2; max_col = be[i].col1;} Trace(i, be[i].row1, be[i].col1); } else { max_row = be[i].row1; min_row = be[i].row2; row_end = be[i].row1; col_end = be[i].col1; res_tem[i].flow[1].row = be[i].row2; res_tem[i].flow[1].col = be[i].col2; res_tem[i].flow[0].row = 1; if(be[i].col1 <= be[i].col2){ min_col = be[i].col1; max_col = be[i].col2; } else {min_col = be[i].col2; max_col = be[i].col1;} Trace(i, be[i].row2, be[i].col2); } if(res_tem[i].flow[0].row == 0){ table[be[i].row1].col[be[i].col1].flow = i; table[be[i].row2].col[be[i].col2].flow = i; } } } void Read(){ int row1, row2, col1, col2; FILE *in = fopen("flow.in", "r"); fscanf(in, "%i %i\n", &N, &P); for(int i=1; i<=P; i++){ fscanf(in, "%i %i %i %i\n", &row1, &col1, &row2, &col2); tab_in[row1].col[col1].flow = tab_in[row2].col[col2].flow = i; be[i].row1 = row1; be[i].col1 = col1; be[i].row2 = row2; be[i].col2 = col2; } fscanf(in, "%i\n", &B); for(int i=1; i<=B; i++){ fscanf(in, "%i %i\n", &row1, &col1); tab_in[row1].col[col1].flow = -1; } } void Output(){ FILE *out = fopen("flow.out", "w"); for(int i=1; i<=P; i++){ fprintf(out, "%i ", res_end[i].flow[0].row); for(int j=1; j<=res_end[i].flow[0].row; j++){ fprintf(out, "%i %i ", res_end[i].flow[j].row, res_end[i].flow[j].col); } fprintf(out, "\n"); } } int main(){ tt = GetTickCount(); Read(); Solve(); Set_res_end(); //??? Insert_cells(); Output(); }