queue< pair<int,int> >que; que.push(mp(r1,c1)); val[r1][c1] = 0; while(!que.empty()) { auto now = que.front(); que.pop(); int x = now.first; int y = now.second; repp(k,1,4) { repp(step,1,10) { int nx = x + step*dx44[k]; int ny = y + step*dy44[k]; if(nx>=1&&nx<=8&&ny>=1&&ny<=8) { if(val[nx][ny]==-1||val[nx][ny]>val[x][y]+1) { val[nx][ny]=val[x][y]+1; que.push(mp(nx,ny)); } } } } }
printf("%d\n", val[r2][c2]); }
C Switch the Lights
Given the initial state of $n$ lights, every position has a switch that flips $[i,r_i]$ at cost $c_i$. Find the minimum final cost, or output $-1$ if it is impossible.
Think carefully: the solution is forced. Scan from $1$ to $n$; if light $i$ is on, you must flip $[i,r_i]$. Keep tracking the state as you go.
Given $n$ numbers and the relative ordering of each $i$ and $i+1$, count the valid permutations.
Dynamic programming: for $x$ numbers, count the ways for $x$ to occupy each position from $1$ to $x$, then transition according to the relation between $x$ and $x+1$.
int sum = 0; for (auto itr : pos) { sum += itr; sum %= mod; } printf("%d\n", sum); }
E Direct the Graph
Given an undirected complete graph on $n$ vertices, orient every edge to maximise the number of directed 3-cycles. Output that maximum and a construction.
The answer is: all triangles minus bad triangles.
All triangles: $C(n,3)$.
A bad triangle is formed by any two edges pointing in the same direction at one vertex.