two positions of a NxN chess board can be connected by pawns move(diagonally) iff both of them are on black/white position, this can be checked by checking the pair. if both of the x,y (start position ) and X,Y(end position) are odd,odd or even,even then they can be connected or both of the pair are
Category: Spiral Shape Problem
903
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
#include <iostream> using namespace std; int main() { int n; int Break=0; long long int res,res1,res2,res3,res4,rank,diff; long long int b_left,b_ryt,t_left,t_ryt; while(cin>>n) { res=1; res1=1; res2=1; res3=1; res4=1; Break=0; if(n==1) { cout<<7<<";"<<8<<";"<<9<<";\n"; cout<<6<<";"<<1<<";"<<2<<";\n"; cout<<5<<";"<<4<<";"<<3<<";\n"; continue; } for(long long int i=0;i<1500000000;i++) { if(Break) break; res1+=i*2; if(res1>=n) { rank=i; res=res1; Break=1; //continue; } res2+=i*4; if(res2>=n) { rank=i; res=res2; Break=1; //continue; } res3+=i*6; if(res3>=n) { rank=i; res=res3; Break=1; //continue; } res4+=i*8; if(res4>=n) { rank=i; res=res4; Break=1; } } if(rank==0) { cout<<7<<";"<<8<<";"<<9<<";\n"; cout<<6<<";"<<1<<";"<<2<<";\n"; cout<<5<<";"<<4<<";"<<3<<";\n"; continue; } diff=rank*2; t_ryt=res; t_left=t_ryt-diff; b_left=t_left-diff; b_ryt=b_left-diff; if(n==t_ryt) { cout<<res+(rank+1)*8-2<<";"<<res+(rank+1)*8-1<<";"<<res+(rank+1)*8<<";\n"; cout<<res-1<<";"<<res<<";"<<res+1<<";\n"; cout<<res-(rank)*8<<";"<<(res+1)-(rank)*8<<";"<<res+2<<";\n"; continue; } if(n==t_left) { cout<<res+(rank+1)*6<<";"<<res+(rank+1)*6+1<<";"<<res+2+(rank+1)*6<<";\n"; cout<<res-1+(rank+1)*6<<";"<<n<<";"<<n+1<<";\n"; cout<<res-2+(rank+1)*6<<";"<<n-1<<";"<<n-rank*6-(rank-1)*2<<";\n"; continue; } if(n==b_left) { //cout<<rank<<"\n"; cout<<res+2+(rank+1)*4<<";"<<n+1<<";"<<n-(2*rank-1)*4<<";\n"; cout<<res+1+(rank+1)*4<<";"<<n<<";"<<n-1<<";\n"; cout<<res+(rank+1)*4<<";"<<res+(rank+1)*4-1<<";"<<res+(rank+1)*4-2<<";\n"; continue; } if(n==b_ryt) { cout<<res-rank*8-(rank-1)*6<<";"<<n-1<<";"<<(res+(rank+1)*2)-2<<";\n"; cout<<n+1<<";"<<n<<";"<<(res+(rank+1)*2)-1<<";\n"; cout<<(res+(rank+1)*2)+2<<";"<<(res+(rank+1)*2)+1<<";"<<res+(rank+1)*2<<";\n"; continue; } if(n>b_ryt && n<b_left) { if(n==4) { cout<<6<<";"<<1<<";"<<2<<";\n"; cout<<5<<";"<<4<<";"<<3<<";\n"; cout<<16<<";"<<15<<";"<<14<<";\n"; continue; } int prev_b_ryt=res-rank*8-(rank-1)*6; int prev_b_left=b_left-(2*rank-1)*4; int next_b_ryt=res+(rank+1)*2; int next_b_left=res+(rank+1)*4; int pos= n- b_ryt; if(pos==1) { cout<<prev_b_ryt+(pos)<<";"<<prev_b_ryt+(pos-1)<<";"<<n-2<<";\n"; cout<<n+1<<";"<<n<<";"<<n-1<<";\n"; cout<<next_b_ryt+pos+2<<";"<<next_b_ryt+pos+1<<";"<<next_b_ryt+pos<<";\n"; continue; } if(b_left-n==1) { cout<<n+2<<";"<<prev_b_ryt+(pos-1)<<";"<<prev_b_ryt+(pos-2)<<";\n"; cout<<n+1<<";"<<n<<";"<<n-1<<";\n"; cout<<next_b_ryt+pos+2<<";"<<next_b_ryt+pos+1<<";"<<next_b_ryt+pos<<";\n"; continue; } cout<<prev_b_ryt+(pos)<<";"<<prev_b_ryt+(pos-1)<<";"<<prev_b_ryt+(pos-2)<<";\n"; cout<<n+1<<";"<<n<<";"<<n-1<<";\n"; cout<<next_b_ryt+pos+2<<";"<<next_b_ryt+pos+1<<";"<<next_b_ryt+pos<<";\n"; continue; } if(n<t_ryt && n>t_left) { if(n==8) { cout<<22<<";"<<23<<";"<<24<<";\n"; cout<<7<<";"<<8<<";"<<9<<";\n"; cout<<6<<";"<<1<<";"<<2<<";\n"; continue; } int prev_t_left=t_left-rank*6-(rank-1)*2; int prev_t_ryt=res-(rank)*8; int next_t_ryt=res+(rank+1)*8; int next_t_left=res+(rank+1)*6; int pos= n- t_left; if(pos==1) { cout<<next_t_left+pos<<";"<<next_t_left+pos+1<<";"<<next_t_left+pos+2<<";\n"; cout<<n-1<<";"<<n<<";"<<n+1<<";\n"; cout<<n-2<<";"<<prev_t_left<<";"<<prev_t_left+1<<";\n"; continue; } { cout<<next_t_left+pos<<";"<<next_t_left+pos+1<<";"<<next_t_left+pos+2<<";\n"; cout<<n-1<<";"<<n<<";"<<n+1<<";\n"; cout<<prev_t_left+(pos-2)<<";"<<prev_t_left+(pos-1)<<";"<<prev_t_left+pos<<";\n"; continue; } } if(n>b_left && n<t_left) { if(n==6) { cout<<20<<";"<<7<<";"<<8<<";\n"; cout<<19<<";"<<6<<";"<<1<<";\n"; cout<<18<<";"<<5<<";"<<4<<";\n"; continue; } int prev_t_left=t_left-rank*6-(rank-1)*2; int prev_b_left=b_left-(2*rank-1)*4; int next_b_left=res+(rank+1)*4; int next_t_left=res+(rank+1)*6; int pos= n- b_left; if(pos==1) { cout<<next_b_left+pos+2<<";"<<n+1<<";"<<prev_b_left+(pos)<<";\n"; cout<<next_b_left+pos+1<<";"<<n<<";"<<prev_b_left+(pos-1)<<";\n"; cout<<next_b_left+pos<<";"<<n-1<<";"<<n-2<<";\n"; continue; } if(n==t_left-1) { cout<<next_b_left+pos+2<<";"<<n+1<<";"<<n+2<<";\n"; cout<<next_b_left+pos+1<<";"<<n<<";"<<prev_b_left+(pos-1)<<";\n"; cout<<next_b_left+pos<<";"<<n-1<<";"<<prev_b_left+(pos-2)<<";\n"; continue; } cout<<next_b_left+pos+2<<";"<<n+1<<";"<<prev_b_left+(pos)<<";\n"; cout<<next_b_left+pos+1<<";"<<n<<";"<<prev_b_left+(pos-1)<<";\n"; cout<<next_b_left+pos<<";"<<n-1<<";"<<prev_b_left+(pos-2)<<";\n"; continue; } if(n<b_ryt) { if(n==2) { cout<<8<<";"<<9<<";"<<10<<";\n"; cout<<1<<";"<<2<<";"<<11<<";\n"; cout<<4<<";"<<3<<";"<<12<<";\n"; continue; } int prev_t_ryt=res-(rank)*8; int prev_b_ryt=res-rank*8-(rank-1)*6; int next_b_ryt=res+(rank+1)*2; int next_t_ryt=res+(rank+1)*8; int pos= b_ryt - n; if(pos==1) { cout<<prev_b_ryt-pos<<";"<<n-1<<";"<<next_b_ryt-(pos+2)<<";\n"; cout<<prev_b_ryt<<";"<<n<<";"<<next_b_ryt-(pos+1)<<";\n"; cout<<n+2<<";"<<n+1<<";"<<next_b_ryt-(pos)<<";\n"; continue; } if(prev_t_ryt+1==n) { cout<<res-1<<";"<<res<<";"<<res+1<<";\n"; cout<<n-1<<";"<<n<<";"<<res+2<<";\n"; cout<<prev_b_ryt-(pos-2)<<";"<<n+1<<";"<<res+3<<";\n"; continue; } if(prev_t_ryt+2==n) { cout<<n-2<<";"<<n-1<<";"<<res+2<<";\n"; cout<<prev_b_ryt-(pos-1)<<";"<<n<<";"<<res+3<<";\n"; cout<<prev_b_ryt-(pos-1)+1<<";"<<n+1<<";"<<res+4<<";\n"; continue; } cout<<prev_b_ryt-pos<<";"<<n-1<<";"<<next_b_ryt-(pos+2)<<";\n"; cout<<prev_b_ryt-pos+1<<";"<<n<<";"<<next_b_ryt-(pos+1)<<";\n"; cout<<prev_b_ryt-pos+2<<";"<<n+1<<";"<<next_b_ryt-(pos)<<";\n"; continue; } } return 0; } |