AOJ 0037 Path on a Grid
こんにちは川です。
今回は普通に深さというか単純に解きました。
基本、左>前>右>後という感じで動いていくとしぜんかな。
最初に提出してからコード長を短くなるように頑張りましたが微妙ですね。
#include<bits/stdc++.h>
using
namespace
std;
string side[4],Longitudinal[5];
int
mox[2][2]={{1,-1},{0,-1}},moy[2][2]={{-1,1},{-1,0}};
char
str[5]=
"URDL"
;
void
serch(
int
x,
int
y,
int
then)
{
if
(then==-1)then=3;
if
(!x&&!y){
cout<<endl;
exit
(0);
}
for
(
int
i=0;i<4;i++){
int
now=(then+i)%4,ka=now/2;
if
(!(now%2)&&y+moy[0][ka]<5
&&
y+moy[0][ka]>-1&&side[y+moy[1][ka]][x]==
'1'
){
cout<<str[now];
serch(x,y+moy[0][ka],now-1);
}
if
(now%2&&x+mox[0][ka]<5&&
x+mox[0][now/2]>-1&&Longitudinal[y][x+mox[1][ka]]==
'1'
){
cout<<str[now];
serch(x+mox[0][ka],y,now-1);
}
}
}
int
main()
{
for
(
int
i=0;i<4;i++)cin>>Longitudinal[i]>>side[i];
cin>>Longitudinal[4];
cout<<
"R"
;
serch(1,0,0);
}