1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| struct Node{ int col; bool vis; int cst; }A[maxM][maxM];
int F[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
inline int jd1(int a,int b,int c,int d){ return A[a][b].col == A[c][d].col ? 0 : 1; }
void dfs(int x,int y,int cst,int lx,int ly){ if(x > m || x < 1 || y > m || y < 1) return; if(A[x][y].vis && cst >= A[x][y].cst) return; A[x][y].vis = 1; A[x][y].cst = cst; for(int i = 0;i<4;i++){ int tx = x + F[i][0]; int ty = y + F[i][1]; if(A[x][y].col == 0){ if(A[tx][ty].col == 0) continue; dfs(tx,ty,cst+jd1(lx,ly,tx,ty),x,y); }else if(A[tx][ty].col == 0){ dfs(tx,ty,cst+2,x,y); }else if(A[x][y].col == A[tx][ty].col){ dfs(tx,ty,cst,x,y); }else{ dfs(tx,ty,cst+1,x,y); } } }
|