题解 P2239 【螺旋矩阵】

Creeper_LKF

2017-08-11 21:38:29

Solution

翻了一年前的柜子找到的,好老了。其实这道题可以模拟,也可以找对角线,但是如果模拟的话就得注意每次不能一个一个加,需要一次加到底。这里为了防止复制,用了C语言的注释 ```cpp program matrix; var n,i,j:longint; a,b,c,d,e,g:longint; x,y:longint; begin read(n); read(j); read(i); if i=1 then write(j)//特判 else begin a:=n; b:=n-1; c:=b; d:=2; e:=2; x:=n; y:=1; while (x<>i) or (y<>j) do//直到没有达到目的地 begin if c=0 then begin inc(d); if (d mod 2=0) then dec(b);//转换步数 c:=b; inc(e); end; if (x=i) or (y=j) then begin case e mod 4 of//e记录方向 1:inc(x); 2:inc(y); 3:dec(x); 0:dec(y); end; dec(c); inc(a);//抹零头 end else begin case e mod 4 of//一定不会有太多用case of的,但是在汇编代码中case of回比if快 1:x:=x+c; 2:y:=y+c; 3:x:=x-c; 0:y:=y-c; end; a:=a+c;//记录一下当前的步数 c:=0; end; end; write(a); end; end. ```