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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <stdio.h> #define gc() getchar_unlocked() int fRI(){ register int N = gc(), r = 0; for(;0x30>N||N>0x3A;N=gc()); do{ r = (r<<3) + (r<<1) + (N&0b1111); N = gc(); }while(0x30<=N); return r; } int main(void){ register int T = fRI(), tc = 0, N, X, ans, i, j, k, use; int map[20][20]; while(T--){ N = fRI(), X = fRI(); ans = N<<1; for(i = 0; i < N; ++i){ for(j = 0; j < N; ++j) map[i][j] = fRI(); use = -1; for(j = 0; j < N-1; ++j){ if(map[i][j]>map[i][j+1]){ if(map[i][j] != map[i][j+1]+1) {--ans; break;} if(j+X>=N) {--ans; break;} for(k = j+2; k <= j+X; ++k){ if(map[i][j+1]!=map[i][k]) {--ans; break;} use = k; } if(use != j+X) break; j = use - 1; } else if(map[i][j]<map[i][j+1]){ if(map[i][j]+1!=map[i][j+1]) {--ans; break;} if(j-X+1<0||j-X<use) {--ans; break;} for(k = j-1; k > j-X; --k){ if(map[i][j]!=map[i][k]) {--ans; break;} use = k; } if(use != j-X+1) break; } } } for(j = 0; j < N; ++j){ use = -1; for(i = 0; i < N-1; ++i){ if(map[i][j]>map[i+1][j]){ if(map[i][j] != map[i+1][j]+1) {--ans; break;} if(i+X>=N) {--ans; break;} for(k = i+2; k <= i+X; ++k){ if(map[i+1][j] != map[k][j]) {--ans; break;} use = k; } if(use != i+X) break; i = use-1; } else if(map[i][j]<map[i+1][j]){ if(map[i][j]+1 != map[i+1][j]) {--ans; break;} if(i-X+1<0||i-X<use) {--ans; break;} for(k = i-1; k > i-X; --k){ if(map[i][j] != map[k][j]) {--ans; break;} use = k; } if(use != i-X+1) break; } } } printf("#%d %d\n",++tc,ans); } return 0; }
|