text
stringlengths
49
983k
#include<iostream> #include<vector> #include<cmath> #include<algorithm> using namespace std; vector<string> strs; int H,W; bool isroad(int fx,int fy,int tx,int ty){ int fromx,fromy,tox,toy; fromx=min(fx,tx); fromy=min(fy,ty); tox=max(fx,tx); toy=max(fy,ty); if(fromx<1 || tox<1 || tox>W || fromx>W || fromy<1 || toy<1 || fromy>H || toy>H) return false; if(fromx==tox){ if(strs[(fromy-1)*2+1][fromx-1]=='1') return true; else{ return false; } }else{ if(strs[(fromy-1)*2][fromx-1]=='1') return true; else{ return false; } } } int vx[]={1,0,-1,0},vy[]={0,-1,0,1}; int main(){ string ins; while(cin>>ins){ strs.push_back(ins); } W=strs[0].size()+1; H=strs.size()/2+1; int nx=2,ny=1,v=0; string outs="R"; while(nx!=1 || ny!=1){ for(int i=5;i>=2;i--){ bool r=isroad(nx,ny,nx+vx[(v+i)%4],ny+vy[(v+i)%4]); if(r){ v=(v+i)%4; nx+=vx[v]; ny+=vy[v]; if(v==0) outs+='R'; else if(v==1) outs+='U'; else if(v==2) outs+='L'; else outs+='D'; break; } } } cout<<outs<<endl; }
#include <bits/stdc++.h> using namespace std; double eps = 1e-10; int main() { string s[16], ans("R"); for(int i = 0; i < 9; ++i){ cin >> s[i]; } int x = 0, y = 0; int d = 0; // 0:right, 1:down, 2:left, 3:up bool flag = false; while(1){ if(d == 0){ if(y >= 2 && s[y - 1][x + 1] == '1'){ ans += 'U'; y -= 1; x += 1; d = 3; } else if(s[y][x + 1] == '1'){ ans += 'R'; x += 1; } else if(s[y + 1][x + 1] == '1'){ ans += 'D'; x += 1; y += 1; d = 1; } else{ ans += 'L'; d = 2; } } else if(d == 1){ if(s[y + 1][x] == '1'){ ans += 'R'; y += 1; d = 0; } else if(s[y + 2][x] == '1'){ ans += 'D'; y += 2; } else if(s[y + 1][x - 1] == '1'){ ans += 'L'; x -= 1; y += 1; d = 2; } else{ ans += 'U'; d = 3; } } else if(d == 2){ if(s[y + 1][x] == '1'){ ans += 'D'; y += 1; d = 1; } else if(s[y][x - 1] == '1'){ ans += 'L'; x -= 1; } else if(y >= 2 && s[y - 1][x] == '1'){ ans += 'U'; y -= 1; d = 3; } else{ ans += 'R'; d = 0; } } else if(d == 3){ if(s[y - 1][x - 1] == '1'){ ans += 'L'; y -= 1; x -= 1; d = 2; } else if(y >= 3 && s[y - 2][x] == '1'){ ans += 'U'; y -= 2; } else if(s[y - 1][x] == '1'){ y -= 1; if(x == 0 && y == 0) break; ans += 'R'; d = 0; } else{ ans += 'D'; d = 1; } } /*if(x == 1 && y == 8 && flag) break; if(x == 1 && y == 8){ flag = true; //break; }*/ if(x == 0 && y == 0) break; } /*cout << s[8][3] << endl; cout << d << endl;*/ cout << ans << endl; return 0; }
#include<iostream> #include<string> #include<cstdio> using namespace std; char walls[5][5][2]; int main() { int x=0,y=0,flag=1; string tmp; for(int i = 0; i < 9; ++i) { flag=(flag+1)%2; cin>>tmp; for(int j = 0; j < 5; ++j) { if(!flag&&j==4) walls[i/2][4][flag]='0'; else walls[i/2][j][flag]=tmp[j]; //cin>>walls[i/2][j][flag]; } } /*cout<<"debug"<<endl; for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) { cout<<" "<<walls[i][j][0]; } cout<<endl; } cout<<endl; for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) { cout<<" "<<walls[i][j][1]; } cout<<endl; }*/ //cout<<"end"<<endl; int dir=0; bool change=false; do { if(dir==0) { if(!change&&0<=y-1&&walls[y-1][x][1]=='1') { dir=3; change=true; } else { if(x==4||walls[y][x][0]=='0') { dir=1; change=true; } else { ++x; cout<<"R"; change=false; } } } else if(dir==1) { if(!change&&x<4&&walls[y][x][0]=='1') { dir=0; change=true; } else { if(y==4||walls[y][x][1]=='0') { dir=2; change=true; } else { ++y; cout<<"D"; change=false; } } } else if(dir==2) { if(!change&&walls[y][x][1]=='1') { dir=1; change=true; } else { if(x==0||walls[y][x-1][0]=='0') { dir=3; change=true; } else { --x; cout<<"L"; change=false; } } } else if(dir==3) { if(!change&&0<=x-1&&walls[y][x-1][0]=='1') { dir=2; change=true; } else { if(y==0||walls[y-1][x][1]=='0') { dir=0; change=true; } else { --y; cout<<"U"; change=false; } } } //cout<<",x="<<x<<",y="<<y<<",dir="<<dir<<endl; } while(change||x!=0||y!=0); cout<<endl; return 0; }
#include <iostream> using namespace std; int main() { char c; bool a[10][5]; bool b[5][5][4]; char label[4] = {'R', 'D', 'L', 'U'}; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int x, y, d, nd; for (int i=0; i<9; i++) { int r = (i%2) ? 5 : 4; for (int j=0; j<r; j++) { cin >> c; a[i][j] = c - '0'; } } for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { b[i][j][0] = (j<4) ? a[2*i][j] : false; b[i][j][1] = (i<4) ? a[2*i+1][j] : false; b[i][j][2] = (j) ? a[2*i][j-1] : false; b[i][j][3] = (i) ? a[2*i-1][j] : false; } } d = 0; cout << label[d]; x = 0; y = 1; do { for (int i=0; i<4; i++) { nd = (d+i+3)%4; if (b[x][y][nd]) { d = nd; break; } } cout << label[d]; x += dx[d]; y+= dy[d]; } while (x || y); cout << endl; return 0; }
#include<iostream> #include<string> using namespace std; int main(){ char kc[6][6][4]; int kabe[6][6][4]; for(int i=0;i<6;i++){ for(int j=0;j<6;j++){ for(int k=0;k<4;k++){ kc[i][j][k]=' '; kabe[i][j][k]=0;}}} for(int i=0;i<4;i++){ scanf("%c%c%c%c\n%c%c%c%c%c\n",&kc[i+1][1][1],&kc[i+1][2][1],&kc[i+1][3][1],&kc[i+1][4][1],&kc[i+1][1][2],&kc[i+1][2][2],&kc[i+1][3][2],&kc[i+1][4][2],&kc[i+1][5][2]);} scanf("%c%c%c%c\n",&kc[5][1][1],&kc[5][2][1],&kc[5][3][1],&kc[5][4][1]); for(int i=1;i<6;i++){ for(int j=1;j<6;j++){ if(kc[i][j][1]=='1'){kabe[i][j][1]=1;} if(kc[i][j][2]=='1'){kabe[i][j][2]=1;} } } for(int i=1;i<6;i++){ for(int j=1;j<6;j++){ kabe[i][j][0]=kabe[i-1][j][2]; kabe[i][j][3]=kabe[i][j-1][1];}} int muki=1; int muki2; int iti[2]={1,1}; int idou[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; string s=""; char a[4]={'U','R','D','L'}; while(1){ for(int i=3;i<7;i++){ muki2=muki+i; muki2%=4; if(kabe[iti[0]][iti[1]][muki2]==1){ s+=a[muki2]; muki=muki2; iti[0]+=idou[muki][0]; iti[1]+=idou[muki][1]; break;}} if(iti[0]==1&&iti[1]==1){break;}} cout<<s<<endl;}
#include <iostream> #include <sstream> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <memory.h> #include <cassert> using namespace std; #define all(c) ((c).begin()), ((c).end()) #define debug(c) cerr << "> " << #c << " = " << (c) << endl; #define iter(c) __typeof((c).begin()) #define present(c, e) ((c).find((e)) != (c).end()) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) #define mp make_pair #define fst first #define snd second #define pb push_back const double EPS = 1e-10; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef complex<double> P; int main() { int g[100][100]; memset(g, 0, sizeof(g)); rep(i, 9) rep(j, 4 + i % 2) { char c; cin >> c; g[i + 1][j * 2 + (i + 1) % 2 + 1] = c - '0'; } int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; char c[] = "URDL"; int x = 2, y = 0, v = 1; do { cout << c[v]; int pv = (v - 1 + 4) % 4; int nv = (v + 1) % 4; int nnv = (v + 2) % 4; if (g[y + dy[v]][x + dx[v]] == 1) { v = pv; } else if (g[y + dy[v] * 2 + dy[nv]][x + dx[v] * 2 + dx[nv]] == 1) { x += dx[v] * 2; y += dy[v] * 2; } else if (g[y + dy[v] * 2 + dy[nv] * 2 + dy[nnv]][x + dx[v] * 2 + dx[nv] * 2 + dx[nnv]] == 1) { x += dx[v] * 2; x += dx[nv] * 2; y += dy[v] * 2; y += dy[nv] * 2; v = nv; } else { x += dx[nv] * 2; y += dy[nv] * 2; v = nnv; } if (x == 0 && y == 0) break; if (x == 2 && y == 0) break; } while(true); cout << endl; return 0; }
#include <iostream> using namespace std; char w[10][8]; int dd[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}, k; char dr[8] = "RDLU"; int pos[2] = {0,1}; bool existWall(int i){ int rem = (k+i+4)%4; if( pos[0] == 0 && rem == 3 ) return false; if( pos[1] == 0 && rem == 2 ) return false; if( rem%2 ){ return w[2*pos[0]+2-rem][pos[1]] == '1'; }else{ return w[2*pos[0]][pos[1]-rem/2] == '1'; } } int main(){ for(int i = 0; i < 9; i++){ cin >> w[i]; } cout << 'R'; k = 0; while( pos[0] != 0 || pos[1] != 0 ){ for(int i = -1; i < 3; i++){ if( existWall(i) ){ k = (k+i+4)%4; break; } } pos[0] += dd[k][0]; pos[1] += dd[k][1]; cout << dr[k]; } cout << endl; return 0; }
#include<iostream> #include<string> using namespace std; #define rep(i, n) for ( int i = 0; i < n; i++) #define N 5 int main(){ bool G[N][N][4]; char ch; rep(i, N) rep(j, N) rep(k, 4) G[i][j][k] = false; rep(i, 2*N-1){ if (i%2){ rep(j, N) { cin >> ch; if (ch == '1') G[(i+1)/2-1][j][3] = G[(i+1)/2][j][1] = true; } } else { rep(j, N-1) { cin >> ch; if (ch == '1') G[i/2][j][0] = G[i/2][j+1][2] = true; } } } string D = "RULD"; int di[4] = {0, -1, 0, 1}; int dj[4] = {1, 0, -1, 0}; int pi = 0, pj = 0, dir = 0; while(1){ if ( G[pi][pj][(dir+1)%4] ){ dir = (dir+1)%4; pi += di[dir]; pj += dj[dir]; cout << D[dir]; } else if ( G[pi][pj][dir] ){ pi += di[dir]; pj += dj[dir]; cout << D[dir]; } else dir = (dir+3)%4; if ( pi == 0 && pj == 0 ) break; } cout << endl; }
#include <iostream> #include <string> #include <cstdlib> using namespace std; const static int SIZE = 11; const static int H_LEN = 4; const static int V_LEN = 5; int main() { int field [ SIZE ][ SIZE ] = { 0 }; for( int i = 0; i < SIZE; i++ ) { if( i == 0 || i == SIZE - 1 ) { continue; } string line; cin >> line; if( i % 2 ) { for( int j = 0; j < H_LEN; j++ ) { field[ i ][ 2 + j * 2 ] = line[ j ] - '0'; } } else { for( int j = 0; j < V_LEN; j++ ) { field[ i ][ 1 + j * 2 ] = line[ j ] - '0'; } } } /* for( int i = 0; i < SIZE; i++ ) { for( int j = 0; j < SIZE; j++ ) { if( i % 2 ) { cout << ( field[ i ][ j ] ? string("-") : string(".") ) << " " << flush; } else { cout << ( field[ i ][ j ] ? string("|") : string(".") ) << " " << flush; } } cout << endl; } */ int counter = 0; int dir = 0; int x = 1, y = 1; int vx[ 4 ] = { 2, 0, -2, 0 }; int vy[ 4 ] = { 0, 2, 0, -2 }; int ax[ 4 ] = { 2, 1, -2, -1 }; int ay[ 4 ] = { -1, 2, 1, -2 }; int bx[ 4 ] = { 3, 0, -3, 0 }; int by[ 4 ] = { 0, 3, 0, -3 }; int cx[ 4 ] = { 2, -1, -2, 1 }; int cy[ 4 ] = { 1, 2, -1, -2 }; do { if( field[ y + ay[ dir ] ][ x + ax[ dir ] ] ) { x += vx[ dir ]; y += vy[ dir ]; cout << ( dir == 0 ? "R" : dir == 1 ? "D" : dir == 2 ? "L" : "U" ) << flush; dir = ( dir - 1 + 4 ) % 4; } else if( field[ y + by[ dir ] ][ x + bx[ dir ] ] ) { x += vx[ dir ]; y += vy[ dir ]; cout << ( dir == 0 ? "R" : dir == 1 ? "D" : dir == 2 ? "L" : "U" ) << flush; } else if( field[ y + cy[ dir ] ][ x + cx[ dir ] ] ) { x += vx[ dir ]; y += vy[ dir ]; cout << ( dir == 0 ? "R" : dir == 1 ? "D" : dir == 2 ? "L" : "U" ) << flush; dir = ( dir + 1 ) % 4; } else { x += vx[ dir ]; y += vy[ dir ]; cout << ( dir == 0 ? "R" : dir == 1 ? "D" : dir == 2 ? "L" : "U" ) << flush; dir = ( dir + 2 ) % 4; } } while( x != 1 || y != 1 ); cout << endl; return EXIT_SUCCESS; }
#include <iostream> using namespace std; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = "RDLU"; int main() { int g[11][11] = {}; char c; for (int i=1; i<10; ++i) { int k = i % 2; // 0=vertical, 1=horizontal for (int j=1+k; j<=9-k; j+=2) { cin >> c; g[i][j] |= c - '0'; g[i-1+k][j-k] |= c - '0'; g[i+1-k][j+k] |= c - '0'; } } int x=1, y=1, d=0; do { for (int t=d+3; ; ++t) { if (g[y + dy[t%4]][x + dx[t%4]]) { d = t%4; break; } } x += dx[d] * 2; y += dy[d] * 2; cout << dc[d]; } while (x != 1 || y != 1); cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1,0,1,0}; const int dy[4] = {0,-1,0,1}; const char d[4] = {'L','U','R','D'}; int main(){ char ch; bool G[6][6][4]; memset(G,false,sizeof(G)); for(int i = 0 ; i < 9 ; i++){ int d = i%2; for(int j = 0 ; j < 4+d ; j++){ cin >> ch; if(ch == '1'){ if(d%2){ G[i/2+1][j][2] = G[i/2+1][j+1][0] = true; }else{ G[i/2][j+1][3] = G[i/2+1][j+1][1] = true; } } } } int x = 1,y = 0,dir = 2; string ans; while(true){ if(!x && !y) break; ans += d[dir]; int rh = (dir+1)%4; if(G[y][x][dir]){ dir--; dir = (dir == -1 ? 3 : dir); }else if(!G[y+dy[dir]][x+dx[dir]][rh]){ x += dx[dir], y += dy[dir]; if(!x && !y) break; x += dx[rh], y += dy[rh]; dir = rh; int nrh = (rh+1)%4; if(!G[y][x][nrh] && !(!x && !y)){ x += dx[nrh], y += dy[nrh]; dir = nrh; } }else{ x += dx[dir]; y += dy[dir]; } } cout << ans << endl; return 0; }
#include<stdio.h> char yokosen[7][6]; char tatesen[6][7]; int main(){ for(int i=0;i<9;i++)scanf("%s",i%2?tatesen[i/2+1]+1:yokosen[i/2+1]+1); int dir=100000; int row=1; int col=2; printf("R"); while(1){ // printf("\n%c %c",tatesen[row][col],yokosen[row][col]); switch(dir%4){ case 0://migi if(tatesen[row-1][col]=='1'){ dir--; row--; printf("U"); } else if(yokosen[row][col]=='1'){ col++; printf("R"); } else if(tatesen[row][col]=='1'){ row++; dir++; printf("D"); } else { col--; dir+=2; printf("L"); } break; case 1://shita if(yokosen[row][col]=='1'){ col++; dir--; printf("R"); } else if(tatesen[row][col]=='1'){ row++; printf("D"); } else if(yokosen[row][col-1]=='1'){ col--; dir++; printf("L"); } else{ dir+=2; row--; printf("U"); } break; case 2://hidari if(tatesen[row][col]=='1'){ row++; dir--; printf("D"); } else if(yokosen[row][col-1]=='1'){ col--; printf("L"); } else if(tatesen[row-1][col]=='1'){ dir++; row--; printf("U"); } else { col++; dir+=2; printf("R"); } break; case 3://ue if(yokosen[row][col-1]=='1'){ col--; dir--; printf("L"); } else if(tatesen[row-1][col]=='1'){ row--; printf("U"); } else if(yokosen[row][col]=='1'){ col++; dir++; printf("R"); } else{ dir+=2; row++; printf("D"); } break; } if(row==1&&col==1)break; } printf("\n"); }
#include <iostream> #include <stdio.h> using namespace std; int main() { char w[9][9],e[5]="URDL"; int x=0,y=0,h=1,i,g; int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0}; for (i=0;i<9;i++) gets(w[i]); while(true) { h=(h+3) % 4; for (i=0;i<4;i++) { if (y+dy[h]>=0 && y+dy[h]<=4 && x+dx[h]>=0 && x+dx[h]<=4) { if (dx[h]==-1) g=-1; else g=0; if (w[2*y+dy[h]][x+g]=='1') break; } h=(h+1) % 4; } x+=dx[h]; y+=dy[h]; cout << e[h]; if (x==0 && y==0) break; } cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; int a[12][12]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; char dc[4] = {'R','D','L','U'}; string f[4] = {"¨" , "«" , "©", "ª"}; //Ž©•ª‚ÌŽüˆÍ‚Sƒ}ƒX‚̕ǂ̏ó‘Ԃ𒲂ׂé void cheakWall(int* wall , const int& px ,const int& py ,const int& d ){ for(int i=0 ; i<4 ; ++i ){ int mx = px + dx[(d+i)%4]; int my = py + dy[(d+i)%4]; wall[i] = a[my][mx]; } } //i‚Þ void go(int& px, int& py, const int& d,int* wall, string& s){ px += dx[d]*2; py += dy[d]*2; //cheakWall( wall , px , py , d ); if( wall[1] ){ //i‚ñ‚¾Œü‚«‚ð‹L˜^ s.push_back( dc[d] ); } } int main(){ string s; //”z—ñ‚̏‰Šú‰» for(int y=0 ; y<12 ; ++y ) for(int x=0 ; x<12 ; ++x ) a[y][x] = 0; //“ü—Í for(int i=0 ; i<9 ; ++i ){ cin >> s; int y = i+1; int x = (i+1)%2 + 1; for(int j=0 ; j<(int)s.size() ; ++j , x+=2 ){ a[y][x] = s[j] - '0'; } } int px = 2, py = 0, d = 0; bool flag = false; s.clear(); while( 1 ){ //cout << "x:" << px << " y:" << py << " d:" << f[d] << endl; if( px == 0 && py == 0 ) break; int wall[4]; //Ž©•ª‚ÌŽü‚è‚̏ó‘Ô cheakWall( wall , px , py , d ); if( !wall[1] ){//‰E‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç //Œü‚«‚ð‰EŒü‚«‚ɉñ‚é d = (d+1)%4; //i‚Þ go( px , py , d , wall , s ); flag = false; }else{ if( !wall[0] ){//‘O‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç //i‚Þ go( px , py , d , wall , s ); flag = false; }else{ if( !wall[3] ){//¶‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç if( wall[1] && flag == false ){ //i‚ñ‚¾Œü‚«‚ð‹L˜^ s.push_back( dc[d] ); } //Œü‚«‚ð¶Œü‚«‚ɉñ‚é d = (d+3)%4; //i‚Þ go( px , py , d , wall , s ); flag = false; }else{//‘OE‰EE¶‚ª•Ç‚¾‚Á‚½‚ç flag = true; if( wall[1] ){ //i‚ñ‚¾Œü‚«‚ð‹L˜^ s.push_back( dc[d] ); } //Œü‚«‚ð‰EŒü‚«‚ɉñ‚é d = (d+3)%4; //i‚ñ‚¾Œü‚«‚ð‹L˜^ s.push_back( dc[d] ); } } } } cout << s << endl; }
#include<bits/stdc++.h> using namespace std; int di[4] = {0, -1, 0, 1}; int dj[4] = {-1, 0, 1, 0}; void print(int c){ if(c == 0) cout << "L"; else if(c == 1) cout << "U"; else if(c == 2) cout << "R"; else cout << "D"; } int main(){ vector<vector<bool> > a(13, vector<bool> (13, true)); for(int i = 0; i < 13; i++){ a[i][0] = a[i][12] = false; } for(int j = 0; j < 13; j++){ a[0][j] = a[12][j] = false; } for(int i = 0; i < 9; i++){ if(i % 2 == 0){ int h = i + 2; string s; cin >> s; for(int j = 0; j < 4; j++){ int w = j * 2 + 3; if(s[j] == '1'){ a[h][w - 1] = false; a[h][w] = false; a[h][w + 1] = false; } } }else{ int h = i + 2; string s; cin >> s; for(int j = 0; j < 5; j++){ int w = j * 2 + 2; if(s[j] == '1'){ a[h - 1][w] = false; a[h][w] = false; a[h + 1][w] = false; } } } } /*for(int i = 0; i < 13; i++){ for(int j = 0; j < 13; j++){ cout << a[i][j]; } cout << endl; }*/ int s_k = 2; int s_i = 2; int s_j = 2; int k = 2; int i = 2; int j = 2; bool flag = false; while(1){ if(flag && i == s_i && j == s_j) break; flag = true; for(int h = 0; h < 4; h++){ int c = (k - 1 + 4 + h) % 4; if(a[i + di[c]][j + dj[c]] == 0 && a[i + 2*di[c]][j + 2*dj[c]] == 0){ print(c); i = i + 2*di[c]; j = j + 2*dj[c]; k = c; break; } } } cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; bool wall[11][11]; class Cbot{ private: public: int x, y; int direc; string log; Cbot(); int Start(); }; Cbot::Cbot(){ x = 2; y = 0; direc = 1; log = ""; } int Cbot::Start(){ int rx[4] = { 1, 0, -1, 0 }; int ry[4] = { 0, 1, 0, -1 }; int fx[4] = { 0, 1, 0, -1 }; int fy[4] = { -1, 0, 1, 0 }; int dx[4] = { 0, 2, 0, -2 }; int dy[4] = { -2, 0, 2, 0 }; char logd[4] = { 'U', 'R', 'D', 'L' }; int cnt = 0; while (1){ //????????§?????? if (wall[y + ry[direc]][x + rx[direc]] == 1){ //????????§?????? log += logd[direc]; if (wall[y + fy[direc]][x + fx[direc]] == 0){ //?????? x += dx[direc]; y += dy[direc]; } else{ //????????? if (direc == 0)direc = 3; else direc--; } } else{ //?????????+?????? if (direc == 3)direc = 0; else direc++; x += dx[direc]; y += dy[direc]; } //??´???????????? if (x == 0 && y == 2 && direc == 3) break; if (x == 0 && y == 0 && direc == 0) break; if (cnt++ > 100) return 0; } return 1; } int main(){ int index = 1; char buf; Cbot bot; for (int i = 0; i < 11; i++){ for (int j = 0; j < 11; j++){ wall[i][j] = 0; } } //?????????????????? while (1){ for (int i = 0; i < 4; i++){ cin >> buf; wall[index][(i + 1) * 2] = buf - '0'; } index++; if (index == 10)break; for (int i = 0; i < 5; i++){ cin >> buf; wall[index][i * 2 + 1] = buf - '0'; } index++; } bot.Start(); cout << bot.log << endl; return 0; }
#include <iostream> using namespace std; int main(){ int f,xyf; char h[5][4],v[4][5]; int x,y; cin>>h[0]; for(int i=0;i<4;i++){ cin>>v[i]; cin>>h[i+1]; } f=xyf=1; y=0; x=1;cout<<"R"; while(x!=0||y!=0){ if(f==1){ if(xyf==1){ if(y>0&&v[y-1][x]=='1'){ y--;cout<<"U";f*=-1;xyf*=-1; } else if(x<4&&h[y][x]=='1'){ x++;cout<<"R"; } else if(v[y][x]=='0'||y==4) xyf*=-1; else{ y++;f*=-1;cout<<"D"; } } else{ if(y<4&&v[y][x]=='1'){ y++;cout<<"D";f*=-1;xyf*=-1; } else if(x>0&&h[y][x-1]=='1'){ x--;cout<<"L"; } else if(v[y-1][x]=='0'||y==0) xyf*=-1; else{ y--;cout<<"U";f*=-1; } } } else{ if(xyf==1){ if(x<4&&h[y][x]=='1'){ x++;cout<<"R";f*=-1; } else if(y<4&&v[y][x]=='1'){ y++;cout<<"D"; } else if(h[y][x-1]=='0'||x==0) xyf*=-1; else{ x--;cout<<"L";f*=-1;xyf*=-1; } } else{ if(x>0&&h[y][x-1]=='1'){ x--;cout<<"L";f*=-1; } else if(y>0&&v[y-1][x]=='1'){ y--;cout<<"U"; } else if(h[y][x]=='0'||x==4) xyf*=-1; else{ x++;cout<<"R";f*=-1;xyf*=-1; } } } } cout<<endl; return 0; }
#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"; //1,0,0 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); }
#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==0&&y==0){ cout<<endl; exit(0); } for(int i=0;i<4;i++){ int now=(then+i)%4,ka=now/2; if(now%2==0){ if(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); } } else{ if(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); }
#include <iostream> #include <string> using namespace std; string s[9]; int x,y,t; bool R() { if(x < 4 && s[y][x] == '1'){ cout << 'R'; t = 0; x++; return true; } return false; } bool L() { if(0 < x && s[y][x-1] == '1'){ cout << 'L'; t = 2; x--; return true; } return false; } bool U() { if(1 < y && s[y-1][x] == '1'){ cout << 'U'; t = 3; y -= 2; return true; } return false; } bool D() { if(y < 7 && s[y+1][x] == '1'){ cout << 'D'; t = 1; y += 2; return true; } return false; } int main() { bool (*move[])() = {U,R,D,L}; for(int i = 0; i < 9; ++i){ cin >> s[i]; } cout << 'R'; x = 1; while(!(x == 0 &&y == 0)){ for(int i =0; i < 4; ++i){ if(move[(t+i)%4]())break; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++) #define ALL(x) x.begin(),x.end() template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; } template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; } typedef long long ll; const int N = 5,M = 9; int edge [25] [25]; const int dx [] = {0,1,0,-1}; const int dy [] = {-1,0,1,0}; string d = "URDL"; bool in_range(int y,int x) { return y >= 0 && y < N && x >= 0 && x < N; } int main() { cin.tie(0); ios::sync_with_stdio(false); FOR(i,0,M){ string s; cin >> s; FOR(j,0,s.size()) if(s [j] == '1'){ if(i % 2 == 0){ edge [(i / 2) * N + j] [(i / 2) * N + j + 1] = true; edge [(i / 2) * N + j + 1] [(i / 2) * N + j] = true; } else{ edge [(i / 2) * N + j] [(i / 2 + 1) * N + j] = true; edge [(i / 2 + 1) * N + j] [(i / 2) * N + j] = true; } } } string ans(1,'R'); int y = 0,x = 1,dir = 1; while(y || x){ for(int i = 3;i <= 6;i++){ int ny = y + dy [(dir + i) % 4],nx = x + dx [(dir + i) % 4]; if(in_range(ny,nx) == false || edge [y * N + x] [ny * N + nx] == false) continue; y = ny; x = nx; ans += d [(dir + i) % 4]; (dir += i) %= 4; break; } } cout << ans << endl; return 0; }
#include <iostream> #include <bitset> using namespace std; enum Dir { U,R,D,L,DirSize }; int main() { bitset<DirSize+1> node[10][10]; for(int y=0; y<5; y++) for(int x=0; x<5; x++) { node[y*2 ][x*2 ].set(R); node[y*2 ][x*2 ].set(D); node[y*2 ][x*2+1].set(D); node[y*2 ][x*2+1].set(L); node[y*2+1][x*2+1].set(L); node[y*2+1][x*2+1].set(U); node[y*2+1][x*2 ].set(U); node[y*2+1][x*2 ].set(R); } node[0][0].reset(D); node[1][0].reset(U); char str[16]; for(int y=0; y<5; y++) { cin>>str; for(int x=0; x<4; x++) { if(str[x]=='1') { node[y*2 ][x*2+1].set(R); node[y*2 ][x*2+1].reset(D); node[y*2+1][x*2+1].set(R); node[y*2+1][x*2+1].reset(U); node[y*2 ][x*2+2].set(L); node[y*2 ][x*2+2].reset(D); node[y*2+1][x*2+2].set(L); node[y*2+1][x*2+2].reset(U); } } if(y==4) break; cin>>str; for(int x=0; x<5; x++) { if(str[x]=='1') { node[y*2+1][x*2 ].set(D); node[y*2+1][x*2 ].reset(R); node[y*2+1][x*2+1].set(D); node[y*2+1][x*2+1].reset(L); node[y*2+2][x*2 ].set(U); node[y*2+2][x*2 ].reset(R); node[y*2+2][x*2+1].set(U); node[y*2+2][x*2+1].reset(R); } } } int y=0,x=0; const int dx[]={ 0, 1, 0,-1}; const int dy[]={-1, 0, 1, 0}; const char dc[]={'U','R','D','L'}; bool end=false; while(!end) { end=true; for(int i=0; i<4; i++) { if(node[y][x][i]) { int nx=x+dx[i]; int ny=y+dy[i]; if(x/2!=nx/2 || y/2!=ny/2) { cout << dc[i]; } x=nx; y=ny; node[y][x].reset((i+2)%4); end=false; break; } } } cout << endl; }
#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); }
#define _USE_MATH_DEFINES #include <iostream> #include <iomanip> #include <cctype> #include <algorithm> #include <functional> #include <vector> #include <cstdio> #include <cstring> #include <cmath> #include <cfloat> #include <map> #include <queue> #include <stack> #include <list> #include <string> #include <set> #include <complex> #include <utility> #include <numeric> #include <bitset> using namespace std; const int INF=1<<30; int d[11][7]; int x=2,y=1; int k=0; typedef pair<int,int> P; void R(){ if(d[y-1][x]) { k=2; y-=2; cout<<"U"; } else if(d[y][x]) { k=0; x++; cout<<"R"; } else if(d[y+1][x]){ k=3; y+=2; cout<<"D"; } else{ k=1; x--; cout<<"L"; } } void L(){ if(d[y+1][x]) { k=3; y+=2; cout<<"D"; } else if(d[y][x-1]) { k=1; x--; cout<<"L"; } else if(d[y-1][x]){ k=2; y-=2; cout<<"U"; } else{ k=0; x++; cout<<"R"; } } void U(){ if(d[y][x-1]) { k=1; x--; cout<<"L"; } else if(d[y-1][x]) { k=2; y-=2; cout<<"U"; } else if(d[y][x]){ k=0; x++; cout<<"R"; } else{ k=3; y+=2; cout<<"D"; } } void D(){ if(d[y][x]) { k=0; x++; cout<<"R"; } else if(d[y+1][x]) { k=3; y+=2; cout<<"D"; } else if(d[y][x-1]){ k=1; x--; cout<<"L"; } else{ k=2; y-=2; cout<<"U"; } } void tansaku(){ while(!(x==1&&y==1)){ switch(k){ case 0: R();break; case 1: L();break; case 2: U();break; case 3: D();break; default:return; } } } int main(){ string s; for(int i=1;i<10;i++){ getline(cin,s); if(s[s.size()-1]=='\r') s.substr(0,s.size()-1); for(int j=0;j<s.size();j++){ if(i%2!=0){ d[i][j+1]=s[j]-'0'; } else{ d[i][j+1]=s[j]-'0'; } } } cout<<"R"; tansaku(); cout<<endl; }
#include <iostream> #include <string> #include <vector> const int horz_node_count = 5; const int vert_node_count = 5; const int direction_count = 4; // [0]:left, [1]:up, [2]:right [3]:down enum { left, up, right, down }; int index(int x, int y, int direction) { return (y * horz_node_count + x) * direction_count + direction; } void read_data(int* walls) { std::vector<std::string> input; for (int i = 0; i < vert_node_count * 2 - 1; ++i) { std::string line; if (std::getline(std::cin, line)) { input.push_back(line); } } std::fill_n(walls, horz_node_count * vert_node_count * direction_count, 0); for (int y = 0; y < vert_node_count; ++y) { for (int x = 0; x < horz_node_count - 1; ++x) { if (input[y*2][x] == '1') { walls[index(x,y,right)] = 1; walls[index(x+1,y,left)] = 1; } } } for (int y = 0; y < vert_node_count - 1; ++y) { for (int x = 0; x < horz_node_count; ++x) { if (input[y*2+1][x] == '1') { walls[index(x,y,down)] = 1; walls[index(x,y+1,up)] = 1; } } } } bool wall_exists(const int* walls, int x, int y, int direction) { return (0 <= x && x < horz_node_count) && (0 <= y && y < vert_node_count) && (walls[index(x,y,direction)] != 0); } int main() { int walls[horz_node_count * vert_node_count * direction_count] = {}; read_data(walls); int x = 0, y = 0, direction = right; do { // determine the next direction direction = (direction + direction_count - 1) % direction_count; for (int i = 0; i < 4; ++i) { if (wall_exists(walls, x, y, direction)) { break; } direction = (direction + 1) % direction_count; } // step forward switch (direction) { case left: --x; std::cout << 'L'; break; case up: --y; std::cout << 'U'; break; case right: ++x; std::cout << 'R'; break; case down: ++y; std::cout << 'D'; break; } } while (x > 0 || y > 0); std::cout << std::endl; return 0; }
#include<string> #include<vector> #include<iostream> using namespace std; bool map[20][20][4]; vector< string > maphor,mapver; int xsize,ysize; void init(){ for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ for(int k=0;k<4;k++){ map[i][j][k]=0; } } } } void setmap(){ for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ if(-1<i&&i<maphor.size()&&-1<j&&j<maphor[0].size()){ if(maphor[i][j]=='1'){ map[i][j][0]=1; } } if(-1<i&&i<maphor.size()&&-1<j-1&&j-1<maphor[0].size()){ if(maphor[i][j-1]=='1'){ map[i][j][2]=1; } } if(-1<i&&i<mapver.size()&&-1<j&&j<mapver[0].size()){ if(mapver[i][j]=='1'){ map[i][j][3]=1; } } if(-1<i-1&&i-1<mapver.size()&&-1<j&&j<mapver[0].size()){ if(mapver[i-1][j]=='1'){ map[i][j][1]=1; } } } } } int solve(){ int x=0,y=0; int v=0; string vec="RULD"; int loop[4]={1,0,3,2}; int vx[4]={1,0,-1,0}; int vy[4]={0,-1,0,1}; int count=0; while(1){ count++; if(count>100)return -1; for(int i=0;i<5;i++){ if(i==4)return -1; if(x==0 && y==0 &&(((v+loop[i])%4)==2)){ return 0; } if(map[y][x][(v+loop[i])%4]==1){ v+=loop[i]; v=v%4; x+=vx[v]; y+=vy[v]; cout<<vec[v]; break; } } } } int main(){ string tmp; for(int i=0;cin>>tmp;i++){ if(i%2==0){ maphor.push_back(tmp); }else{ mapver.push_back(tmp); } } xsize=maphor[0].size()+1; ysize=mapver.size()+1; init();setmap(); /* for(int i=0;i<xsize;i++){ for(int j=0;j<ysize;j++){ for(int k=0;k<4;k++){ cout<<map[i][j][k]; } cout<<" "; } cout<<endl; } */ if(solve()==-1)return -1; cout<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; string side[4],Longitudinal[5]; void righ(int x,int y); void lef(int x,int y); void up(int x,int y); void down(int x,int y); void serch(int x,int y,int then); int main() { for(int i=0;i<4;i++)cin>>Longitudinal[i]>>side[i]; cin>>Longitudinal[4]; cout<<"R"; serch(1,0,0); } void righ(int x,int y) { if(x+1<5&&Longitudinal[y][x]=='1'){ cout<<"R"; serch(x+1,y,0); } } void lef(int x,int y) { if(x-1>-1){ if(Longitudinal[y][x-1]=='1'){ cout<<"L"; serch(x-1,y,2); } } } void up(int x,int y) { if(y-1>-1){ if(side[y-1][x]=='1'){ cout<<"U"; serch(x,y-1,3); } } } void down(int x,int y) { if(y+1<5&&side[y][x]=='1'){ cout<<"D"; serch(x,y+1,1); } } int mox[2][4]={{0,1,0,-1},{0,0,0,-1}},moy[2][4]={{-1,0,1,0},{-1,0,0,0}}; char str[5]="URDL"; void serch(int x,int y,int then) { if(then==-1)then=3; if(x==0&&y==0){ cout<<endl; exit(0); } for(int i=0;i<4;i++){ int now=(then+i)%4; if(now%2==0){ if(y+moy[0][now]<5&&y+moy[0][now]>-1&&side[y+moy[1][now]][x]=='1'){ cout<<str[now]; serch(x,y+moy[0][now],now-1); } } else{ if(x+mox[0][now]<5&&x+mox[0][now]>-1&&Longitudinal[y][x+mox[1][now]]=='1'){ cout<<str[now]; serch(x+mox[0][now],y,now-1); } } } }
#include<stdio.h> char f[6][6],i,j,a,y,x,p; int main() { for(;i<9;++i){ scanf("%s",f[5]); for(j=0;j<4+i%2;++j){ a=f[5][j]-48; f[i/2][j]|=a<<(1+i%2); f[i/2+i%2][j+!(i%2)]|=a<<3-i%2*3; } } do{ while(!((f[y][x]>>(p=(p+1)%4))&1)); putchar("URDL"[p]); x+=p?2-p:0; y+=p==3?0:p-1; p=(p+2)%4; }while(y!=0||x!=0||f[0][0]==6&&p==3); return !puts(""); }
#include<iostream> #include<string> #include<sstream> enum direction { L, R, U, D }; int main() { int wall[9][5]; for (int i = 0; i < 9; i++) { std::string foo; std::cin >> foo; std::stringstream bar(foo); for (int j = 0;; j++) { char baz; bar >> baz; wall[i][j] = static_cast<int>(baz - '0'); if (i % 2 == 0 && j == 3 || j == 4) { break; } } } direction dir = R; std::cout << 'R'; int crd[2] = { 0,0 }; while (true) { switch (dir) { case L: if (crd[1] != 8 && wall[crd[1] + 1][crd[0]] == 1) { crd[1]++; dir = D; std::cout << 'D'; } else if (crd[0] == 0 || wall[crd[1]][crd[0] - 1] == 0) { if (wall[crd[1] - 1][crd[0]] == 0) { dir = R; std::cout << 'R'; } else { crd[1]--; dir = U; std::cout << 'U'; } } else { crd[0]--; std::cout << 'L'; } break; case R: if (crd[1] != 0 && wall[crd[1] - 1][crd[0] + 1] == 1) { crd[0]++; crd[1]--; dir = U; std::cout << 'U'; } else if (crd[0] == 3 || wall[crd[1]][crd[0] + 1] == 0) { if (wall[crd[1] + 1][crd[0] + 1] == 0) { dir = L; std::cout << 'L'; } else { crd[0]++; crd[1]++; dir = D; std::cout << 'D'; } } else { crd[0]++; std::cout << 'R'; } break; case U: if (crd[0] != 0 && wall[crd[1] - 1][crd[0] - 1] == 1) { crd[0]--; crd[1]--; dir = L; std::cout << 'L'; } else if (crd[1] == 1 || wall[crd[1] - 2][crd[0]] == 0) { if (wall[crd[1] - 1][crd[0]] == 0) { dir = D; std::cout << 'D'; } else { crd[1]--; dir = R; std::cout << 'R'; } } else { crd[1] -= 2; std::cout << 'U'; } break; case D: if (crd[0] != 4 && wall[crd[1] + 1][crd[0]] == 1) { crd[1]++; dir = R; std::cout << 'R'; } else if (crd[1] == 7 || wall[crd[1] + 2][crd[0]] == 0) { if (wall[crd[1] + 1][crd[0] - 1] == 0) { dir = U; std::cout << 'U'; } else { crd[0]--; crd[1]++; dir = L; std::cout << 'L'; } } else { crd[1] += 2; std::cout << 'D'; } break; } if (crd[0] == 0 && (crd[1] == 0 || crd[1] == 1)) { std::cout << std::endl; break; } } return 0; }
#include<iostream> #include<iomanip> #include<algorithm> #include<array> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdio> #include<cstring> #include<functional> #include<limits> #include<list> #include<map> #include<numeric> #include<set> #include<stack> #include<string> #include<sstream> #include<unordered_map> #include<queue> #include<vector> using namespace std; //#define int long long using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(a) (a).begin(),(a).end() #define dump(o) {cerr<<#o<<" "<<o<<endl;} #define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;} #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL const int MOD = 1e9 + 7; int dx[] = { -1,0,1,0 }; int dy[] = { 0,-1,0,1 }; string LURD = "LURD"; bool M[16][16][4]; //????????¨?????????????????? int main() { rep(i, 0, 9) { if (i % 2) { rep(j, 0, 5) { char c; cin >> c; if (c == '1') { M[i / 2][j][3] = true; M[i / 2 + 1][j][1] = true; } } } else { rep(j, 0, 4) { char c; cin >> c; if (c == '1') { M[i / 2][j][2] = true; M[i / 2][j + 1][0] = true; } } } } int x = 0, y = 0, dir = 2, cnt = 0; do { rep(i, 0, 4) { //?????????????????????????£???????????????????????????????????????????????????????????????? int dir2 = (dir + 3 + i) % 4; if (M[y][x][dir2]) { x += dx[dir2]; y += dy[dir2]; dir = dir2; cout << LURD[dir2]; break; } } } while (x || y); cout << endl; return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <sstream> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <utility> #include <cctype> #include <numeric> #include <cassert> using namespace std; #define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i)) #define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++) char ver[10][10], hol[10][10]; const int dx[] = {-1,0,1,0}; const int dy[] = {0,1,0,-1}; const char dir[] = {'U','R','D','L'}; void input() { scanf("%s", hol[1]+1); rep(i,4) { scanf("%s", ver[i+1]+1); scanf("%s", hol[i+2]+1); } rep(i,8) rep(j,8) { if (hol[i][j] == '1') hol[i][j] = 1; else hol[i][j] = 0; if (ver[i][j] == '1') ver[i][j] = 1; else ver[i][j] = 0; } // rep(i,8) { // putchar(' '); // rep(j,8) printf("%2d", hol[i][j]); puts(""); // rep(j,8) printf("%2d", ver[i][j]); puts(""); // } } bool is_end(int x, int y, int d) { return (x == 0 && y == 1 && d == 0) || (x == 1 && y == 1 && d == 3); } void update(int &x, int &y, int &d) { const int bx = x, by = y, bd = d; switch (d) { case 0: { if (hol[y][x]) d = 3; else if (ver[y-1][x+1]) --y; else if (hol[y][x+1]) { ++x; --y; d = 1; } else { d = 2; ++x; } break; } case 1: { if (ver[y][x+1]) d = 0; else if (hol[y+1][x+1]) ++x; else if (ver[y+1][x+1]) { ++x; ++y; d = 2; } else { d = 3; ++y; } break; } case 2: { if (hol[y+1][x]) d = 1; else if (ver[y+1][x]) ++y; else if (hol[y+1][x-1]) { --x; ++y; d = 3; } else { d = 0; --x; } break; } case 3: { if (ver[y][x]) d = 2; else if (hol[y][x-1]) --x; else if (ver[y-1][x]) { --x; --y; d = 0; } else { d = 1; --y; } break; } } if (bx == x && by == y && bd == d) { printf("%d %d %d\n", x, y, d); assert(false); } } void solve() { int x = 1, y = 0, d = 1; string ans = "R"; do { // printf("%d,%d - %d : %s\n", x, y, d, ans.c_str()); update(x, y, d); ans += dir[d]; } while (!is_end(x,y,d)); printf("%s\n", ans.c_str()); } int main() { input(); solve(); return 0; }
#include<stdio.h>//////////AOJ0037 int i,j,w[5][5];char*p,b[1<<7];; int F(int(v)){scanf("%s",b);for( p=b ;*p;++p ){w[ p-b +1-v][j +v]|=((w[p- b][j ]|= (*p==49 )<<3-v)&8>> v)>> 2;} return( j+=v );/* */} int main(){ for( ;4-F (0); F(1 ));for( p=b, i=j= (*b= 3)^ 3;i+="" "BA" "BC" [*b ]-66,j= (*b)["ABCB"]-66+j, *++ p="ULDR "[*b],i+j;)for((* b+= 1)&=~4; !(w [i][j]&(1<<*b));(*b+=3)&=3);return !puts(b+1);}/*May2018,tw:@siikya*/
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <stack> #include <vector> #include <map> #include <set> #include <iostream> using namespace std; int main() { bool points[5][5][4]; for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) for (int k = 0; k < 4; ++k) points[i][j][k] = false; for (int i = 0; i < 9; ++i) { int y = i/2; if (i%2 == 0) { for (int j = 0; j < 4; ++j) { int e; scanf("%1d", &e); if (e) points[y][j][1] = points[y][j+1][3] = true; } } else { for (int j = 0; j < 5; ++j) { int e; scanf("%1d", &e); if (e) points[y][j][2] = points[y+1][j][0] = true; } } } int x, y; x = 1; y = 0; int dir = 1; int dx[] = { 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0 }; char* ds = "URDL"; putchar(ds[dir]); while (!(x == 0 && y == 0)) { if (points[y][x][(dir+3)%4]) dir = (dir+3)%4; else if (points[y][x][dir]) ; else if (points[y][x][(dir+1)%4]) dir = (dir+1)%4; else dir = (dir+2)%4; x += dx[dir]; y += dy[dir]; putchar(ds[dir]); } putchar('\n'); return 0; }
#include <iostream> #include <algorithm> using namespace std; const char mark[4] = {'R', 'D', 'L', 'U'}; const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int main(void) { bool w[5][9]; for (int i = 0; i < 9; i++) { int x = 4 + i % 2; for (int j = 0; j < x; j++) { char c; cin >> c; w[j][i] = (c == '1'); } } int x = 0, y = 0, d = 0; do { int dx, dy, _x, _y; d--; if (d < 0) d += 4; for (int i = 0; i < 4; i++) { dx = dir[d][0]; dy = dir[d][1]; _x = x + dx; _y = y + dy; if (_x >= 0 && _x <= 4 && _y >= 0 && _y <= 4) { if (d % 2 == 0 && w[min(x, _x)][y * 2]) break; if (d % 2 == 1 && w[x][min(y, _y) * 2 + 1]) break; } d++; d %= 4; } x = _x; y = _y; cout << mark[d]; } while (!(x == 0 && y == 0)); cout << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; #define rep(i,n) for(int i=0;i<int(n);i++) void input(bool grid[][9]) { rep(i,9){ string s; cin>>s; if(i%2==0) rep(j,4) grid[i][j*2+1]=s[j]-'0'; else rep(j,5) grid[i][j*2]=s[j]-'0'; } } enum dir{ UP,RIGHT,DOWN,LEFT }; const int di[]={-1,0,1,0}; const int dj[]={0,1,0,-1}; const char output[]="URDL"; bool in_range(int i,int j) { return 0<=i && i<9 && 0<=j && j<9; } int main() { bool grid[9][9]={{0}}; input(grid); int i=0,j=0; dir prev=RIGHT; for(;;){ dir next[4]={LEFT,UP,RIGHT,DOWN}; rotate(next,next+prev,next+4); int k; for(k=0;k<3;k++){ if(in_range(i+di[next[k]],j+dj[next[k]]) && grid[i+di[next[k]]][j+dj[next[k]]]) break; } cout<<output[next[k]]; i+=di[next[k]]*2; j+=dj[next[k]]*2; prev=next[k]; if(i==0 && j==0) break; } cout<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using db = double; using ll = long long; using vi = vector <int>; #define op operator #define pb push_back int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; const int N = 11; bool a[N][N][4]; char c[4] = {'R', 'D', 'L', 'U'}; int main() { //ios :: sync_with_stdio(0); for(int i = 0; i < 9; i ++) { string s; cin >> s; if(i & 1) { for(int j = 0; j < 5; j ++) if(s[j] == '1') { a[i / 2][j][1] = 1; a[i / 2 + 1][j][3] = 1; } } else { for(int j = 0; j < 4; j ++) if(s[j] == '1') { a[i / 2][j][0] = 1; a[i / 2][j + 1][2] = 1; } } } int x = 0, y = 0, d = 0; for(;;) { d = (d + 3) % 4; while(!a[x][y][d]) d = (d + 1) % 4; //cout << x << ' ' << y << ' ' << d << '\n'; x += dx[d]; y += dy[d]; cout << c[d]; if(!x && !y) break; } cout << '\n'; return 0; }
#include <iostream> #include <string> using namespace std; enum eCOURSE{ COURSE_U, COURSE_R, COURSE_D, COURSE_L, }; struct tDataSet{ int *pX, *pY, *field; eCOURSE* course; }; const int SIZE_X = 7; const int SIZE_Y = 11; bool checkGoal( tDataSet& data ){ if ( ( *data.pX == 0 && *data.pY == 1 && *data.course == COURSE_U && data.field[ 2*SIZE_X + 1 ] ) || ( *data.pX == 1 && *data.pY == 1 && *data.course == COURSE_L && data.field[ 1*SIZE_X + 1 ] && !data.field[ 2*SIZE_X + 1 ] ) ){ return true; } return false; } int main(){ string s; int field[ SIZE_Y * SIZE_X ]; for ( int i = 0; i < SIZE_Y * SIZE_X; ++i ){ field[ i ] = 0; } for ( int i = 1; i < SIZE_Y - 1; ++i ){ cin >> s; for ( unsigned j = 0; j < s.size(); ++j ){ field[ i*7 + (j+1) ] = static_cast< int >( s[ j ] - '0' ); } } s = ""; int pX = 1, pY = 0; eCOURSE course = COURSE_R; tDataSet data; data.pX = &pX; data.pY = &pY; data.course = &course; data.field = field; bool goal = false; while ( !goal ){ int defU = ( 2 * pY ) * SIZE_X + ( pX + 1 ); int defR = ( 2 * pY + 1 ) * SIZE_X + pX; int defD = ( 2 * pY ) * SIZE_X + pX; int defL = ( 2 * ( pY - 1 ) + 1 ) * SIZE_X + pX; switch ( course ){ case COURSE_U : if ( field[ defU ] ){ s += 'U'; goal = checkGoal( data ); if ( field[ defL ] ){ course = COURSE_L; }else{ --pY; } }else{ course = COURSE_R; ++pX; } break; case COURSE_R : if ( field[ defR ] ){ s += 'R'; goal = checkGoal( data ); if ( field[ defU ] ){ course = COURSE_U; }else{ ++pX; } }else{ course = COURSE_D; ++pY; } break; case COURSE_D : if ( field[ defD ] ){ s += 'D'; goal = checkGoal( data ); if ( field[ defR ] ){ course = COURSE_R; }else{ ++pY; } }else{ course = COURSE_L; --pX; } break; case COURSE_L : if ( field[ defL ] ){ s += 'L'; goal = checkGoal( data ); if ( field[ defD ] ){ course = COURSE_D; }else{ --pX; } }else{ course = COURSE_U; --pY; } break; } } cout << s << endl; return 0; }
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> #include<math.h> #include<iomanip> #include<stdio.h> #include <stdlib.h> #include<map> #include<set> using namespace std; int main(){ bool f[13][13]={}; int TT[15][15]={}; string p,q; for(int i=1;i<=4;i++){ cin>>p>>q; for(int j=0;j<p.size();j++) if(p[j]=='1')f[2*i-1][j+1]=1; for(int j=0;j<q.size();j++) if(q[j]=='1')f[i*2][j+1]=1; } cin>>p; for(int j=0;j<p.size();j++) if(p[j]=='1')f[9][j+1]=1; /*for(int i=0;i<=10;i++){ for(int j=0;j<=5;j++) cout<<f[i][j]; cout<<endl; }*/ int x=1,y=1; char z='R';int v=0; while(1){ if(z=='R'){ if(f[y-1][x]){ cout<<"U";z='U';y-=2; } else if(f[y][x]){ cout<<"R";z='R';x++; } else if(f[y+1][x]){ cout<<"D";z='D';y+=2; } else{ cout<<"L";z='L';x--; } } else if(z=='L'){ if(f[y+1][x]){ cout<<"D";z='D';y+=2; } else if(f[y][x-1]){ cout<<"L";z='L';x--; } else if(f[y-1][x]){ cout<<"U";z='U';y-=2; } else{ cout<<"R";z='R';x++; } } else if(z=='U'){ if(f[y][x-1]){ cout<<"L";z='L';x--; } else if(f[y-1][x]){ cout<<"U";z='U';y-=2; } else if(f[y][x]){ cout<<"R";z='R';x++; } else{ cout<<"D";z='D';y+=2; } } else if(z=='D'){ if(f[y][x]){ cout<<"R";z='R';x++; } else if(f[y+1][x]){ cout<<"D";z='D';y+=2; } else if(f[y][x-1]){ cout<<"L";z='L';x--; } else{ cout<<"U";z='U';y-=2; } } if(x==1&&y==1)break; v++; //if(v>30)break; //cout<<x<<" "<<y<<"#"<<v<<endl; TT[y][x]=v; }/* for(int i=0;i<=10;i++){ for(int j=0;j<=5;j++) cout<<TT[i][j]; cout<<endl; } */ cout<<endl; return 0; }
#include <iostream> #define VARIABLE(x) cerr << #x << "=" << x << endl #define rep(i,n) for(int i=0;i<(int)(n);i++) #define REP(i,m,n) for (int i=m;i<(int)(n);i++) const int INF = 10000000; using namespace std; typedef long long ll; /** Problem0037 : Path on a Grid **/ int main() { string s, ds="LRUD"; bool grid[5][5][4]={0}; int y=0; rep(k, 9) { cin>>s; if (k%2==0) { for (int i=0; i<4; i++) { if (s[i] == '1') { grid[i][y][1]=true; grid[i+1][y][0]=true; } } } else { for (int i=0; i<5; i++) { if (s[i] == '1') { grid[i][y][3]=true; grid[i][y+1][2]=true; } } y++; } } int d=1, x=0; y=0; while (1) { switch (d) { case 1: if (grid[x][y][2]) { y--; d=2; cout << "U"; } else if (grid[x][y][1]) { x++; d=1; cout << "R"; } else if (grid[x][y][3]) { y++; d=3; cout << "D"; } else { x--; d=0; cout << "L"; } break; case 3: if (grid[x][y][1]) { x++; d=1; cout << "R"; } else if (grid[x][y][3]) { y++; d=3; cout << "D"; } else if (grid[x][y][0]) { x--; d=0; cout << "L"; } else { y--; d=2; cout << "U"; } break; case 0: if (grid[x][y][3]) { y++; d=3; cout << "D"; } else if (grid[x][y][0]) { x--; d=0; cout << "L"; } else if (grid[x][y][2]) { y--; d=2; cout << "U"; } else { x++; d=1; cout << "R"; } break; case 2: if (grid[x][y][0]) { x--; d=0; cout << "L"; } else if (grid[x][y][2]) { y--; d=2; cout << "U"; } else if (grid[x][y][1]) { x++; d=1; cout << "R"; } else { y++; d=3; cout << "D"; } break; } if (x==0 && y==0) break; } cout << endl; return 0; }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <iostream> #include <cstdio> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P> PP; static const double EPS = 1e-8; int tx[] = {0,1,0,-1}; int ty[] = {-1,0,1,0}; bool is_path[10][10][10][10]; const char ch_dir[] = {'U','R','D','L'}; void dfs(int sx,int sy,int dir){ for(int i=0;i<4;i++){ int next_dir = (dir + i + 3) % 4; int dx = tx[next_dir] + sx; int dy = ty[next_dir] + sy; if(dx < 0 || dx > 4 || dy < 0 || dy > 4) continue; if(!is_path[sx][sy][dx][dy]) continue; printf("%c",ch_dir[next_dir]); if(dx == 0 && dy == 0) return; dfs(dx,dy,next_dir); break; } } int main(){ memset(is_path,false,sizeof(is_path)); for(int i=0;i<9;i++){ char buf[8]; scanf("%s",buf); int y = i / 2; if(i % 2 == 0){ for(int x=0;x<4;x++){ if(buf[x] == '1'){ is_path[x][y][x+1][y] = true; is_path[x+1][y][x][y] = true; } } } else if(i % 2 != 0){ for(int x=0;x<5;x++){ if(buf[x] == '1'){ is_path[x][y][x][y+1] = true; is_path[x][y+1][x][y] = true; } } } } dfs(0,0,1); printf("\n"); }
#include <iostream> #include <string> using namespace std; int a[12][12]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; char dc[4] = {'R','D','L','U'}; //Ž©•ª‚ÌŽüˆÍ‚Sƒ}ƒX‚̕ǂ̏ó‘Ԃ𒲂ׂé void cheakWall(int* wall , const int& px ,const int& py ,const int& d ){ for(int i=0 ; i<4 ; ++i ){ int mx = px + dx[(d+i)%4]; int my = py + dy[(d+i)%4]; wall[i] = a[my][mx]; } } //i‚ñ‚¾Œü‚«‚ð‹L˜^ void record(string& s,int* wall,const int d){ if( wall[1] ){ s.push_back( dc[d] ); } } //i‚Þ void go(int& px, int& py, const int& d,int* wall, string& s){ px += dx[d]*2; py += dy[d]*2; record( s , wall , d ); } int main(){ string s; //”z—ñ‚̏‰Šú‰» for(int y=0 ; y<12 ; ++y ) for(int x=0 ; x<12 ; ++x ) a[y][x] = 0; //“ü—Í for(int i=0 ; i<9 ; ++i ){ cin >> s; int y = i+1; int x = (i+1)%2 + 1; for(int j=0 ; j<(int)s.size() ; ++j , x+=2 ){ a[y][x] = s[j] - '0'; } } int px = 2, py = 0, d = 0; bool flag = false; s.clear(); while( 1 ){ if( px == 0 && py == 0 ) break; int wall[4]; //Ž©•ª‚ÌŽü‚è‚̏ó‘Ô cheakWall( wall , px , py , d ); if( !wall[1] ){//‰E‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç //Œü‚«‚ð‰EŒü‚«‚ɉñ‚é d = (d+1)%4; //i‚Þ go( px , py , d , wall , s ); flag = false; }else{ if( !wall[0] ){//‘O‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç //i‚Þ go( px , py , d , wall , s ); flag = false; }else{ if( !wall[3] ){//¶‚ª•Ç‚¶‚á‚È‚©‚Á‚½‚ç if( flag == false ){ //i‚ñ‚¾Œü‚«‚ð‹L˜^ record( s , wall , d ); } //Œü‚«‚ð¶Œü‚«‚ɉñ‚é d = (d+3)%4; //i‚Þ go( px , py , d , wall , s ); flag = false; }else{//‘OE‰EE¶‚ª•Ç‚¾‚Á‚½‚ç flag = true; //i‚ñ‚¾Œü‚«‚ð‹L˜^ record( s , wall , d ); //Œü‚«‚ð‰EŒü‚«‚ɉñ‚é d = (d+3)%4; //i‚ñ‚¾Œü‚«‚ð‹L˜^ s.push_back( dc[d] ); } } } } cout << s << endl; }
#include <iostream> #include <string> using namespace std; string yoko[5]; string tate[4]; int x = 0; int y = 0; char d; void left() { x--; d = 'L'; cout << 'L'; } void right() { x++; d = 'R'; cout << 'R'; } void up() { y--; d = 'U'; cout << 'U'; } void down() { y++; d = 'D'; cout << 'D'; } int main() { for (int i = 0; i < 9; i++) { if (i % 2 == 0) cin >> yoko[i / 2]; else cin >> tate[i / 2]; } right(); while (true) { if (d == 'R') { if (y != 0 && tate[y - 1][x] == '1') up(); else if (x != 4 && yoko[y][x] == '1') right(); else if (y != 4 && tate[y][x] == '1') down(); else left(); } else if (d == 'L') { if (y != 4 && tate[y][x] == '1') down(); else if (x != 0 && yoko[y][x - 1] == '1') left(); else if (y != 0 && tate[y - 1][x] == '1') up(); else right(); } else if (d == 'U') { if (x != 0 && yoko[y][x - 1] == '1') left(); else if (y != 0 && tate[y - 1][x] == '1') up(); else if (x != 4 && yoko[y][x] == '1') right(); else down(); } else if (d == 'D') { if (x != 4 && yoko[y][x] == '1') right(); else if (y != 4 && tate[y][x] == '1') down(); else if (x != 0 && yoko[y][x - 1] == '1') left(); else up(); } if (x == 0 && y == 0) break; } cout << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { int vwall[5][6] = {0}, hwall[5][6] = {0}; string tmp; for (int i = 0; i < 9; i++) { cin >> tmp; if (i % 2 == 0) for (int j = 0; j < 4; j++) hwall[i/2][j+1] = tmp[j]-'0'; else for (int j = 0; j < 5; j++) vwall[j][i/2+1] = tmp[j]-'0'; } string str = ""; enum Compass {N = 0, E, W, S}; enum Compass compass = E; int x = 0, y = 0; while (true) { if (compass == E) if (vwall[x][y]) { compass = N; str += 'U'; y--; } else if (hwall[y][x+1]) { x++; str += 'R'; } else if (vwall[x][y+1]) { compass = S; str += 'D'; y++; } else { compass = W; } else if (compass == W) if (vwall[x][y+1]) { compass = S; str += 'D'; y++; } else if (hwall[y][x]) { x--; str += 'L'; } else if (vwall[x][y]) { compass = N; str += 'U'; y--; } else { compass = E; } else if (compass == N) if (hwall[y][x]) { compass = W; str += 'L'; x--; } else if (vwall[x][y]) { y--; str += 'U'; } else if (hwall[y][x+1]) { compass = E; str += 'R'; x++; } else { compass = S; } else if (compass == S) if (hwall[y][x+1]) { compass = E; str += 'R'; x++; } else if (vwall[x][y+1]) { y++; str += 'D'; } else if (hwall[y][x]) { compass = W; str += 'L'; x--; } else { compass = N; } if (x == 0 && y == 0) { cout << str << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; int node(int y, int x) { return 5 * y + x; } int to(int dy, int dx, int now) { int y = now / 5 + dy, x = now % 5 + dx; return node(y, x); } bool inside(int now) { int y = now / 5, x = now % 5; return 0 <= node(y, x) && node(y, x) < 25; } bool g[25][25]; int main() { // input for (int i = 0; i < 9; ++i) { if (i & 1) { for (int j = 0; j < 5; ++j) { int f; scanf("%1d", &f); g[node(i / 2, j)][node(i / 2 + 1, j)] = f; g[node(i / 2 + 1, j)][node(i / 2, j)] = f; } } else { for (int j = 0; j < 4; ++j) { int f; scanf("%1d", &f); g[node(i / 2, j)][node(i / 2, j + 1)] = f; g[node(i / 2, j + 1)][node(i / 2, j)] = f; } } } int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0}; char d[] = {'U', 'R', 'D', 'L'}; int now = node(0, 1); int direction = 1; string ans = "R"; while (now) { direction = (direction + 3) % 4; for (int i = 0; i < 4; ++i) { int dir = (direction + i) % 4; int next = to(dy[dir], dx[dir], now); if (!inside(next) || !g[now][next]) continue; now = next; direction = dir; ans += d[dir]; break; } } cout << ans << endl; }
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <complex> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <assert.h> #include <array> #include <cstdio> #include <cstring> #include <random> #include <functional> #include <numeric> #include <bitset> #include <fstream> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) int dx[4] = {-1,0,1,0}; int dy[4] = {0,-1,0,1}; vector<pair<int, int>> G[55]; int main() { rep(i, 5) { char x; rep(j, 4) { cin >> x; if(x-'0') { G[i * 5 + j].emplace_back(i * 5 + j + 1, 2); G[i * 5 + j + 1].emplace_back(i * 5 + j, 0); } } if(i == 4) break; rep(j, 5) { cin >> x; if(x-'0') { G[i * 5 + j].emplace_back((i + 1) * 5 + j, 3); G[(i + 1) * 5 + j].emplace_back(i * 5 + j, 1); } } } string const OP = "LURD"; int y = 0, x = 0, d = 2; while(y != 0 || x != 0 || d != 0) { int le = -1, st = -1, ri = -1, dw = -1; for(auto && e: G[y * 5 + x]) { if(e.second == (d + 3) % 4) le = e.second; if(e.second == (d + 1) % 4) ri = e.second; if(e.second == d) st = e.second; if(e.second == (d + 2) % 4) dw = e.second; } if(le != -1) d = le; else if(st != -1) ; else if(ri != -1) d = ri; else d = dw; y += dy[d], x += dx[d]; cout << OP[d]; } cout << endl; return 0; }
#include<iostream> #include<string> #include<vector> using namespace std; static const int X = 11, Y = 11; static const int DIR = 4; static const string dirchar= "RDLU"; static const int dirx[DIR] = {1,0,-1,0}, diry[DIR] = {0,1,0,-1}; int main(){ int field[X][Y] = {0}; string s; int x,y; y=0; while( getline(cin,s) ){ for(x=0;x<s.size();++x)field[x*2+1][y+1] = s[x]-'0'; y++; } for(y=1;y<Y;y+=2) for(x=1;x<X;x+=2) if(field[x][y])field[x+1][y] = 1; for(y=2;y<Y;y+=2) for(x=1;x<X;x+=2) if(field[x][y]){ field[x][y+1] = 1; field[x][y-1] = 1; } vector<int> ans; x=y=1; int dir=0; while(true){ for(int i=0; i<DIR; ++i){ const int tdir = (dir+DIR-1+i)%DIR; if( field[x + dirx[tdir]][y + diry[tdir]] ){ x += dirx[tdir]*2; y += diry[tdir]*2; ans.push_back(tdir); dir = tdir; break; } } if( x==1 && y==1 )break; } for(int i=0;i<ans.size();++i)cout << dirchar[ans[i]]; cout << endl; return 0; }
#include <iostream> #include <cstdio> #include <string> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <ctime> using namespace std; #define rep(i,a,n) for(int i=a; i<n; i++) #define repr(i,a,n) for(int i=a; i>=n; i--) #define pb(a) push_back(a) #define fr first #define sc second #define INF 999999999 #define X real() #define Y imag() #define EPS (1e-10) #define EQ(a,b) (abs((a) - (b)) < EPS) #define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) ) #define LE(n, m) ((n) < (m) + EPS) #define GE(n, m) ((n) + EPS > (m)) typedef vector<int> VI; typedef vector<VI> MAT; typedef pair<int, int> pii; typedef long long int ll; typedef complex<double> P; typedef pair<P, P> L; typedef pair<P, double> C; namespace std { bool operator<(const P a, const P b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } #define S 6 // right, down, left, up int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int const MOD = 1000000007; int main() { bool board[S][S][4] = {}; rep(i,0,9) { int l, n; if(i % 2 == 0) l = 4; else l = 5; rep(j,0,l) { scanf("%1d", &n); if(n == 1) { if(i % 2 == 0) { board[i/2][j+1][1] = true; board[i/2+1][j+1][3] = true; } else { board[i/2+1][j][0] = true; board[i/2+1][j+1][2] = true; } } } } string d = "RDLU"; string ret; int dir = 0; int nx = 0, ny = 1; for(int i=0; i<150; i++) { if(nx == 1 && ny == 0 && dir == 2) break; if(nx == 0 && ny == 0 && dir == 3) break; int x, y; x = nx + dx[dir], y = ny + dy[dir]; if(board[nx][ny][(dir + 1) % 4] == 0) { dir = (dir + 1) % 4; nx = nx + dx[dir], ny = ny + dy[dir]; } else if(board[nx][ny][dir] == 1) { ret += d[dir]; dir = (dir - 1 + 4) % 4; } else { ret += d[dir]; nx = x, ny = y; } } cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} template<class T> inline T sqr(T x) {return x*x;} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define mp make_pair #define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define exist(s,e) ((s).find(e)!=(s).end()) #define range(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) range(i,0,n) #define clr(a,b) memset((a), (b) ,sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF =1LL << 62; const int inf =1 << 29; string wall[9]; int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; inline int iswall(int cx,int cy,int d){ switch(d){ case 0: cy=2*cy; cx=cx; break; case 1: cy=2*cy+1; cx=cx; break; case 2: cy=2*cy; cx=cx-1; break; case 3: cy=2*cy-1; cx=cx; break; } if(cy<0||9<=cy||cx<0||5<=cx) return 0; return wall[cy][cx]=='1'; } int main(void){ rep(i,9){ cin >> wall[i]; if(wall[i].size()==4) wall[i]+="0"; } cout << 'R'; int cx=1,cy=0,cd=0; map<int,char> dir; dir[0]='R',dir[1]='D',dir[2]='L',dir[3]='U'; while(1){ rep(i,4){ int nd=(cd+i+3)%4; if(iswall(cx,cy,nd)){ cout << dir[nd]; cy+=dy[nd],cx+=dx[nd],cd=nd; break; } } if(cx==0&&cy==0) break; } cout << endl; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; #define REP(i,n) for(int i = 0; i < (int)n; ++i) #define DEBUG(x) cerr << #x << " = " << x << endl int main() { static bool wall[6][6][4]; const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; REP(i,6) REP(j,6) REP(k,4) { int ni = i + dy[k]; int nj = j + dx[k]; if(0 <= ni && ni < 6 && 0 <= nj && nj < 6) { wall[i][j][k] = 0; } else { wall[i][j][k] = 1; } } string s; for(int i = 0; i < 4; i++) { getline(cin, s); for(int j = 0; j < 4; j++) { wall[i][j + 1][1] = (s[j] == '1'); wall[i + 1][j + 1][3] = (s[j] == '1'); } getline(cin, s); for(int j = 0; j < 5; j++) { wall[i + 1][j][0] = (s[j] == '1'); wall[i + 1][j + 1][2] = (s[j] == '1'); } } getline(cin, s); for(int j = 0; j < 4; j++) { wall[4][j + 1][1] = (s[j] == '1'); wall[5][j + 1][3] = (s[j] == '1'); } string icon = "RDLU"; #if 0 REP(i,6) REP(j,6) { cerr << "(" << i << "," << j << ")"; REP(k,4) { if(wall[i][j][k]) cerr << icon[k]; } cerr << endl; } #endif int X = 1, Y = 0, dir = 0; string ans = "R"; do { #if 0 cerr << "X = " << X << ", Y = " << Y << ", dir = " << dir << endl; #endif if(wall[Y][X][dir] == 0) { Y += dy[dir]; X += dx[dir]; if(wall[Y][X][(dir + 1) % 4] == 0) { dir = (dir + 1) % 4; X += dx[dir]; Y += dy[dir]; if(wall[Y][X][(dir + 1) % 4] == 0) { dir = (dir + 1) % 4; X += dx[dir]; Y += dy[dir]; ans.push_back(icon[dir]); } else { ans.push_back(icon[dir]); } } else { ans.push_back(icon[dir]); } } else { dir = (dir - 1 + 4) % 4; ans.push_back(icon[dir]); } } while(!((X == 1 && Y == 0))); ans = ans.substr(0, (int)ans.size() - 1); cout << ans << endl; }
#include<bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; int a[9][9][4],x,y,dir=1; int dx[]={0,1,0,-1}; int dy[]={-1,0,1,0}; string s="URDL"; int main(){ r(i,9){ if(i%2){ r(j,5){ scanf("%1d",&x); if(x)a[i/2][j][2]=a[i/2+1][j][0]=1; } }else{ r(j,4){ scanf("%1d",&x); if(x)a[i/2][j][1]=a[i/2][j+1][3]=1; } } }x=1; cout<<s[dir]; while(!(!x&&!y)){ if(a[y][x][(dir+3)%4])dir=(dir+3)%4; else if(a[y][x][dir]); else if(a[y][x][(dir+1)%4])dir=(dir+1)%4; else dir=(dir+2)%4; x+=dx[dir]; y+=dy[dir]; cout<<s[dir]; } cout<<endl; }
#include<iostream> #include<bitset> #include<string> #include<algorithm> #define UP_DIRECTION 3 #define RIGHT_DIRECTION 2 #define DOWN_DIRECTION 1 #define LEFT_DIRECTION 0 using namespace std; int dy[]={0,1,0,-1},dx[]={-1,0,1,0}; int main(){ bitset<4>Square[5][5]; string Wall[9]; char For_Output[]="LDRU"; for(int i=0;i<9;i++)cin>>Wall[i]; for(int i=0;i<9;i+=2){ Wall[i]="0"+Wall[i]+"0"; for(int j=0;j<5;j++){ Square[i/2][j][LEFT_DIRECTION]=(Wall[i][j]-'0'); Square[i/2][j][RIGHT_DIRECTION]=(Wall[i][j+1]-'0'); } } for(int i=1;i<9;i+=2){ for(int j=0;j<5;j++){ Square[i/2][j][DOWN_DIRECTION]=(Wall[i][j]-'0'); Square[i/2+1][j][UP_DIRECTION]=(Wall[i][j]-'0'); } } int Direction=RIGHT_DIRECTION; int y=0,x=0; do{ int R=Direction-1; int L=Direction+1; int B=Direction-2; if(R<0)R=3; if(B<0)B+=4; L%=4; if(Square[y][x][L]) Direction=L; else if(Square[y][x][Direction]); else if(Square[y][x][R]) Direction=R; else Direction=B; y+=dy[Direction]; x+=dx[Direction]; cout<<For_Output[Direction]; }while(y!=0||x!=0); cout<<endl; }
#include <iostream> #include <string> using namespace std; bool wall[5][5][4]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main(){ string s; for(int i = 0; i < 9; ++i){ cin >> s; if(i % 2 == 0){ for(int j = 0; j < 4; ++j){ if(s[j] == '1'){ wall[j][i / 2][0] = wall[j + 1][i / 2][2] = true; } } } else{ for(int j = 0; j < 5; ++j){ if(s[j] == '1'){ wall[j][i / 2][1] = wall[j][i / 2 + 1][3] = true; } } } } s.clear(); int x = 0, y = 0, dir = 0; do{ s += "RDLU"[dir]; x += dx[dir]; y += dy[dir]; if(wall[x][y][(dir + 3) % 4]){ dir = (dir + 3) % 4; } else if(!wall[x][y][dir]){ if(wall[x][y][(dir + 1) % 4]){ dir = (dir + 1) % 4; } else{ dir = (dir + 2) % 4; } } } while(x != 0 || y != 0 || dir != 0); cout << s << endl; }
#include<iostream> #include<algorithm> using namespace std; const int N = 5; const int di[] = {0,1,0,-1}; const int dj[] = {1,0,-1,0}; const char C[] = {'R','D','L','U'}; int g[N][N]; int main(void){ fill(g[0],g[N],0); for(int s = 0 ; s < 2 * N - 1 ; s ++){ for(int j = 0 ; j < N - 1 + s % 2 ; j ++){ int i = s / 2; char a; cin>> a; if(a=='1'){ if(s%2==0){ g[i][j] |= (1<<0); g[i][j+1] |= (1<<2); } else{ g[i][j] |= (1<<1); g[i+1][j] |= (1<<3); } } } } int i,j,d; i = j = d = 0; do{ for(int k = 0 ; k < 4 ; k ++){ int nd = (d+3+k) % 4; if(g[i][j] & (1<<nd)){ d = nd; i += di[d]; j += dj[d]; cout << C[d]; break; } } }while(!(i==0 && j==0)); cout<<endl; }
#include <iostream> #include <vector> #include <stack> #include <queue> #include <map> #include <bitset> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cmath> #define X first #define Y second #define WIDTH 8 #define HEIGHT 8 using namespace std; struct point{ point(int x = 0, int y = 0, char dir = 'R'):x(x), y(y), dir(dir){} int x; int y; char dir; }; char moveMap(vector<string> &map, point &player){ switch(player.dir){ case 'R': if(player.y != 0 && map[player.y - 1][player.x + 1] == '1'){ player.y -= 1; player.x += 1; player.dir = 'U'; } else if(player.x != map[player.y].size() - 1 && map[player.y][player.x + 1] == '1'){ player.x += 1; } else if(player.y != 8 && map[player.y + 1][player.x + 1] == '1'){ player.y += 1; player.x += 1; player.dir = 'D'; } else player.dir = 'L'; break; case 'L': if(player.y != 8 && map[player.y + 1][player.x] == '1'){ player.y += 1; player.dir = 'D'; } else if(player.x != 0 && map[player.y][player.x - 1] == '1'){ player.x -= 1; } else if(player.y != 0 && map[player.y - 1][player.x] == '1'){ player.y -= 1; player.dir = 'U'; } else player.dir = 'R'; break; case 'U': if(player.x != 0 && map[player.y - 1][player.x - 1] == '1'){ player.y -= 1; player.x -= 1; player.dir = 'L'; } else if(player.y != 1 && map[player.y - 2][player.x] == '1'){ player.y -= 2; } else if(player.x != map[player.y].size() - 1 && map[player.y - 1][player.x] == '1'){ player.y -= 1; player.dir = 'R'; } else player.dir = 'D'; break; case 'D': if(player.x != map[player.y].size() - 1 && map[player.y + 1][player.x] == '1'){ player.y += 1; player.dir = 'R'; } else if(player.y != 7 && map[player.y + 2][player.x] == '1'){ player.y += 2; } else if(player.x != 0 && map[player.y + 1][player.x - 1] == '1'){ player.y += 1; player.x -= 1; player.dir = 'L'; } else player.dir = 'U'; break; default: break; } return player.dir; } int main(){ string tmp; while(cin >> tmp){ if(cin.eof()) break; vector<string> map; map.push_back(tmp); for(int i = 1; i < 9; i++){ cin >> tmp; map.push_back(tmp); } point player(0, 0, 'R'); string root; root.push_back('R'); while(1){ root.push_back(moveMap(map, player)); if(player.x == 0 && player.y == 0) break; } for(int i = 0; i < root.size(); i++){ cout << root[i]; } cout << endl; } return 0; }
#include <stdio.h> char RideBack[9][9],T[6],ans[4]={'L','U','R','D'};int a[4][2]={-1,0,0,-1,1,0,0,1};bool OK=true; void dfs(int x, int y, int w, int deep) {if (deep) printf("%c",ans[w]);if (!x && !y && deep) OK=false;for (int j=(w+3)&3;OK;j=(j+1)&3) {int n_x=x+a[j][0],n_y=y+a[j][1];if (n_x>=0 && n_x<9 && n_y>=0 && n_y<9 && RideBack[n_y][n_x]=='1') dfs(x+a[j][0]*2,y+a[j][1]*2,j,deep+1);}} int main() {int i,j,k;for (i=0;i<9;i++) {scanf("%s",&T);if (i&1) for (k=0,j=0;j<5;RideBack[i][k]=T[j++],k+=2); else for (k=1,j=0;j<4;RideBack[i][k]=T[j++],k+=2);}dfs(0,0,1,0);puts("");return 0;}
#include<iostream> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) const int N=6; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; const string op="RDLU"; enum{RIGHT=0,DOWN,LEFT,UP}; int m[N][N]; bool e[N][N][N]; string solve(){ int y=0,x=1,r=RIGHT; string ret=""; do{ bool a=true; if (!e[y][x][(r+1)%4])r=(r+1)%4; while(e[y][x][r])ret+=op[r],r=(r+3)%4; if (e[y][x][(r+1)%4])ret+=op[r]; y+=dy[r]; x+=dx[r]; }while(y != 0 || x != 1); return ret; } int main(){ rep(i,N)rep(j,N)rep(k,4)e[i][j][k]=false; rep(i,5){ string tmp; cin>>tmp; rep(j,4)if (tmp[j] == '1')e[i][j+1][DOWN]=e[i+1][j+1][UP]=true; if (i == 4)break; cin>>tmp; rep(j,5)if (tmp[j] == '1')e[i+1][j][RIGHT]=e[i+1][j+1][LEFT]=true; } cout << solve() << endl; }
#include <iostream> #include <vector> using namespace std; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; char dc[5] = "RDLU"; bool cangocw(int y, int x, int dir, vector<string> &s){ if(dir%2==0){ return s[2*y +1][x +1-(dir/2)] == '0'; }else{ return s[2*y +(1-dir/2)*2][x+1] == '0'; } } int main(){ vector<string> s(11); s[0] = s[10] = "0000000"; for(int i=1; i<=9; i++){ cin >> s[i]; s[i] = "0" + s[i] + "0"; } int dir=0; int x=0, y=0; string ans = ""; do{ if(cangocw(y, x, dir, s)){ dir = (dir+1)%4; continue; } x += dx[dir]; y += dy[dir]; ans += dc[dir]; dir = (dir+3)%4; }while(!(x==0 && y==0 && dir==0)); cout << ans << endl; return 0; }
#include<iostream> #include<cstdio> using namespace std; int main(){ int field[6][6]={0}; for(int i=0; i<9; i++){ if(i%2 == 0){ for(int j=0,a; j<4; j++){ scanf("%1d",&a); field[i/2][j+1]+=4*a; field[i/2+1][j+1]+=a; } } else{ for(int j=0,a; j<5; j++){ scanf("%1d",&a); field[i/2+1][j]+=2*a; field[i/2+1][j+1]+=8*a; } } } int x=0,y=1,d=2; for(;;){ if(d == 1){ if(!(field[x][y]&2)){d = 2;y++;} else if(field[x][y]&1){cout <<"U";d = 8;} else{cout <<"U";x--;} } else if(d == 2){ if(!(field[x][y]&4)){d = 4;x++;} else if(field[x][y]&2){cout <<"R";d = 1;} else{cout <<"R";y++;} } else if(d == 4){ if(!(field[x][y]&8)){d = 8;y--;} else if(field[x][y]&4){cout <<"D";d = 2;} else{cout <<"D";x++;} } else if(d == 8){ if(!(field[x][y]&1)){d = 1;x--;} else if(field[x][y]&8){cout <<"L";d = 4;} else{cout <<"L";y--;} } if(x == 0 && y == 1 && d == 2) break; } cout <<endl; return 0; }
#include<bits/stdc++.h> using namespace std; string side[4],Longitudinal[5]; int mox[2][4]={{0,1,0,-1},{0,0,0,-1}},moy[2][4]={{-1,0,1,0},{-1,0,0,0}}; char str[5]="URDL"; void serch(int x,int y,int then) { if(then==-1)then=3; if(x==0&&y==0){ cout<<endl; exit(0); } for(int i=0;i<4;i++){ int now=(then+i)%4; if(now%2==0){ if(y+moy[0][now]<5&&y+moy[0][now]>-1&&side[y+moy[1][now]][x]=='1'){ cout<<str[now]; serch(x,y+moy[0][now],now-1); } } else{ if(x+mox[0][now]<5&&x+mox[0][now]>-1&&Longitudinal[y][x+mox[1][now]]=='1'){ cout<<str[now]; serch(x+mox[0][now],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); }
#include <iostream> using namespace std; //R:0 L:1 U:2 D:3 int map[11][7], type, x=2, y=1; char a[4], b[5]; void init(){ for(int i=1; i<=9; i++){ if(i%2){ cin>>a; for(int n=0; n<4; n++)map[i][n+1] = char(a[n])-'0'; } else { cin>>b; for(int m=0; m<5; m++)map[i][m+1] = char(b[m])-'0'; } } } void Right(){ if(map[y-1][x])type=2, y-=2, cout<<'U'; else if(map[y][x])type=0, x+=1, cout<<'R'; else if(map[y+1][x])type=3, y+=2, cout<<'D'; else type=1, x-=1, cout<<'L'; } void Left(){ if(map[y+1][x])type=3, y+=2, cout<<'D'; else if(map[y][x-1])type=1, x-=1, cout<<'L'; else if(map[y-1][x])type=2, y-=2, cout<<'U'; else type=0, x+=1, cout<<'R'; } void Up(){ if(map[y][x-1])type=1, x-=1, cout<<'L'; else if(map[y-1][x])type=2, y-=2, cout<<'U'; else if(map[y][x])type=0, x+=1, cout<<'R'; else type=3, y+=2, cout<<'D'; } void Down(){ if(map[y][x])type=0, x+=1, cout<<'R'; else if(map[y+1][x])type=3, y+=2, cout<<'D'; else if(map[y][x-1])type=1, x-=1, cout<<'L'; else type=2, y-=2, cout<<'U'; } void solve(){ while(!(x==1&&y==1)){ switch(type){ case 0: Right(); break; case 1: Left(); break; case 2: Up(); break; case 3: Down(); break; default: return; } } } int main(){ init(); cout<<'R'; solve(); cout<<"\n"; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; string dc = "RDLU"; int main() { int g[11][11] = {}; char c; for (int i=1; i<10; ++i) { if (i % 2 == 1) { for (int j=2; j<=8; j+=2) { cin >> c; g[i][j] = c - '0'; g[i][j-1] |= g[i][j]; g[i][j+1] |= g[i][j]; } } else { for (int j=1; j<=9; j+=2) { cin >> c; g[i][j] = c - '0'; g[i-1][j] |= g[i][j]; g[i+1][j] |= g[i][j]; } } } int x = 1, y = 1, d = 0; do { for (int t=3; t<=6; ++t) { if (g[y + dy[(d + t) % 4]][x + dx[(d + t) % 4]]) { d = (d + t) % 4; break; } } x += dx[d] * 2; y += dy[d] * 2; cout << dc[d]; } while ( ! (x == 1 && y == 1)); cout << endl; return 0; }
#include<iostream> #include<algorithm> #include<string> using namespace std; bool mas[11][11][4]; string s = "LURD"; const int dy[] = { 0, -1, 0, 1}, dx[] = { -1, 0, 1, 0}; void dfs( int y, int x, int pos){ cout << s[pos]; if(!y && !x){ cout << endl; return; } for(int i = 0 ; i < 4 ; i++ ){ int dd = ( pos + 3 + i ) % 4; int ny = y + dy[ dd], nx = x + dx[ dd]; if(mas[y][x][dd]){ dfs( y + dy[ dd], x + dx[ dd], dd); return; } } } int main(){ char c; for(int i = 0 ; i < 9 ; i++ ){ if(i & 1) for(int j = 0 ; j < 5 ; j++ ){ cin >> c; if(c == '1') mas[i / 2][j][3] = mas[i / 2 + 1][j][1] = true; } else for(int j = 0 ; j < 4 ; j++ ){ cin >> c; if(c == '1') mas[i / 2][j][2] = mas[i / 2][j + 1][0] = true; } } dfs( 0, 1, 2); }
#include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <string> #define rep(x,to) for(int x=0;x<to;x++) #define rep2(x,from,to) for(int x=from;x<to;x++) using namespace std; int main(void){ int hk=5; //0:上 1:右 2:下 3:左 int cnt=0; string w[12],ans=""; string shk[4]={"U","R","D","L"}; rep(i,9) { cin >> w[i+1] ; w[i+1] = " " + w[i+1]; } int x=1, y=1; if(w[1][1]=='1') { hk = 1; cout <<"R";} else {hk = 2; cout << "U";} while(1){ // printf("\n%d h=%d (%d,%d) %c:",cnt,hk,y,x,w[y][x]); switch( hk){ case 0: if(w[y-1][x-1] == '1' ) { hk = 3; y--; x--;} else if(w[y-2][x] == '1') { hk = 0; y -=2;} else if(w[y-1][x] == '1'){ hk = 1; y--;} else { hk = 2; } break; case 1: if(w[y-1][x+1] == '1' ){ hk = 0; y--; x++;} else if(w[y][x+1] == '1') { hk = 1; x++;} else if(w[y+1][x+1] == '1'){ hk = 2; y++; x++;} else { hk = 3; } break; case 2: if(w[y+1][x] == '1' ) { hk = 1; y++; } else if(w[y+2][x] == '1' ) { hk = 2; y +=2;} else if( w[y+1][x-1] == '1'){ hk = 3; y++; x--;} else { hk = 0;} break; case 3: if(w[y+1][x] == '1' ){ hk = 2; y++; } else if(w[y][x-1] == '1') { hk = 3; x--;} else if( w[y-1][x] == '1'){ hk = 0; y--; } else { hk = 1; } break; } cout << shk[hk]; // ans = ans + shk[hk]; cnt++; if(cnt>200) break; if(x==1 && y ==1) break; } cout << endl; // cout << ans << endl; return 0; }
#include <cstdio> using namespace std; #define rep(i,n) for(int i=0; i<int(n); ++i) int main() { int field[5][5] = {{0}}; rep(i, 9){ char str[6]; scanf("%s", str); if(i%2){ rep(j, 5){ int a = str[j]-'0'; field[i/2][j] |= a<<2; field[i/2+1][j] |= a; } }else{ rep(j, 4){ int a = str[j]-'0'; field[i/2][j] |= a<<1; field[i/2][j+1] |= a<<3; } } } putchar('R'); int y=0, x=1; int pre = 1; do{ // printf("(%d, %d)\n", x, y); pre = (pre+2)%4; int dire = pre; do{ dire = (dire + 1)%4; }while(!((field[y][x]>>dire)&1)); pre = dire; putchar("URDL"[dire]); int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; x += dx[dire]; y += dy[dire]; }while(y!=0 || x!=0 || (field[0][0]==6 && pre==3)); puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) int dx[4] = {-1,0,1,0}, dy[4] = {0,-1,0,1}; vector<pair<int, int>> G[55]; int main() { rep(i, 5) { char x; rep(j, 4) { cin >> x; if(x-'0') { G[i * 5 + j].emplace_back(i * 5 + j + 1, 2); G[i * 5 + j + 1].emplace_back(i * 5 + j, 0); } } if(i == 4) break; rep(j, 5) { cin >> x; if(x-'0') { G[i * 5 + j].emplace_back((i + 1) * 5 + j, 3); G[(i + 1) * 5 + j].emplace_back(i * 5 + j, 1); } } } string const OP = "LURD"; int y = 0, x = 0, d = 2; while(y != 0 || x != 0 || d != 0) { int le = -1, st = -1, ri = -1, dw = -1; for(auto && e: G[y * 5 + x]) { if(e.second == (d + 3) % 4) le = e.second; if(e.second == (d + 1) % 4) ri = e.second; if(e.second == d) st = e.second; if(e.second == (d + 2) % 4) dw = e.second; } if(le != -1) d = le; else if(st != -1) ; else if(ri != -1) d = ri; else d = dw; y += dy[d], x += dx[d]; cout << OP[d]; } cout << endl; return 0; }
#include<cstdio> int main(){ char a[4][5]; char b[5][4]; int d = 2; int x = 0, y = 0; for(int i = 0; i < 4; i++){ scanf("%c%c%c%c\n", &a[0][i], &a[1][i], &a[2][i], &a[3][i]); scanf("%c%c%c%c%c\n", &b[0][i], &b[1][i], &b[2][i], &b[3][i], &b[4][i]); } scanf("%c%c%c%c", &a[0][4], &a[1][4], &a[2][4], &a[3][4]); while(d){ if(d == 2){ if(y && b[x][y-1] - '0'){ d = 1; y--; printf("U"); }else if(x == 4){ d = 3; }else if(a[x][y] - '0'){ x++; printf("R"); }else{ d = 3; } } if(d == 3){ if(x - 4 && a[x][y] - '0'){ d = 2; x++; printf("R"); }else if(y == 4){ d = 4; }else if(b[x][y] - '0'){ y++; printf("D"); }else{ d = 4; } } if(d == 4){ if(y - 4 && b[x][y] - '0'){ d = 3; y++; printf("D"); }else if(x == 0){ if(y == 0){ printf("\n"); return 0; } d = 1; }else if(a[x-1][y] - '0'){ x--; printf("L"); }else{ d = 1; } } if(d == 1){ if(x && a[x-1][y] - '0'){ d = 4; x--; printf("L"); }else if(y == 0){ if(x == 0){ printf("\n"); return 0; } d = 2; }else if(b[x][y-1] - '0'){ y--; printf("U"); }else{ d = 2; } } } }
//35 #include<iostream> using namespace std; int main(){ bool w[11][11]={{}}; for(int i=1;i<=9;i++){ for(int j=1+i%2;j<10;j+=2){ char c; cin>>c; w[i][j]=c-'0'; } } int y=0,x=2; int d=1; int dy[]={-1,0,1,0}; int dx[]={0,1,0,-1}; do{ const char *s="URDL"; d=(d+1)%4; while(w[y+dy[d]][x+dx[d]]){ cout<<s[(d+3)%4]; d=(d+3)%4; } y+=dy[d]*2; x+=dx[d]*2; }while(x||y); cout<<endl; return 0; }
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; const int H = 5; const int W = 5; const int D = 4; const string DIR = "RULD"; int v[H][W][D], my[4] = {0, -1, 0, 1}, mx[4] = {1, 0, -1, 0}, d = 0, y = 0, x = 1; int main() { REP(i, 9){ string s; cin >>s; if(i % 2) REP(j, s.length()) if(s[j] == '1') v[(i + 1) / 2 - 1][j][3] = v[(i + 1) / 2][j][1] = true; if(!(i % 2)) REP(j, s.length()) if(s[j] == '1') v[i / 2][j][0] = v[i / 2][j + 1][2] = true; } stringstream ss; ss << 'R'; while(y != 0 || x != 0){ if(v[y][x][(d + 1) % 4]){ d = (d + 1) % 4; y += my[d]; x += mx[d]; ss << DIR[d]; } else if (v[y][x][d]){ y += my[d]; x += mx[d]; ss << DIR[d]; } else{ d = (d + 3) % 4; } } cout <<ss.str() <<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,a) FOR(i,0,a) int maze[20][20]; char s[6]; char ans[100]; int sz=0; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; char sym[]="RDLU"; int main(){ REP(i,9){ scanf("%s",s); REP(j,strlen(s)){ if (i%2==0){ maze[i+1][2*j+2]=s[j]-'0'; }else{ maze[i+1][2*j+1]=s[j]-'0'; } } } ans[0]='R'; sz++; int x=3,y=1; int dir=0; while (x!=1 || y!=1){ REP(i,4){ int d=(dir+3+i)%4; if (maze[y+dy[d]][x+dx[d]]){ dir=d; break; } } x+=dx[dir]*2; y+=dy[dir]*2; ans[sz++]=sym[dir]; } printf("%s\n",ans); return 0; }
#include<iostream> #include<string> #include<sstream> #include<algorithm> #include<cmath> #include<map> #include<set> #include<vector> #include<stack> #include<queue> #include<deque> using namespace std; int main() { vector<vector<char> > root (9, vector<char>(5)); char c; for(int i = 0; i < 9; i++) { for(int j = 0; j < 4 + (i % 2); j++) { cin >> c; root[i][j] = c; } } cout << "R"; int x = 1; int y = 0; int d = 2; char dirs [] = { 'L', 'U', 'R', 'D', 'L', 'U', 'R', 'D' }; while ( x || y ) { bool gone = false; for (int i = -1; i < 3; i++) { switch(dirs[d + i]) { case 'L': if (x > 0 && root[2 * y][x - 1] == '1') { x -= 1; d = 4; cout << 'L'; gone = true; } break; case 'U': if (y > 0 && root[2 * y - 1][x] == '1') { y -= 1; d = 1; cout << 'U'; gone = true; } break; case 'R': if (x < 4 && root[2 * y][x] == '1') { x += 1; d = 2; cout << 'R'; gone = true; } break; case 'D': if (y < 4 && root[2 * y + 1][x] == '1') { y += 1; d = 3; cout << 'D'; gone = true; } break; } if (gone) break; } } cout << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <string> using namespace std; int main(){ int yoko[10][10]; int tate[10][10]; for(int i = 0; i < 5; i++){ for(int j = 0; j < 4; j++){ char c; cin>>c; yoko[i][j]=c-'0'; } if(i!=4){ for(int j = 0; j < 5; j++){ char c; cin>>c; tate[i][j]=c-'0'; } } } cout<<'R'; int cx=1; int cy=0; // 0:‰E,1:‰º,2:¶,3:ã int angle=0; while(1){ int nx=cx; int ny=cy; if(angle==0){ nx=cx+1; ny=cy; if(cy!=0&&tate[cy-1][cx]){ angle=3; cy--; cout<<"U"; } // •Ç‚ª‚ ‚é‚È‚ç else if(nx>=0&&ny>=0&&ny<5&&nx<5&&yoko[cy][cx]){ cy=ny; cx=nx; cout<<'R'; } // ‚È‚¢‚È‚çAŒü‚«‚ð•ύX else if(tate[cy][cx]){ angle=1; cout<<"D"; cy++; } else{ angle=2; cout<<"L"; cx--; } } else if(angle==1){ nx=cx; ny=cy+1; // ‰E‚̕ǂð—Dæ if(cx!=4&&yoko[cy][cx]){ cx++; cout<<"R"; angle=0; } // •Ç‚ª‚ ‚é‚È‚ç else if(nx>=0&&ny>=0&&ny<5&&nx<5&&tate[cy][cx]){ cy=ny; cx=nx; cout<<'D'; } // ‚È‚¢‚È‚çAŒü‚«‚ð•ύX // ¶ else if(cx!=0&&yoko[cy][cx-1]){ cx--; cout<<"L"; angle=2; } // ‚Í‚ñ‚Ä‚ñ else{ cy--; cout<<'U'; angle=3; } } // ¶ else if(angle==2){ nx=cx-1; ny=cy; // ‰º‚̕ǂª—Dæ if(cy!=4&&tate[cy][cx]){ cout<<'D'; cy++; angle=1; } // •Ç‚ª‚ ‚é‚È‚ç else if(nx>=0&&ny>=0&&ny<5&&nx<5&&yoko[cy][cx-1]){ cy=ny; cx=nx; cout<<'L'; } // ‚È‚¢‚È‚çAŒü‚«‚ð•ύX // ã else if(cy!=0&&tate[cy-1][cx]){ cout<<'U'; angle=3; cy--; } // ‚Í‚ñ‚Ä‚ñ else{ cout<<'R'; angle=0; cx++; } } // ã else if(angle==3){ nx=cx; ny=cy-1; // •Ç‚ª‚ ‚é‚È‚ç // ¶‚̕ǂð—Dæ if(cx!=0&&yoko[cy][cx-1]){ cout<<'L'; angle=2; cx--; } else if(nx>=0&&ny>=0&&ny<5&&nx<5&&tate[cy-1][cx]){ cy=ny; cx=nx; cout<<'U'; } // ‚È‚¢‚È‚çAŒü‚«‚ð•ύX // ‰E else if(cx!=4&&yoko[cy][cx]){ angle=0; cout<<'R'; cx++; } // ‚Í‚ñ‚Ä‚ñ else{ cout<<'D'; cy++; angle=1; } } if(cx==0&&cy==0) break; } cout<<endl; return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; typedef enum _DIRECTION { dU, dR, dD, dL, DIR_MAX } DIR; const int MAX_N = 5; const int MAX_D = 4; bool mat[MAX_N][MAX_N][MAX_D]; int main ( void ) { for (int i = 0; i < MAX_N; ++i) { for (int j = 0; j < MAX_N; ++j) { fill(mat[i][j], mat[i][j]+MAX_D, false); } } for (int i = 0; i < 9; ++i) { string str; cin >> str; if (i & 1) { for (int j = 0, J = str.size(); j < J; ++j) { mat[i/2][j][dD] = (str[j] == '1'); if (i/2+1 < MAX_N) { mat[i/2+1][j][dU] = (str[j] == '1'); } } } else { for (int j = 0, J = str.size(); j < J; ++j) { mat[i/2][j][dR] = mat[i/2][j+1][dL] = (str[j] == '1'); } } } char c_dir[] = "URDL"; int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 }; DIR dir = dU; int x = 0, y = 0; bool hit = true; while (hit) { hit = false; for (int i = 0; i < DIR_MAX; ++i) { DIR next = (DIR)((dir + DIR_MAX - 1 + i) % DIR_MAX); if (mat[y][x][next]) { dir = next; mat[y][x][dir] = false; cout << c_dir[dir]; x += dx[dir]; y += dy[dir]; hit = true; break; } } } cout << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; const int N = 5; const int di[] = {0,1,0,-1}; const int dj[] = {1,0,-1,0}; const char C[] = {'R','D','L','U'}; int g[N][N]; int main() { fill(g[0], g[N], 0); for(int s = 0; s < 2*N-1; ++s) { for(int j = 0; j < N-1+s%2; ++j) { int i = s/2; char a; cin >> a; if(a == '1') { if(s%2 == 0) { g[i][j] |= (1<<0); g[i][j+1] |= (1<<2); } else { g[i][j] |= (1<<1); g[i+1][j] |= (1<<3); } } } } int i, j, d; i = j = 0; d = 0; do { for(int k = 0; k < 4; ++k) { int nd = (d+3+k)%4; if(g[i][j] & (1<<nd)) { d = nd; i += di[d]; j += dj[d]; cout << C[d]; break; } } } while(!(i == 0 && j == 0)); cout << endl; return 0; }
#include<iostream> #include<string> bool field[10][6]; int dir = 1; int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 }; int x = 1, y = 0; char s[5] = "URDL"; bool safe( int x, int y ) { return x >= 0 && x < 9 && y >= 0 && y < 9; } char g[9][9]; int main() { for( int i = 0; i != 9; ++i ) { std::string line; std::cin >> line; bool odd = i & 1; for( int j = !odd; j < 9; j += 2 ) { g[i][j] = line[j/2] - '0' ? odd ? '|' : '_' : ' '; g[i][j+1] = '.'; } if( !odd ) g[i][0] = '.'; } do { int fdir = ( 4 + dir - 1 ) % 4; int fx = x + dx[dir] + dx[fdir], fy = y + dy[dir] + dy[fdir]; if( !safe( x, y ) || g[y][x] == ' ' ) { x -= dx[dir], y -= dy[dir], dir = ( dir + 1 ) % 4; x += dx[dir], y += dy[dir]; } else if( safe( fx, fy ) && ( g[fy][fx] == '_' || g[fy][fx] == '|' ) ) { std::cout << s[dir]; x += dx[dir], y += dy[dir], dir = ( 4 + dir - 1 ) % 4; x += dx[dir], y += dy[dir]; } else { x += dx[dir] * 2, y += dy[dir] * 2; std::cout << s[dir]; } if( x == 1 && y == 0 ) { std::cout << s[dir]; break; } }while( true ); std::cout << std::endl; return 0; }
#include <cstdio> #include <complex> using namespace std; int dir = 0; int cur_x; int cur_y; int cur_side; int refd[] = {0, 1, 0, 1}; int refx[] = {0, 0, -1, 0}; int refy[] = {0, 0, 0, -1}; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; char ans[] = "RDLU"; bool WithinRange(int d, int y, int x) { if (x < 0) return false; if (y < 0) return false; if (d == 0) { if (x >= 4) return false; if (y >= 5) return false; } else { if (x >= 5) return false; if (y >= 4) return false; } return true; } char walls[2][5][5]; int main() { for (int i=0; i<9; i++) { int n = i%2 ? 5: 4; for (int j=0; j<n; j++) { scanf(" %c", &walls[i%2][i/2][j]); } } cur_side = -1; while (1) { int a, b, c; for (int i=0; i<4; i++) { int ndir = (dir+3+i)%4; a = refd[ndir]; b = cur_y + refy[ndir]; c = cur_x + refx[ndir]; if (WithinRange(a, b, c) && walls[a][b][c] == '1') { cur_y += dy[ndir]; cur_x += dx[ndir]; printf("%c", ans[ndir]); dir = ndir; break; } } //printf("(%d, %d)\n", cur_y, cur_x); if (cur_y == 0 && cur_x == 0) break; } puts(""); }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int, int>P; int dx[]{ 0,1,0,-1 }, dy[]{ 1,0,-1,0 }; char dir[] = "RDLU"; bool b[5][5][5][5]; int main() { rep(i, 9) { rep(j, i & 1 ? 5 : 4) { int d; scanf("%1d", &d); if (i & 1)b[i / 2][j][i / 2 + 1][j] = b[i / 2 + 1][j][i / 2][j] = d; else b[i / 2][j][i / 2][j + 1] = b[i / 2][j + 1][i / 2][j] = d; } } int id = 0, x = 0, y = 0; do { for (int i = 3;; i++) { int nx = x + dx[(id + i) % 4], ny = y + dy[(id + i) % 4]; if (0 <= nx&&nx < 5 && 0 <= ny&&ny < 5 && b[x][y][nx][ny]) { x = nx; y = ny; (id += i) %= 4; printf("%c", dir[id]); break; } } } while (x != 0 || y != 0); printf("\n"); }
#include<iostream> #include<string> using namespace std; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; char dc[]={'R','D','L','U'}; bool t[7][7][4]; string str; int flg=0; void init(); void getnum(int,int,int); bool ck(int y,int x,int d){ if(t[y][x][d]==1)return true; int ny=y+dy[d]; int nx=x+dx[d]; if(t[ny][nx][(d+2)%4]==1)return true; return false; } void rec(int y,int x,int d); int main(){ init(); //cout<<"test"<<endl; for(int i=1;i<=4;i++){ for(int j=1;j<=4;j++){ //cin>>t[i][j][0]; getnum(i,j,0); } for(int j=1;j<=5;j++){ //cin>>t[i][j][1]; getnum(i,j,1); } } for(int i=1;i<=4;i++)getnum(5,i,0);//cin>>t[5][i][0]; //cout<<"inputend"<<endl; rec(1,1,0); return 0; } void init(){ for(int i=0;i<7;i++) for(int j=0;j<7;j++) for(int k=0;k<4;k++) t[i][j][k]=0; string s; str=""; for(int i=0;i<9;i++){ cin>>s; str+=s; } } void rec(int y,int x,int d){ //cout<<y << " " <<x<<" "<<d<<endl; flg++; if(y==1&&x==1&&flg>1){ cout<<endl; return; } int d1=(d+1)%4; int d2=(d+2)%4; int d3=(d+3)%4; if(ck(y,x,d3)){ cout<<dc[d3]; rec(y+dy[d3],x+dx[d3],d3); }else if(ck(y,x,d)){ cout<<dc[d]; rec(y+dy[d],x+dx[d],d); }else if(ck(y,x,d1)){ cout<<dc[d1]; rec(y+dy[d1],x+dx[d1],d1); }else{ cout<<dc[d2]; rec(y+dy[d2],x+dx[d2],d2); } } void getnum(int a,int b,int c){ if(str[0]=='0')t[a][b][c]=0; else t[a][b][c]=1; if(str.size()>=2)str=str.substr(1); }
#include <stdio.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char *dat = "RDLU"; int w[6][6][4]; int main() { rep (i, 9) { char t; if (i % 2 == 0) { rep (j, 4) { scanf(" %c", &t); if (t == '1') w[i/2][j][0] = w[i/2][j+1][2] = 1; } } else { rep (j, 5) { scanf(" %c", &t); if (t == '1') w[i/2][j][1] = w[i/2+1][j][3] = 1; } } } int x = 0, y = 0, d = 0; do { rep (i, 4) { const int nd = (d+i) % 4; if (w[y][x][nd]) { putchar(dat[nd]); x += dx[nd]; y += dy[nd]; d = (nd + 3) % 4; break; } } } while (x != 0 || y != 0); putchar('\n'); return 0; }
#include <iostream> #include <iomanip> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <utility> #include <stack> #include <queue> #include <map> #include <set> #include <functional> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define rep(i,n) for(int i=0; i<(int)(n); i++) #define repc(i,s,e) for(int i=(s); i<(int)(e); i++) #define pb push_back #define mp make_pair #define all(r) r.begin(),r.end() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, pii> pip; const int INF = 1000000; const double EPS = 1e-8; const int mod = 1e9 + 7; int dx[]={1,0,-1,0}; int dy[]={0,-1,0,1}; int main(){ int p[5][5][4]; memset(p, 0, sizeof(p)); string s; rep(i, 9){ cin>>s; rep(j, s.size()){ if(s[j] == '1'){ if(i%2 == 0){ p[i/2][j][0] = 1; p[i/2][j + 1][2] = 1; }else{ p[i/2][j][3] = 1; p[i/2 + 1][j][1] = 1; } } } } int dir = 0; s ="RULD"; int x = 0, y = 0; while(1){ if(dir == 0){ if(p[y][x][1]){ dir = 1; }else if(p[y][x][0]){ }else if(p[y][x][3]){ dir = 3; }else if(p[y][x][2]){ dir = 2; } }else if(dir == 1){ if(p[y][x][2]){ dir = 2; }else if(p[y][x][1]){ }else if(p[y][x][0]){ dir = 0; }else if(p[y][x][3]){ dir = 3; } }else if(dir == 2){ if(p[y][x][3]){ dir = 3; }else if(p[y][x][2]){ }else if(p[y][x][1]){ dir = 1; }else if(p[y][x][0]){ dir = 0; } }else{ if(p[y][x][0]){ dir = 0; }else if(p[y][x][3]){ }else if(p[y][x][2]){ dir = 2; }else if(p[y][x][1]){ dir = 1; } } cout<<s[dir]; x += dx[dir]; y += dy[dir]; //printf(" (%d, %d)\n", x, y); if(x == 0 && y == 0) break; } printf("\n"); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(a) (a).begin(),(a).end() int main(){ bool w[5][5][4]={}; char a; rep(i,5){ rep(j,4){ cin>>a; w[i][j][0]=a-'0'; w[i][j+1][2]=a-'0'; } if(i==4)continue; rep(j,5){ cin>>a; w[i][j][1]=a-'0'; w[i+1][j][3]=a-'0'; } } assert(w[0][0][0]); int py=0,px=0; int dir=0; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; string ans=""; string ch[]={"R","D","L","U"}; do{ for(int i=-1;i<=2;i++){ int nd=(dir+i+4)%4; if(w[py][px][nd]){ ans+=ch[nd]; py+=dy[nd]; px+=dx[nd]; dir=nd; break; } } }while(py!=0||px!=0); cout<<ans<<endl; }
#include <iostream> using namespace std; int main(){ int muki = 1;//0 is n,1 is e,2 is s,3 is w int t = 1, myoko= 1, mtate = 0; char yoko[7][6], tate[7][6]; for(int i = 0; i < 7; i++){ for(int j = 0; j < 6; j++){ yoko[i][j] = '0'; tate[i][j] = '0'; } } for(int i = 1; i < 10; i++){ if(i % 2 != 0){ for(int j = 1; j < 5; j++){ cin >> yoko[(i/2)+1][j]; } } else{ for(int j = 1; j < 6; j++){ cin >> tate[j][t]; } t++; } } cout << "R"; while(myoko != 0 || mtate != 0){ if(muki == 0){ if(yoko[mtate+1][myoko] == '1'){ myoko = myoko-1; cout << "L"; muki = 3; } else if(tate[myoko+1][mtate] == '1'){ mtate = mtate-1; cout << "U"; muki = 0; } else if(yoko[mtate+1][myoko+1] == '1'){ myoko = myoko+1; cout << "R"; muki = 1; } else{ mtate = mtate+1; cout << "D"; muki = 2; } } else if(muki == 1){ if(tate[myoko+1][mtate] == '1'){ mtate = mtate-1; cout << "U"; muki = 0; } else if(yoko[mtate+1][myoko+1] == '1'){ myoko = myoko+1; cout << "R"; muki = 1; } else if(tate[myoko+1][mtate+1] == '1'){ mtate = mtate+1; cout << "D"; muki = 2; } else{ myoko = myoko-1; cout << "L"; muki = 3; } } else if(muki == 2){ if(yoko[mtate+1][myoko+1] == '1'){ myoko = myoko+1; cout << "R"; muki = 1; } else if(tate[myoko+1][mtate+1] == '1'){ mtate = mtate+1; cout << "D"; muki = 2; } else if(yoko[mtate+1][myoko] == '1'){ myoko = myoko-1; cout << "L"; muki = 3; } else{ mtate = mtate-1; cout << "U"; muki = 0; } } else{ if(tate[myoko+1][mtate+1] == '1'){ mtate = mtate+1; cout << "D"; muki = 2; } else if(yoko[mtate+1][myoko] == '1'){ myoko = myoko-1; cout << "L"; muki = 3; } else if(tate[myoko+1][mtate] == '1'){ mtate = mtate-1; cout << "U"; muki = 0; } else{ myoko = myoko+1; cout << "R"; muki = 1; } } } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int dx[]={1,0,-1,0,1,-1,-1,1}; const int dy[]={0,1,0,-1,1,1,-1,-1}; const int INF = 1<<30; const double EPS = 1e-8; #define PB push_back #define mk make_pair #define fr first #define sc second #define ll long long #define reps(i,j,k) for(int i = (j); i < (k); i++) #define rep(i,j) reps(i,0,j) #define MOD 1000000007 #define lengthof(x) (sizeof(x) / sizeof(*(x))) typedef pair<int,int> Pii; typedef pair<Pii,Pii> P; typedef vector<int> vi; typedef vector<vi> vvi; int main(){ char d[] = {'R','D','L','U'}; bool w[5][9]={{false}}; rep(i,9){ int lim = 4+i%2; rep(j,lim){ char t; cin >> t; w[j][i] = (t == '1'); } } int x,y,s; x = y = s = 0; while(true){ int nx,ny; s--; s = (s+4) % 4; rep(i,4){ nx = x + dx[s]; ny = y + dy[s]; if(nx >= 0 && nx <= 4 && ny >= 0 && ny <= 4){ if(s%2==0 && w[min(x,nx)][y*2])break; if(s%2==1 && w[x][min(y,ny) * 2 + 1])break; } s++; s %= 4; } y = ny; x = nx; cout << d[s]; if((x == 0 && y == 0))break; } puts(""); return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cctype> #include<math.h> #include<string> #include<string.h> #include<stack> #include<queue> #include<vector> #include<utility> #include<set> #include<map> #include<stdlib.h> #include<iomanip> using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define MOD 1000000007 #define rep(i,n) for(i=0;i<(n);i++) #define loop(i,a,n) for(i=a;i<(n);i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int,int> pii; int g[6][6][4]={};//top,bottom,left,right int main(void) { int i,j; rep(i,9){ string s; cin>>s; if(i%2){ rep(j,5){ g[i/2+1][j][3] = s[j] - '0'; g[i/2+1][1+j][2] = g[i/2+1][j][3]; } }else{ rep(j,4){ g[i/2+1][1+j][0] = s[j] - '0'; g[i/2][1+j][1] = g[i/2+1][1+j][0]; } } } char s[5] = {"LRDU"}; int dx[4] = {0,0,1,-1}; int dy[4] = {-1,1,0,0}; int x=0,y=1,z=1;//(x,y)右手がz while(x+y){ if(g[x][y][z]){ cout<<s[z]; if((z==1 && g[x][y][1] && g[x][y][3]) || (z==0 && g[x][y][0] && g[x][y][2]) || (z==2 && g[x][y][2] && g[x][y][1]) || (z==3 && g[x][y][3] && g[x][y][0])){ switch(z){ case 0: z=2;break; case 1: z=3;break; case 2: z=1;break; case 3: z=0;break; } }else{ x = x + dx[z]; y = y + dy[z]; } }else{ switch(z){ case 0: z=3;x--;break; case 1: z=2;x++;break; case 2: z=0;y--;break; case 3: z=1;y++;break; } } } cout<<endl; }
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { vector<vector<vector<bool> > > wall(5, vector<vector<bool> >(5, vector<bool>(4, false))); for(int i=0; i<5; ++i){ for(int j=0; j<4; ++j){ char a; cin >> a; if(a == '1') wall[i][j][0] = wall[i][j+1][2] = true; } if(i < 4){ for(int j=0; j<5; ++j){ char a; cin >> a; if(a == '1') wall[i][j][1] = wall[i+1][j][3] = true; } } } char dir[] = {'R', 'D', 'L', 'U'}; int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; int y = 0; int x = 0; int d = 0; do{ if(wall[y][x][d]){ y += dy[d]; x += dx[d]; cout << dir[d]; d += 3; d %= 4; }else{ ++ d; d %= 4; } }while(y != 0 || x != 0); cout << endl; return 0; }
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <cstring> #include <vector> #include <map> #include <utility> #include <complex> #include <stack> #include <climits> #include <set> #include <numeric> using namespace std; bool points[5][5][4]; int main() { for (int i = 0; i < 9; ++i){ int y = i/2; if (i%2 == 0){ for (int j = 0; j < 4; ++j){ int e; scanf("%1d", &e); if (e) points[y][j][1] = points[y][j+1][3] = true; } } else { for (int j = 0; j < 5; ++j){ int e; scanf("%1d", &e); if (e) points[y][j][2] = points[y+1][j][0] = true; } } } int x, y; x = 1; y = 0; int dir = 1; int dx[] = { 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0 }; char* ds = "URDL"; putchar(ds[dir]); while (!(x == 0 && y == 0)) { if (points[y][x][(dir+3)%4]) dir = (dir+3)%4; else if (points[y][x][dir]) ; else if (points[y][x][(dir+1)%4]) dir = (dir+1)%4; else dir = (dir+2)%4; x += dx[dir]; y += dy[dir]; putchar(ds[dir]); } putchar('\n'); return 0; }
#include<iostream> using namespace std; string s[9]; int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; int r; bool in(int a) { return 0<=a&&a<=4; } bool check(int a,int b,int c,int d) { if(!(in(a)&&in(b)&&in(c)&&in(d)))return false; if(a==c) { return s[2*a][b<d?b:d]=='1'; } else { return s[2*(a<c?a:c)+1][b]=='1'; } } int main() { for(int i=0;i<9;i++)cin>>s[i]; int x=0,y=0; while(1) { for(int dr=3;dr<7;dr++) { int nr=(r+dr)%4; int tx=x+dx[nr],ty=y+dy[nr]; if(check(x,y,tx,ty)) { r=nr; x=tx; y=ty; break; } } cout<<"RDLU"[r]; if(x==0&&y==0)break; } cout<<endl; }
#include<cstdio> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<map> #include<set> #include<complex> #include<stack> #include<cmath> using namespace std; #define reps(i,f,n) for(int i=f;i<int(n);i++) #define rep(i,n) reps(i,0,n) class P{ public: int a,b,c; P(){} P(int a,int b,int c):a(a),b(b),c(c){} }; int main(){ const int N = 20; const int B = 2; string m[2][N]; P dp[4]; dp[0]=P(0,0,0); dp[1]=P(1,-1,0); dp[2]=P(0,-1,0); dp[3]=P(1,-1,1); int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; string ds="RDLU"; rep(i,B){ m[0][i]=m[1][i]="00000000000"; m[0][i+9/2+B]=m[1][i+9/2+B]="00000000000"; } rep(i,9){ string s; cin>>s; if(i%2==0){ m[0][i/2+B]="00"+s+"00"; } else{ m[1][i/2+B]="00"+s+"00"; } } int x = B; int y = B; int dir = 0; string ans = ""; while(1){ if(x==B && y==B && dir==0 && ans.size()!=0)break; //printf("%d %d %d [%s]%d\n",x,y,dir,ans.c_str(),ans.size()); if(ans.size()>50)break; P right = dp[dir]; P front = dp[(dir+3)%4]; ans += ds[dir]; if(m[front.a][y+front.b][x+front.c]=='1'){ dir=(dir+3)%4; }else if(m[right.a][y+right.b+dy[dir]][x+right.c+dx[dir]]=='0'){ P nr = dp[dir]; while(m[nr.a][y+nr.b+dy[dir]][x+nr.c+dx[dir]]=='0'){ x+=dx[dir]; y+=dy[dir]; dir=(dir+1)%4; nr=dp[dir]; } x+=dx[dir]; y+=dy[dir]; }else{ x+=dx[dir]; y+=dy[dir]; } } cout<<ans<<endl; }
#include <iostream> using namespace std; char horz[5][4+1]; char vert[4][5+1]; int x, y, vx, vy; void turn_left() { int temp = vx; vx = vy; vy = -temp; } void turn_right() { int temp = vx; vx = -vy; vy = temp; } inline bool ok(char c) { return (c == '1'); } bool avail() { if (vx == 1) { if (x < 4 && ok(horz[y][x])) return true; } else if (vx == -1) { if (x > 0 && ok(horz[y][x-1])) return true; } else if (vy == 1) { if (y < 4 && ok(vert[y][x])) return true; } else /* if (vy == -1) */ { if (y > 0 && ok(vert[y-1][x])) return true; } return false; } void print() { if (vx == 1) cout << "R"; else if (vx == -1) cout << "L"; else if (vy == 1) cout << "D"; else /* if (vy == -1) */ cout << "U"; } int main() { for (int i = 0; i < 4; i++) cin >> horz[i] >> vert[i]; cin >> horz[4]; x=0, y=0, vx=0, vy=-1; do { while (!avail()) turn_right(); print(); x += vx, y += vy; turn_left(); } while (x != 0 || y != 0); cout << endl; return 0; }
#include<iostream> #include<cassert> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define inf (1<<29) #define MAX 100 using namespace std; struct You {//??????????????????????????§?¨????dx[(dir+1)%4],dy[(dir+1)%4]??????????????´??? int x,y,dir; You(int x=-inf,int y=-inf):x(x),y(y),dir(0){} }; char G[MAX][MAX]; int dx[] = {+1,+0,-1,+0}; int dy[] = {+0,+1,+0,-1}; void init() { rep(i,MAX)rep(j,MAX)G[i][j] = '.'; } void makeGraph() { string line; int x; REP(y,1,10) {//odd -> yoko, even -> tate cin >> line; x = (y%2?1:0); rep(i,line.size()) { if(line[i] == '1') { G[y][x] = (y%2?'-':'|'); if(y%2) G[y][x-1] = G[y][x+1] = 'o'; else G[y+1][x] = G[y-1][x] = 'o'; } x += 2; } } } void print() { rep(i,11) { rep(j,11) { cout << G[i][j]; } cout << endl; } cout << endl; } void compute() { You you(0,0); bool fir = true; while(true) { int x = you.x,y = you.y,dir = you.dir; int hx = x + dx[(dir+1)%4],hy = y + dy[(dir+1)%4]; if(!fir && hx == 0 && hy == 1) { cout << '\n'; return; } fir = false; int nx = x + dx[dir],ny = y + dy[dir]; hx += dx[dir],hy += dy[dir]; assert(G[ny][nx] != 'o'); if(G[ny][nx] == '|' || G[ny][nx] == '-') { dir = (dir + 3)%4; you.dir = dir; } else if(G[hy][hx] == '.') { dir = (dir + 1)%4; you.x = nx + dx[dir], you.y = ny + dy[dir],you.dir = dir; } else if(G[hy][hx] == 'o' || G[hy][hx] == '-' || G[hy][hx] == '|') { you.x += dx[dir],you.y += dy[dir]; } hx = you.x + dx[(dir+1)%4], hy = you.y + dy[(dir+1)%4]; if(G[hy][hx] != 'o') cout << (dir==0?'R':(dir==1?'D':(dir==2?'L':'U'))); } } int main() { init(); makeGraph(); compute(); return 0; }
#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <sstream> #include <map> #include <set> #include <cstdio> #define REP(i,k,n) for(int i=k;i<n;i++) #define rep(i,n) for(int i=0;i<n;i++) #define INF 1<<30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> P; int dy[4] = {-1,0,1,0}; int dx[4] = {0,1,0,-1}; string ans = "URDL"; bool f[5][5][5]; int main() { memset(f,0,sizeof(f)); rep(i,9) { int y = i/2; if(i%2 == 0) { string t; cin >> t; rep(j,4) { if(t[j] == '1') { f[y][j][1] = true; f[y][j+1][3] = true; } } } else { string t; cin >> t; rep(j,5) { if(t[j] == '1') { f[y+1][j][0] = true; f[y][j][2] = true; } } } } int dy[4] = {-1,0,1,0}; int dx[4] = {0,1,0,-1}; string ans = "URDL"; int y = 0, x = 0, dir = 1; do { if(f[y][x][(dir+3)%4]) { dir = (dir+3)%4; } else if(f[y][x][dir]) { } else if(f[y][x][(dir+1)%4]) { dir = (dir+1)%4; } else if(f[y][x][(dir+2)%4]) { dir = (dir+2)%4; } y += dy[dir]; x += dx[dir]; cout << ans[dir]; } while(y != 0 || x != 0); cout << endl; return 0; }
#include<iostream> #include<cstdio> using namespace std; int main(){ int field[6][6] = {0},a,x = 1,y = 0,t = 2; for(int i=0; i<5; i++){ for(int j=0; j<4; j++){ scanf("%1d",&a); field[j+1][i]+=(a<<2); field[j+1][i+1]+=(a<<0); } if(i<4){ for(int j=0; j<5; j++){ scanf("%1d",&a); field[j][i+1]+=(a<<1); field[j+1][i+1]+=(a<<3); } } } for(;;){ if(t == 1){ if(field[x][y]&2){ cout <<"U"; if(field[x][y]&1){t = 8;} else{y--;} } else{x++;t = 2;} } else if(t == 2){ if(field[x][y]&4){ cout <<"R"; if(field[x][y]&2){t = 1;} else{x++;} } else{y++;t = 4;} } else if(t == 4){ if(field[x][y]&8){ cout <<"D"; if(field[x][y]&4){t = 2;} else{y++;} } else{x--;t = 8;} } else if(t == 8){ if(field[x][y]&1){ cout <<"L"; if(field[x][y]&8){t = 4;} else{x--;} } else{y--;t = 1;} } if(!x && !y){break;} } cout <<endl; return 0; }
#include<cstdio> #include<iostream> #include<string> #define R 1 #define D 2 #define L 3 #define U 4 using namespace std; int main(void){ int tate[6][7]={0}; char inp; int yoko[7][6]={0}; int x,y; int now; for(int i=1;i<6;i++){ for(int j=1;j<5;j++){ inp=getchar(); yoko[i][j]=(int)(inp-'0'); } getchar(); if(i!=5){ for(int j=1;j<6;j++){ inp=getchar(); tate[i][j]=(int)(inp-'0'); } getchar(); } } now=R; bool f=false; x=1,y=1; while(1){ if(now==R){ now=D; if(tate[y-1][x]==1&&f==false) now=U,f=true; else if(yoko[y][x]==1){ x++; cout<<"R"; now=R; f=false; } } if(now==D){ now=L; if(yoko[y][x]==1&&f==false) now=R,f=true; else if(tate[y][x]==1){ y++; cout<<"D"; now=D; f=false; } } if(now==L){ now=U; if(tate[y][x]==1&&f==false) now=D,f=true; else if(yoko[y][x-1]==1){ x--; cout<<"L"; now=L; f=false; } } if(now==U){ now=R; if(yoko[y][x-1]==1&&f==false) now=L,f=true; else if(tate[y-1][x]==1){ y--; cout<<"U"; now=U; f=false; } } if(x==1&&y==1) break; } cout<<endl; return 0; }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int, int>P; int dx[]{ 0,1,0,-1 }, dy[]{ 1,0,-1,0 }; char dir[] = "RDLU"; bool b[5][5][5][5]; int main() { rep(i, 9) { rep(j, i & 1 ? 5 : 4) { int d; scanf("%1d", &d); if (i & 1)b[i / 2][j][i / 2 + 1][j] = b[i / 2 + 1][j][i / 2][j] = d; else b[i / 2][j][i / 2][j + 1] = b[i / 2][j + 1][i / 2][j] = d; } } int id = 0, x = 0, y = 0; do { for (int i = 3; i <= 6; i++) { int nx = x + dx[(id + i) % 4], ny = y + dy[(id + i) % 4]; if (0 <= nx&&nx < 5 && 0 <= ny&&ny < 5 && b[x][y][nx][ny]) { x = nx; y = ny; (id += i) %= 4; printf("%c", dir[id]); break; } } } while (x != 0 || y != 0); printf("\n"); }
#include <cstdio> #include <string> #include <iostream> using namespace std; string str[100]; int len; int fie[101][101]; int m; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; char s[4]={'R','D','L','U'}; int main(void){ int n=9; for(int i=0;i<10;i++)cin >> str[i]; m=str[0].size(); for(int i=0;i<n;i++){ for(int j=0;j<str[i].size();j++){ if(str[i][j]=='1'){ if(i%2==0)fie[j*2+2][i+1]=1; if(i%2==1)fie[j*2+1][i+1]=1; } } } int x=2,y=0,dir=0; while(1){ int nx=x+dx[(dir+1)%4],ny=y+dy[(dir+1)%4]; if(nx>=0 && nx<=m*2+2 && ny>=0 && ny<=n+1){ if(fie[nx][ny]==1)printf("%c",s[dir]); } nx=x+dx[dir],ny=y+dy[dir]; if(nx>=0 && nx<=m*2+2 && ny>=0 && ny<=n+1){ if(fie[nx][ny]==0){ x+=dx[dir]*2; y+=dy[dir]*2; if(fie[x+dx[(dir+1)%4]][y+dy[(dir+1)%4]]==0){ dir=(dir+1)%4; } }else{ if(fie[x+dx[(dir+1)%4]][y+dy[(dir+1)%4]]==0){ dir=(dir+1)%4; x+=dx[dir]*2; y+=dy[dir]*2; }else if(fie[x+dx[(dir+3)%4]][y+dy[(dir+3)%4]]==0){ dir=(dir+3)%4; x+=dx[dir]*2; y+=dy[dir]*2; printf("%c",s[dir]); }else{ dir=(dir+2)%4; x+=dx[dir]*2; y+=dy[dir]*2; printf("%c",s[(dir+1)%4]); printf("%c",s[dir]); } } }else dir=(dir+1)%4; //printf("%d %d %d %d %d\n",x,y,dir,dx[dir],dy[dir]); if(x==0 && y==0)break; } printf("\n"); return 0; }
#include <iostream> #include <cstring> using namespace std; typedef struct{ int flg[4]; }Point; int main(void){ int di[] = {-1,0,1,0}; int dj[] = {0,1,0,-1}; char ch[] = "URDL"; Point p[5][5]; memset(p,0,sizeof(p)); for(int i=0;i<9;i++){ char wall[8]; cin>>wall; if(i % 2){ for(int j=0;j<5;j++){ p[i/2][j].flg[2] = p[i/2+1][j].flg[0] = wall[j] - '0'; } } else{ for(int j=0;j<4;j++){ p[i/2][j].flg[1] = p[i/2][j+1].flg[3] = wall[j] - '0'; } } } int pi=0, pj=1, d=1; cout<<"R"; while(pi != 0 || pj != 0){ int front = p[pi][pj].flg[d]; int left = p[pi][pj].flg[(d+3)%4]; if(left == 1 || left == 0 && front == 1){ if(left == 1) d = (d+3)%4; cout<<ch[d]; pi += di[d]; pj += dj[d]; } else{ d = (d+1)%4; } } cout<<endl; return 0; }
#include<stdio.h> int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; char*S="RDLU",c; int main() { int e[5][5][4]={}; int i,j,x,y,a; for(i=0;i<9;++i) { if(i&1) for(j=0;j<5;++j) scanf(" %c",&c),e[i/2][j][1]=e[i/2+1][j][3]=c-'0'; else for(j=0;j<4;++j) scanf(" %c",&c),e[i/2][j][0]=e[i/2][j+1][2]=c-'0'; } for(x=y=a=0;;) { if(e[y][x][(a+3)%4]) { a=(a+3)%4; putchar(S[a]); x+=dx[a]; y+=dy[a]; } else if(e[y][x][a]) { putchar(S[a]); x+=dx[a]; y+=dy[a]; } else if(e[y][x][(a+1)%4]) { a=(a+1)%4; putchar(S[a]); x+=dx[a]; y+=dy[a]; } else a=(a+2)%4; if(x+y+a==0)break; } puts(""); return 0; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <math.h> #include <vector> #include <set> using namespace std; typedef long long LL; static const double EPS = 1e-9; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) /* const int dx[4] = {-1,0,0,0}; const int dy[4] = {0,-1,0,0}; const int dx2[4] = {-1,0,1,0}; const int dy2[4] = {0,1,0,-1}; */ static const int dx[4] = {1,0,-1,0}; static const int dy[4] = {0,1,0,-1}; int main(void){ int d = 0; /* int a[5][4]; int b[4][5]; */ int m[9][9]; int x=0,y=0; string tmp; /* for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ c = getchar(); a[i][j] = c - '0'; } getchar(); for(int j=0; j<5; j++){ c = getchar(); b[i][j] = c - '0'; } getchar(); } for(int j=0; j<4; j++){ c = getchar(); a[4][j] = c - '0'; } */ for(int i=0; i<9; i++){ cin>>tmp; int jbegin; if(i%2){ jbegin=0; }else{ jbegin=1; } for(int j=jbegin,k=0; j<9; j+=2,k++){ m[i][j] = tmp[k]; } } for(int time=0; time>-1; time++){ for(int r=-1; r<3; r++){ //printf("%d",(d+r+4)%4); if(y+dy[(d+r+4)%4]>=0&&y+dy[(d+r+4)%4]<=8 &&x+dx[(d+r+4)%4]>=0&&x+dx[(d+r+4)%4]<=8&& m[y+dy[(d+r+4)%4]][x+dx[(d+r+4)%4]]=='1'){ d = (d+r+4)%4; x += 2*dx[d]; y += 2*dy[d]; break; } } char out; switch(d){ case 0: out = 'R'; break; case 1: out = 'D'; break; case 2: out = 'L'; break; case 3: out = 'U'; break; } putchar(out); //printf("%d,%d\n",x,y); if(x==0&&y==0) break; } cout<<endl; return 0; }
#include<iostream> #include<cstdio> #include<vector> using namespace std; #define int long long int gd(int d, vector<int> w){ if(w[(d+1)%4]==1) return (d+1)%4; else if(w[d]==1) return d; else if(w[(d+3)%4]==1) return (d+3)%4; else return (d+2)%4; }; signed main(){ char buf[256]; vector<vector<vector<int>>> rms(5,vector<vector<int>>(5, vector<int>(4,0))); for(int i=0;i<9;i++){ scanf("%s",buf); if(i%2==0){ for(int j=0;j<4;j++){ if(buf[j]=='1'){ rms[i/2][j][0] = 1; rms[i/2][j+1][2] = 1; } } }else{ for(int j=0;j<5;j++){ if(buf[j]=='1'){ rms[i/2][j][3] = 1; rms[i/2+1][j][1] = 1; } } } } int x=0,y=0,d=0; while(true){ d = gd(d,rms[y][x]); if(d==0){cout<<"R"; x++;} else if(d==1){cout<<"U"; y--;} else if(d==2){cout<<"L"; x--;} else {cout<<"D"; y++;} if(x==0&&y==0) break; } cout << endl; return 0; }
// 今向いている方向にとっての左から始めて、時計回りに壁を探して、最初にみつけた壁の方向に進む #include <fstream> #include <iostream> #include <cmath> #include <vector> #include <queue> #include <utility> #include <algorithm> using namespace std; struct Direction { bool u, d, r, l; }; void dump_Direction(Direction di) { cout << "[" << di.u << di.r << di.d << di.l << "]"; } int main() { // ifstream cin("../test.txt"); Direction g[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { g[i][j].u = false; g[i][j].d = false; g[i][j].r = false; g[i][j].l = false; } } for (int i = 0; i < 9; i++) { string s; cin >> s; if (i % 2 == 0) { for (int j = 0; j < 4; j++) { int k = i / 2; g[k][j].r = (s[j] == '1'); g[k][j+1].l = (s[j] == '1'); } } else { for (int j = 0; j < 5; j++) { int k = i / 2; g[k][j].d = (s[j] == '1'); g[k+1][j].u = (s[j] == '1'); } } } // 表示用 // for (int i = 0; i < 5; i++) { // for (int j = 0; j < 5; j++) { // dump_Direction(g[i][j]); // } // cout << endl; // } int x = 0; int y = 0; int d = 0; // 0: u, 1: r, 2: d, 3: l int dx[] = {0,1,0,-1}; int dy[] = {-1,0,1,0}; char c[] = {'U','R','D','L'}; while (true) { bool dir[] = {g[y][x].u, g[y][x].r, g[y][x].d, g[y][x].l}; // dump_Direction(g[y][x]); int i = 0; for (;i < 4; i++) { if (dir[(i+d)%4]) break; } int j = (i+d)%4; // cout << j; x += dx[j]; y += dy[j]; cout << c[j]; d = (j + 3) % 4; if (!(x|y)) break; } cout << endl; }
#include <cstdio> using namespace std; int main(){ char yoko[7][6];//5 * 4 char tate[6][7];//4 * 5 int x, y;//position char dir;//next direction for(int i = 0; i < 4; i++){ scanf("%s", yoko[i]); scanf("%s", tate[i]); } scanf("%s", yoko[4]); x = 0; y = 0; dir = 'R'; while(true){ printf("%c", dir); switch (dir){ case 82://right x++; if(y > 0 && tate[y - 1][x] == '1') dir = 'U'; else if(yoko[y][x] == '1') dir = 'R'; else if(tate[y][x] == '1') dir = 'D'; else dir = 'L'; break; case 68://down y++; if(yoko[y][x] == '1') dir = 'R'; else if(tate[y][x] == '1') dir = 'D'; else if(x > 0 && yoko[y][x - 1] == '1') dir = 'L'; else dir = 'U'; break; case 76://left x--; if(tate[y][x] == '1') dir = 'D'; else if(x > 0 && yoko[y][x - 1] == '1') dir = 'L'; else if(y > 0 && tate[y - 1][x] == '1') dir = 'U'; else dir = 'R'; break; case 85://up y--; if(x > 0 && yoko[y][x - 1] == '1') dir = 'L'; else if(y > 0 && tate[y - 1][x] == '1') dir = 'U'; else if(yoko[y][x] == '1') dir = 'R'; else dir = 'D'; break; default : break; } if(x == 0 && y == 0) break; } printf("\n"); return 0; }