import java.util.Arrays ;
/*
プログラミングのお題スレ Part15
//mevius.5ch.net/test/read.cgi/tech/1564310397/137
137 名前:さまよえる蟻人間 ◆T6xkBnTXz7B0 [sage] 投稿日:2019/08/17(土) 15:58:34.13 ID:hkO+8710
お題: オカダンゴムシには進行中に壁にぶつかると左へ、次は右へ(あるいは右へ、次は左へ)と交互に曲がっていく習性がある。この行動は「交替性転向反応」といい、左右に交互に曲がる事で天敵から逃げられる確率を高めているといわれている(ウィキペディアより引用)。
何もない整数平面上にスタート地点(S)とゴール地点(G)が与えられる。仮想ダンゴムシ(@)の周辺に壁(#)をいくつか作って、スタート地点からゴール地点まで誘導せよ。
ただし、ダンゴムシは最初は右向きを向いていて、最初にぶつかったら左に曲がるものとする。
90度曲がって目の前にすぐに壁がある場合は同じ方へもう90度曲がるものとする。壁はいくつ作ってもよい。
例) S=(0, 0), G=(5, 2)
→#={(6, 0)}.
例) S=(1, 2), G=(-1, 1)
→#={(2, 2), (1, 3), (0, 2), (1, 0)}.
*/
class Ideone
{
private static final Point [ ] EMPTY
= new Point [ 0 ] ;
public static void main
( String [ ] args
) {
}
{
Point [ ] wall
= wall
( s, g
) ; System .
out .
printf ( "S:%s G:%s Wall:%s X:(0,0)%n" , s, g,
Arrays .
toString ( wall
) ) ; System .
out .
println ( "--------------------" ) ; int minX
= Math .
min ( 0 ,
Math .
min ( s.
x , g.
x ) ) ; int minY
= Math .
min ( 0 ,
Math .
min ( s.
y , g.
y ) ) ; int maxX
= Math .
max ( 0 ,
Math .
max ( s.
x , g.
x ) ) ; int maxY
= Math .
max ( 0 ,
Math .
max ( s.
y , g.
y ) ) ; {
minX
= Math .
min ( minX, p.
x ) ; minY
= Math .
min ( minY, p.
y ) ; maxX
= Math .
max ( maxX, p.
x ) ; maxY
= Math .
max ( maxY, p.
y ) ; }
char [ ] [ ] cs = new char [ maxY - minY + 1 ] [ maxX - minX + 1 ] ;
for ( int i
= 0 ; i
< cs.
length ; i
++ ) Arrays .
fill ( cs
[ i
] ,
' ' ) ; cs[ - minY] [ - minX] = 'X' ;
cs[ s.y - minY] [ s.x - minX] = 'S' ;
cs[ g.y - minY] [ g.x - minX] = 'G' ;
{
cs[ p.y - minY] [ p.x - minX] = '#' ;
}
for ( int i = cs.length - 1 ; i >= 0 ; i-- )
System .
out .
println ( "////////////////////" ) ; }
// 壁を作るところ
{
int dx = g.x - s.x ;
int dy = g.y - s.y ;
if ( dx >= 0 )
{
if ( dy == 0 ) return points( ) ;
if ( dy > 0 ) return points( g.x + 1 , s.y ) ;
return points( g.x + 1 , s.y , g.x , s.y + 2 , g.x + 1 , s.y + 1 ) ;
}
if ( dy == 0 ) return points( s.x + 1 , s.y , s.x , s.y + 1 ) ;
if ( dy > 0 ) return points( s.x + 1 , s.y , s.x , s.y + 1 , g.x - 1 , s.y ) ;
return points( s.x + 1 , s.y , s.x , s.y + 1 , s.x - 1 , s.y , s.x , g.y - 1 ) ;
}
static Point [ ] points
( int ...
coodinate ) {
for ( int i = 0 ; i < points.length ; i++ )
points
[ i
] = new Point ( coodinate
[ i
* 2 ] , coodinate
[ i
* 2 + 1 ] ) ; return points;
}
{
int x, y;
{
this .x = x;
this .y = y;
}
@Override
{
return "(" + x + "," + y + ")" ;
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgovKgrjg5fjg63jgrDjg6njg5/jg7PjgrDjga7jgYrpoYzjgrnjg6wgUGFydDE1IAovL21ldml1cy41Y2gubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xNTY0MzEwMzk3LzEzNwoKMTM3IOWQjeWJje+8muOBleOBvuOCiOOBiOOCi+ifu+S6uumWkyDil4ZUNnhrQm5UWHo3QjAgW3NhZ2VdIOaKleeov+aXpe+8mjIwMTkvMDgvMTco5ZyfKSAxNTo1ODozNC4xMyBJRDpoa08rODcxMArjgYrpoYw6IOOCquOCq+ODgOODs+OCtOODoOOCt+OBq+OBr+mAsuihjOS4reOBq+WjgeOBq+OBtuOBpOOBi+OCi+OBqOW3puO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TWF0aC5taW4ocy54LCBnLngpKTsKICAgICAgICBpbnQgbWluWSA9IE1hdGgubWluKDAsIE1hdGgubWluKHMueSwgZy55KSk7CiAgICAgICAgaW50IG1heFggPSBNYXRoLm1heCgwLCBNYXRoLm1heChzLngsIGcueCkpOwogICAgICAgIGludCBtYXhZID0gTWF0aC5tYXgoMCwgTWF0aC5tYXgocy55LCBnLnkpKTsKICAgICAgICBmb3IgKFBvaW50IHAgOiB3YWxsKQogICAgICAgIHsKICAgICAgICAgICAgbWluWCA9IE1hdGgubWluKG1pblgsIHAueCk7CiAgICAgICAgICAgIG1pblkgPSBNYXRoLm1pbihtaW5ZLCBwLnkpOwogICAgICAgICAgICBtYXhYID0gTWF0aC5tYXgobWF4WCwgcC54KTsKICAgICAgICAgICAgbWF4WSA9IE1hdGgubWF4KG1heFksIHAueSk7CiAgICAgICAgfQoKICAgICAgICBjaGFyW11bXSBjcyA9IG5ldyBjaGFyW21heFkgLSBtaW5ZICsgMV1bbWF4WCAtIG1pblggKyAxXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGNzLmxlbmd0aDsgaSsrKSBBcnJheXMuZmlsbChjc1tpXSwgJyAnKTsKICAgICAgICBjc1stbWluWV1bLW1pblhdID0gJ1gnOwogICAgICAgIGNzW3MueSAtIG1pblldW3MueCAtIG1pblhdID0gJ1MnOwogICAgICAgIGNzW2cueSAtIG1pblldW2cueCAtIG1pblhdID0gJ0cnOwogICAgICAgIGZvciAoUG9pbnQgcCA6IHdhbGwpCiAgICAgICAgewogICAgICAgICAgICBjc1twLnkgLSBtaW5ZXVtwLnggLSBtaW5YXSA9ICcjJzsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IGNzLmxlbmd0aC0xOyBpID49IDA7IGktLSkKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGNzW2ldKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIi8vLy8vLy8vLy8vLy8vLy8vLy8vIik7CiAgICB9CiAgICAKICAgIC8vIOWjgeOCkuS9nOOCi+OBqOOBk+OCjQogICAgc3RhdGljIFBvaW50W10gd2FsbChQb2ludCBzLCBQb2ludCBnKQogICAgewogICAgICAgIGludCBkeCA9IGcueCAtIHMueDsKICAgICAgICBpbnQgZHkgPSBnLnkgLSBzLnk7CiAgICAgICAgCiAgICAgICAgaWYgKGR4ID49IDApCiAgICAgICAgewogICAgICAgICAgICBpZiAoZHkgPT0gMCkgcmV0dXJuIHBvaW50cygpOwogICAgICAgICAgICBpZiAoZHkgPiAwKSByZXR1cm4gcG9pbnRzKGcueCArIDEsIHMueSk7CiAgICAgICAgICAgIHJldHVybiBwb2ludHMoZy54ICsgMSwgcy55LCBnLngsIHMueSArIDIsIGcueCArIDEsIHMueSArIDEpOwogICAgICAgIH0KCiAgICAgICAgaWYgKGR5ID09IDApIHJldHVybiBwb2ludHMocy54ICsgMSwgcy55LCBzLngsIHMueSArIDEpOwogICAgICAgIGlmIChkeSA+IDApIHJldHVybiBwb2ludHMocy54ICsgMSwgcy55LCBzLngsIHMueSArIDEsIGcueCAtIDEsIHMueSk7CiAgICAgICAgcmV0dXJuIHBvaW50cyhzLnggKyAxLCBzLnksIHMueCwgcy55ICsgMSwgcy54IC0gMSwgcy55LCBzLngsIGcueSAtIDEpOwogICAgfQoKICAgIAogICAgc3RhdGljIFBvaW50W10gcG9pbnRzKGludC4uLmNvb2RpbmF0ZSkKICAgIHsKICAgICAgICBQb2ludFtdIHBvaW50cyA9IG5ldyBQb2ludFtjb29kaW5hdGUubGVuZ3RoIC8gMl07CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBwb2ludHMubGVuZ3RoOyBpKyspCiAgICAgICAgICAgIHBvaW50c1tpXSA9IG5ldyBQb2ludChjb29kaW5hdGVbaSAqIDJdLCBjb29kaW5hdGVbaSAqIDIgKyAxXSk7CiAgICAgICAgcmV0dXJuIHBvaW50czsKICAgIH0KICAgIAogICAgc3RhdGljIGNsYXNzIFBvaW50CiAgICB7CiAgICAgICAgaW50IHgsIHk7CgogICAgICAgIFBvaW50KGludCB4LCBpbnQgeSkKICAgICAgICB7CiAgICAgICAgICAgIHRoaXMueCA9IHg7CiAgICAgICAgICAgIHRoaXMueSA9IHk7CiAgICAgICAgfQoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgU3RyaW5nIHRvU3RyaW5nKCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiAiKCIgKyB4ICsgIiwiICsgeSArICIpIjsKICAgICAgICB9CiAgICB9Cn0K
stdout
S:(1,1) G:(5,1) Wall:[] X:(0,0)
--------------------
S G
X
////////////////////
S:(1,1) G:(5,5) Wall:[(6,1)] X:(0,0)
--------------------
G
S #
X
////////////////////
S:(1,1) G:(1,5) Wall:[(2,1)] X:(0,0)
--------------------
G
S#
X
////////////////////
S:(1,1) G:(-5,5) Wall:[(2,1), (1,2), (-6,1)] X:(0,0)
--------------------
G
#
# S#
X
////////////////////
S:(1,1) G:(-5,1) Wall:[(2,1), (1,2)] X:(0,0)
--------------------
#
G S#
X
////////////////////
S:(1,1) G:(-5,-5) Wall:[(2,1), (1,2), (0,1), (1,-6)] X:(0,0)
--------------------
#
#S#
X
G
#
////////////////////
S:(1,1) G:(1,-5) Wall:[(2,1), (1,3), (2,2)] X:(0,0)
--------------------
#
#
S#
X
G
////////////////////
S:(1,1) G:(5,-5) Wall:[(6,1), (5,3), (6,2)] X:(0,0)
--------------------
#
#
S #
X
G
////////////////////