text
stringlengths
49
983k
#include <iostream> #include <sstream> #include <string> #include <vector> #include <deque> #include <stack> #include <queue> #include <set> #include <map> #include <bitset> #include <algorithm> #include <numeric> #include <complex> #include <functional> #include <utility> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; #define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl #define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,n) repi(i,0,n) #define iter(c) __typeof((c).begin()) #define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++) #define allof(c) (c).begin(),(c).end() #define mp make_pair typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; 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); //rep(i,9){ // rep(j,9) // cout<<grid[i][j]<<" "; // cout<<endl; //} int i=0,j=0; dir prev=RIGHT; for(;;){ //printf("# (%d,%d) prev:%c\n",i,j,output[prev]); dir next[4]; if(prev==UP){ next[0]=LEFT;next[1]=UP;next[2]=RIGHT;next[3]=DOWN; } else if(prev==RIGHT){ next[0]=UP;next[1]=RIGHT;next[2]=DOWN;next[3]=LEFT; } else if(prev==DOWN){ next[0]=RIGHT;next[1]=DOWN;next[2]=LEFT;next[3]=UP; } else if(prev==LEFT){ next[0]=DOWN;next[1]=LEFT;next[2]=UP;next[3]=RIGHT; } 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; } //printf("%d(%c)\n",k,output[next[k]]); 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 <iostream> #include <string> using namespace std; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; const char dir[] = "RDLU"; int main() { string input; while (cin >> input) { bool map[5][5][4]; // Initialization for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) for (int k = 0; k < 4; k++) map[i][j][k] = false; // Input (0,0) - (0,4) for (int i = 0; i < 4; i++) if (input[i] == '1') { map[0][i][0] = true; map[0][i + 1][2] = true; } // Input Others for (int i = 0; i < 8; i++) { cin >> input; int lim; (i % 2) ? (lim = 4) : (lim = 5); for (int j = 0; j < lim; j++) if (input[j] == '1') { if (i % 2) { map[(i + 1) / 2][j][0] = true; map[(i + 1) / 2][j + 1][2] = true; } else { map[i / 2][j][1] = true; map[(i / 2) + 1][j][3] = true; } } } // algorithm int x, y, d; x = y = d = 0; do { int del = (d + 3) % 4; for (int i = 0; i < 4; i++) if (map[y][x][(del + i) % 4]) { d = (del + i) % 4; break; } x += dx[d]; y += dy[d]; cout << dir[d]; } while (x || y); cout << endl; } return 0; }
#include <bits/stdc++.h> #define SIZE 300005 #define MOD 1000000007LL #define INF INT_MAX / 3 #define LLINF 1LL << 60 #define REP(i,n) for(int i=0;i<n;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define DOWN(i,b,a) for(int i=b;i>=a;i--) #define SET(a,c) memset(a,c,sizeof a) #define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define FOREACH(i,c) for(auto (i) : (c)) #define BIT(i,j) ((i)>>(j))&1 #define ALL(o) (o).begin(), (o).end() #define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end()) #define SQ(x) ((x)*(x)) using namespace std; typedef long long ll; typedef valarray<int> Array; typedef pair<ll,ll> Pll; typedef pair<int, int> Pii; typedef pair<double, double> Pdd; template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i];if(i==a.size()-1)cout<<endl;else cout<<" ";}} int gcd(int a,int b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);} int lcm(int a,int b){return a==0||b==0?0:a*b/gcd(a,b);} int a[35]; void solve() { string ans = ""; int pre, i, dir = 0, now = 0; int mov[] = {1,5,-1,-5}; string c[] = {"R","D","L","U"}; do { pre = (dir+2)%4; for(i=(pre+1)%4;i!=pre;++i%=4) { if(a[now]>>i&1) { dir = i; break; } } if(i == pre) dir = pre; ans += c[dir]; now += mov[dir]; } while(now != 0); cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); fill_n(a,35,0); string s; int c; REP(i,9) { cin >> s; c = i/2; if(i%2==0) { REP(j,4) if(s[j]=='1') { a[c*5+j] += 1; a[c*5+j+1] += 4; } } else { REP(j,5) if(s[j]=='1') { a[c*5+j] += 2; a[(c+1)*5+j] += 8; } } } solve(); return 0; }
#include<iostream> using namespace std; struct block{ bool right; bool down; bool left; bool up; block():right(false),down(false),left(false),up(false){} }; void ud(block* b,string& s,int row){ for(int i=0;i<4;i++){ if(s[i]=='1'){ b[row*6+i+1].down=true; b[(row+1)*6+i+1].up=true; } } } void lr(block* b,string& s,int row){ for(int i=0;i<5;i++){ if(s[i]=='1'){ b[row*6+i].right=true; b[row*6+i+1].left=true; } } } enum direction{UP,RIGHT,DOWN,LEFT}; int main(){ block b[36]{}; int p=1; direction d=direction::RIGHT; string s; cin>>s; ud(b,s,0); cin>>s; lr(b,s,1); cin>>s; ud(b,s,1); cin>>s; lr(b,s,2); cin>>s; ud(b,s,2); cin>>s; lr(b,s,3); cin>>s; ud(b,s,3); cin>>s; lr(b,s,4); cin>>s; ud(b,s,4); do{ switch(d){ case direction::UP: cout<<"U"; if(b[p].up){ d=direction::LEFT; }else{ p-=6; if(!b[p].right){ p+=1; d=direction::RIGHT; if(!b[p].down){ p+=6; d=direction::DOWN; } } } break; case direction::RIGHT: cout<<"R"; if(b[p].right){ d=direction::UP; }else{ p+=1; if(!b[p].down){ p+=6; d=direction::DOWN; if(!b[p].left){ p-=1; d=direction::LEFT; } } } break; case direction::DOWN: cout<<"D"; if(b[p].down){ d=direction::RIGHT; }else{ p+=6; if(!b[p].left){ p-=1; d=direction::LEFT; if(!b[p].up){ p-=6; d=direction::UP; } } } break; case direction::LEFT: cout<<"L"; if(b[p].left){ d=direction::DOWN; }else{ p-=1; if(!b[p].up){ p-=6; d=direction::UP; if(!b[p].right){ p+=1; d=direction::RIGHT; } } } break; } }while(p!=1); cout<<"\n"; }
#include <iostream> #include <string> #define N 10 using namespace std; int main(){ string str, ans = ""; bool h[N][N]; // setinel bool v[N][N]; char dir = 'R'; int mx = 1; int my = 1; int i, j, len; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { h[i][j] = v[i][j] = false; } } i = j = 1; while(cin >> str){ if(i == 1) len = str.size(); if(str.size() == len){ for (int k = 0; k < str.size(); k++) { h[i][k+1] = str[k] == '1' ? true : false; } i++; }else{ for (int k = 0; k < str.size(); k++) { v[j][k+1] = str[k] == '1' ? true : false; } j++; } } do{ if (dir == 'L') { if(v[my][mx]){ my++; dir = 'D'; }else if(h[my][mx-1]){ mx--; dir = 'L'; }else if(v[my-1][mx]){ my--; dir = 'U'; }else{ mx++; dir = 'R'; } }else if (dir == 'R') { if(v[my-1][mx]){ my--; dir = 'U'; }else if(h[my][mx]){ mx++; dir = 'R'; }else if(v[my][mx]){ my++; dir = 'D'; }else{ mx--; dir = 'L'; } }else if (dir == 'U') { if(h[my][mx-1]){ mx--; dir = 'L'; }else if(v[my-1][mx]){ my--; dir = 'U'; }else if(h[my][mx]){ mx++; dir = 'R'; }else{ my++; dir = 'D'; } }else if (dir == 'D') { if(h[my][mx]){ mx++; dir = 'R'; }else if(v[my][mx]){ my++; dir = 'D'; }else if(h[my][mx-1]){ mx--; dir = 'L'; }else{ my--; dir = 'U'; } } ans += dir; }while(!(mx == 1 && my == 1)); cout << ans << endl; return 0; }
#include<iostream> #include<vector> #include<string> using namespace std; vector<pair<int, int>>x[10][10]; string S; int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 }; char V[5] = "RDLU"; int main() { int cnt = 0; for (int i = 0; i < 9;i++){ cin >> S; if (i % 2 == 0) { for (int j = 0; j < S.size(); j++) { if (S[j] == '0')continue; x[cnt][j].push_back(make_pair(cnt, j + 1)); x[cnt][j + 1].push_back(make_pair(cnt, j)); } } else { for (int j = 0; j < S.size(); j++) { if (S[j] == '0')continue; x[cnt][j].push_back(make_pair(cnt + 1, j)); x[cnt + 1][j].push_back(make_pair(cnt, j)); } cnt++; } } int cx = 0, cy = 0, dir = 0; while (true) { cx += dx[dir]; cy += dy[dir]; cout << V[dir]; if (cx == 0 && cy == 0)break; for (int i = 3; i <= 6; i++) { int F = (i + dir) % 4; for (int j = 0; j < x[cx][cy].size(); j++) { if (x[cx][cy][j] == make_pair(cx + dx[F], cy + dy[F])) { dir = F; goto E; } } } E:; } cout << endl; return 0; }
#include <iostream> #include <algorithm> #define rep(i,n) for(int i = 0 ; i < n ; i++ ) using namespace std; 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 a,int b){ return 0 <= a && a < 9 && 0 <= b && b < 9; } int main(){ bool grid[9][9] = {{0}}; input(grid); int i = 0,j = 0; dir prev = RIGHT; for(;;){ dir next[] = {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 <iostream> #include <istream> #include <string> #include <vector> using namespace std; int main() { vector<bool> w(11 * 11, false); for (int i = 1; i < 10; i++) { for (int j = 1 + i % 2; j < 10; j += 2) { char c; cin >> c; if (c - '0') w[i * 11 + j] = true; } } int x = 2, y = 0, d = 1, dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; do { const string s = "URDL"; d = (d + 1) % 4; while (w[(y + dy[d]) * 11 + 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; }
#include <iostream> using namespace std; int main(void){ int state; //ツ前ツ嘉アツづ個督ョツつ「ツつスツ陛サツ古シ int x, y; //current position bool horizon[5][4]; bool vertical[4][5]; char tmp; int i,j; for(i=0;i<9;i++){ if(i%2 == 0){ for(j=0;j<4;j++){ cin >> tmp; if(tmp == '1'){ horizon[i/2][j] = true; }else{ horizon[i/2][j] = false; } } }else{ for(j=0;j<5;j++){ cin >> tmp; if(tmp == '1'){ vertical[i/2][j] = true; }else{ vertical[i/2][j] = false; } } } } state = 'R'; cout << "R"; x = 0; y = 1; while(!(x == 0 && y == 0)){ if(state == 'R'){ if(x - 1 >= 0 && vertical[x-1][y] ){ state = 'U'; x--; cout << "U"; }else if(y <= 3 && horizon[x][y]){ state = 'R'; y++; cout << "R"; }else if(x <= 3 && vertical[x][y]){ state = 'D'; x++; cout << "D"; }else{ state = 'L'; y--; cout << "L"; } }else if(state == 'D'){ if(y <= 3 && horizon[x][y]){ state = 'R'; y++; cout << "R"; }else if(x <= 3 && vertical[x][y]){ state = 'D'; x++; cout << "D"; }else if( y >= 1 && horizon[x][y-1]){ state = 'L'; y--; cout << "L"; }else{ state = 'U'; x--; cout << "U"; } }else if(state == 'L'){ if(x <= 3 && vertical[x][y]){ state = 'D'; x++; cout << "D"; }else if(y >= 1 && horizon[x][y-1]){ state = 'L'; y--; cout << "L"; }else if(x >= 1 && vertical[x-1][y]){ state = 'U'; x--; cout << "U"; }else{ state = 'R'; y++; cout << "R"; } }else{ if(y >= 1 && horizon[x][y-1]){ state = 'L'; y--; cout << "L"; }else if(x >= 1 && vertical[x-1][y]){ state = 'U'; x--; cout << "U"; }else if(y <= 3 && horizon[x][y]){ state = 'R'; y++; cout << "R"; }else{ state = 'D'; x++; cout << "D"; } } //cout << x << " " << y << endl; //cin >> i; } cout << endl; return 0; }
#include <iostream> #include <string> #include <array> #include <bitset> #include <cstdint> int main() { enum { Left, Up, Right, Down }; typedef std::array<std::bitset<4>, 5> Row; typedef std::array<Row, 5> Matrix; Matrix matrix; for (auto& row : matrix) { row.fill(std::bitset<4>()); } std::string input; std::getline(std::cin, input); for (std::uint16_t x = 0; x < 4U; ++x) { if (input[x] == '1') { matrix[0][x].set(Right); matrix[0][x + 1].set(Left); } } for (std::uint16_t y = 0; y < 4U; ++y) { std::getline(std::cin, input); for (std::uint16_t x = 0; x < 5U; ++x) { if (input[x] == '1') { matrix[y][x].set(Down); matrix[y + 1][x].set(Up); } } std::getline(std::cin, input); for (std::uint16_t x = 0; x < 4U; ++x) { if (input[x] == '1') { matrix[y + 1][x].set(Right); matrix[y + 1][x + 1].set(Left); } } } std::uint16_t y = 0, x = 0; char direct = 'R'; do { if (direct == 'R') { if (matrix[y][x][Up]) { direct = 'U'; --y; } else if (matrix[y][x][Right]) { direct = 'R'; ++x; } else if (matrix[y][x][Down]) { direct = 'D'; ++y; } else { direct = 'L'; --x; } } else if (direct == 'D') { if (matrix[y][x][Right]) { direct = 'R'; ++x; } else if (matrix[y][x][Down]) { direct = 'D'; ++y; } else if (matrix[y][x][Left]) { direct = 'L'; --x; } else { direct = 'U'; --y; } } else if (direct == 'L') { if (matrix[y][x][Down]) { direct = 'D'; ++y; } else if (matrix[y][x][Left]) { direct = 'L'; --x; } else if (matrix[y][x][Up]) { direct = 'U'; --y; } else { direct = 'R'; ++x; } } else if (direct == 'U') { if (matrix[y][x][Left]) { direct = 'L'; --x; } else if (matrix[y][x][Up]) { direct = 'U'; --y; } else if (matrix[y][x][Right]) { direct = 'R'; ++x; } else { direct = 'D'; ++y; } } std::cout << direct << std::flush; } while (not (y == 0 and x == 0)); std::cout << std::endl; return 0; }
#include<cstdio> using namespace std; #define r(i,n) for(int i=0;i<n;++i) int main() { int f[5][5]={{0}}; r(i,9){ char s[6]; scanf("%s",s); if(i%2){ r(j,5){ int a=s[j]-'0'; f[i/2][j]|=a<<2; f[i/2+1][j]|=a; } }else{ r(j,4){ int a=s[j]-'0'; f[i/2][j]|=a<<1; f[i/2][j+1]|=a<<3; } } } printf("R"); int y=0,x=1,p=1; do{ p=(p+2)%4; int d=p; do d=(d+1)%4; while(!((f[y][x]>>d)&1)); p=d; putchar("URDL"[d]); int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; x+=dx[d]; y+=dy[d]; }while(y!=0||x!=0||f[0][0]==6&&p==3); puts(""); return 0; }
#include <iostream> #include <sstream> using namespace std; void move(int,int,char); char det_angle(int,int,char); char perm_angle[4] = {'R','U','L','D'}; int grid[5][5][4] = {0}; int main(){ int flag; for(int i=0;i<9;i++){ if(i%2 == 0){ for(int j=0;j<4;j++){ fscanf(stdin,"%1d",&flag); //cout << flag; if(flag == 1){ //cout << "+"; grid[i/2][j][0] = 1; //R grid[i/2][j+1][2]= 1; //L } } } else{ for(int j=0;j<5;j++){ fscanf(stdin,"%1d",&flag); //cout << flag; if(flag == 1){ //cout << "-"; grid[i/2][j][3] = 1; //D grid[i/2+1][j][1]= 1; //U } } } } int x = 0,y =0; char angle = 'R'; do{ // move(x,y,det_angle(x,y,angle)); cout << angle; if(angle == 'R'){ ++y; } else if(angle == 'U'){ --x; } else if(angle == 'L'){ --y; } else if(angle == 'D'){ ++x; } angle = det_angle(x,y,angle); } while(x != 0 || y!= 0); //cout << 'R'; //move(0,1,'R'); cout << endl; } void move(int x,int y,char angle){ if(x == 0 && y== 0){ cout << endl; //exit(0); } cout << angle; if(angle == 'R'){ ++y; } else if(angle == 'U'){ --x; } else if(angle == 'L'){ --y; } else if(angle == 'D'){ ++x; } //cout << "test" << x <<" " << y; //move(x,y,det_angle(x,y,angle)); //cout << angle << endl; } char det_angle(int x,int y,char angle){ //cout << x << y << " " << angle << endl; int start; if(angle == 'R'){ start = 5; } else if(angle == 'U'){ start = 6; } else if(angle == 'L'){ start = 7; } else if(angle == 'D'){ start = 4; } //cout << start << endl; for(int i = start;start - i < 4 ;--i){ if(grid[x][y][i%4] == 1){ //cout << "kettei" << i <<endl ; return perm_angle[i%4]; } } return 'Z'; }
#include<iostream> #define E 0 #define N 1 #define W 2 #define S 3 using namespace std; int wall_v[5][5]; // wall_v[a][b] : ‘æa—ñ ã‚©‚çb”Ԗڂ̕ǂª‚ ‚é‚©‚Ç‚¤‚© ([a][4]‚͏í‚É0) int wall_h[5][5]; // wall_h[a][b] : ‘æas ¶‚©‚çb”Ԗڂ̕ǂª‚ ‚é‚©‚Ç‚¤‚© ([a][4]‚͏í‚É0) bool isRightWall(int x,int y,int dir){ if(dir==N) y--; if(dir==W) x--; if(y<0 || 4<y || x<0 || 4<x) return false; if(dir==E) return wall_h[y][x]; if(dir==N) return wall_v[x][y]; if(dir==W) return wall_h[y][x]; if(dir==S) return wall_v[x][y]; } bool isForwardWall(int x,int y,int dir){ if(dir==E) y--; if(dir==N) x--; if(y<0 || 4<y || x<0 || 4<x) return false; if(dir==E) return wall_v[x][y]; if(dir==N) return wall_h[y][x]; if(dir==W) return wall_v[x][y]; if(dir==S) return wall_h[y][x]; } bool isBackwardWall(int x,int y,int dir){ if(dir==W) y--; if(dir==S) x--; if(y<0 || 4<y || x<0 || 4<x) return false; if(dir==E) return wall_v[x][y]; if(dir==N) return wall_h[y][x]; if(dir==W) return wall_v[x][y]; if(dir==S) return wall_h[y][x]; } void goRight(int &x,int &y,int &dir){ if(dir==E) y++; if(dir==N) x++; if(dir==W) y--; if(dir==S) x--; dir=(dir+3)%4; } void goForward(int &x,int &y,int &dir){ if(dir==E) x++; if(dir==N) y--; if(dir==W) x--; if(dir==S) y++; } void goLeft(int &x,int &y,int &dir){ if(dir==E) y--; if(dir==N) x--; if(dir==W) y++; if(dir==S) x++; dir=(dir+1)%4; } void goBackward(int &x,int &y,int &dir){ if(dir==E) x--; if(dir==N) y++; if(dir==W) x++; if(dir==S) y--; dir=(dir+2)%4; } int main() { for(int i=0;i<9;i++){ char s[8]; cin>>s; if(i%2==0){ for(int j=0;j<4;j++) wall_h[i/2][j]=s[j]-'0'; } else{ for(int j=0;j<5;j++) wall_v[j][i/2]=s[j]-'0'; } } int x=0,y=0,dir=0; do{ if(!isForwardWall(x,y,dir)){ if(!isRightWall(x,y,dir)){ if(!isBackwardWall(x,y,dir)) goBackward(x,y,dir); else goRight(x,y,dir); } else goForward(x,y,dir); } else goLeft(x,y,dir); switch(dir){ case E: cout<<'R'; break; case N: cout<<'U'; break; case W: cout<<'L'; break; case S: cout<<'D'; break; } }while(!(x==0&&y==0)); cout<<endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { string in[9]; int x = 0, y = 0, d = 0; //d..direction 0→、1↓、2←、3↑ for (int i = 0; i < 9; i++) cin >> in[i]; do { if (d == 0) { if (y > 0) { if (in[2 * y - 1][x] == '1') { cout << "U"; d = 3; y--; continue; } } if (x < in[0].size()) { if (in[2 * y][x] == '1') { cout << "R"; x++; continue; } } if (y < 4) { if (in[2 * y + 1][x] == '1') { cout << "D"; d = 1; y++; continue; } } cout << "L"; x--; d = 2; continue; } if (d == 1) { if (x < in[0].size()) { if (in[2 * y][x] == '1') { cout << "R"; d = 0; x++; continue; } } if (y < 4) { if (in[2 * y + 1][x] == '1') { cout << "D"; y++; continue; } } if (x > 0) { if (in[2 * y][x - 1] == '1') { cout << "L"; d = 2; x--; continue; } } cout << "U"; d = 3; y--; continue; } if (d == 2) { if (y < 4) { if (in[2 * y + 1][x] == '1') { cout << "D"; d = 1; y++; continue; } } if (x > 0) { if (in[2 * y][x - 1] == '1') { cout << "L"; x--; continue; } } if (y > 0) { if (in[2 * y - 1][x] == '1') { cout << "U"; d = 3; y--; continue; } } cout << "R"; d = 0; x++; continue; } if (d == 3) { if (x > 0) { if (in[2 * y][x - 1] == '1') { cout << "L"; d = 2; x--; continue; } } if (y > 0) { if (in[2 * y - 1][x] == '1') { cout << "U"; y--; continue; } } if (x < in[0].size()) { if (in[2 * y][x] == '1') { cout << "R"; d = 0; x++; continue; } } cout << "D"; d = 1; y++; continue; } }while (x != 0 || y != 0); cout << endl; return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #define RIGHT 1 #define LEFT -1 #define UP 2 #define DOWN -2 using namespace std; int main(){ int i,j,state,sy=0,sx=1,t_state; char row[5][4],col[4][5]; cin>>row[0]; for(i=0;i<4;i++){ cin>>col[i]; cin>>row[i+1]; } for(i=0;i<5;i++){ for(j=0;j<4;j++){ row[i][j]=row[i][j]-'0'; } } for(i=0;i<5;i++){ for(j=0;j<4;j++){ col[j][i]=col[j][i]-'0'; } } cout<<"R"<<flush; state=RIGHT; while(sy || sx){ switch(state){ case RIGHT: if(sy>0 && col[sy-1][sx]){ cout<<"U"<<flush; t_state=UP; sy--; }else if(sx<4 && row[sy][sx]){ cout<<"R"<<flush; t_state=RIGHT; sx++; }else if(sy<4 && col[sy][sx]){ cout<<"D"<<flush; t_state=DOWN; sy++; }else if(sx>0 && row[sy][sx-1]){ cout<<"L"<<flush; t_state=LEFT; sx--; } break; case LEFT: if(sy<4 && col[sy][sx]){ cout<<"D"<<flush; t_state=DOWN; sy++; }else if(sx>0 && row[sy][sx-1]){ cout<<"L"<<flush; t_state=LEFT; sx--; }else if(sy>0 && col[sy-1][sx]){ cout<<"U"<<flush; t_state=UP; sy--; }else if(sx<4 && row[sy][sx]){ cout<<"R"<<flush; t_state=RIGHT; sx++; } break; case UP: if(sx>0 && row[sy][sx-1]){ cout<<"L"<<flush; t_state=LEFT; sx--; }else if(sy>0 && col[sy-1][sx]){ cout<<"U"<<flush; t_state=UP; sy--; }else if(sx<4 && row[sy][sx]){ cout<<"R"<<flush; t_state=RIGHT; sx++; }else if(sy<4 && col[sy][sx]){ cout<<"D"<<flush; t_state=DOWN; sy++; } break; case DOWN: if(sx<4 && row[sy][sx]){ cout<<"R"<<flush; t_state=RIGHT; sx++; }else if(sy<4 && col[sy][sx]){ cout<<"D"<<flush; t_state=DOWN; sy++; }else if(sx>0 && row[sy][sx-1]){ cout<<"L"<<flush; t_state=LEFT; sx--; }else if(sy>0 && col[sy-1][sx]){ cout<<"U"<<flush; t_state=UP; sy--; } break; } state=t_state; } cout<<endl; return 0; }
#include <iostream> #include <vector> using namespace std; const int X = 1000; const int Y = 10; bool g[Y][X][Y][X]; struct State { int x; int y; int dir; }; int dirx[] = {1, 0, -1, 0}; int diry[] = {0, 1, 0, -1}; string ans = ""; vector<int> inputLine() { string s; vector<int> a; cin >> s; for(char c : s) { a.push_back(c - '0'); } return a; } string toString(int dir) { switch(dir) { case 0: return "R"; case 1: return "D"; case 2: return "L"; case 3: return "U"; } } struct State at(struct State st) { for(int i=0;i<4;i++) { int ndir = (st.dir + i) % 4; int nx = st.x + dirx[ndir]; int ny = st.y + diry[ndir]; if(0 <= nx && 0 <= ny && g[st.y][st.x][ny][nx]) { // printf("nx: %d, ny: %d, ndir: %d\n", nx, ny, ndir); ans += toString(ndir); int nndir = (ndir+3)%4; int nnx = nx + dirx[nndir]; int nny = ny + diry[nndir]; if(0 <= nnx && 0 <= nny && g[ny][nx][nny][nnx]) { // print("dirx: %d, diry: %d\n", dirx[ndir // printf("nnx: %d, nny: %d, nndir: %d\n", nnx, nny, nndir); return (State){nx, ny, nndir}; }else { return (State){nx, ny, ndir}; } } } } int main() { vector<vector<int>> a; for(int i=0;i<9;i++) { a.push_back(inputLine()); } for(int i=0;i<Y;i++) { for(int j=0;j<X;j++) { for(int k=0;i<Y;i++) { for(int l=0;j<X;j++) { g[i][j][k][l] = false; } } } } for(int i=0;i<4;i++) { for(int j=0;j<a[0].size();j++) { if(a[2*i][j] == 1) { g[i][j][i][j+1] = true; g[i][j+1][i][j] = true; } } for(int j=0;j<a[1].size();j++) { if(a[2*i+1][j] == 1) { g[i][j][i+1][j] = true; g[i+1][j][i][j] = true; } } } for(int j=0;j<a[0].size();j++) { if(a[8][j] == 1) { g[4][j][4][j+1] = true; g[4][j+1][4][j] = true; } } auto st = at((State){0, 0, 0}); while(st.x != 0 || st.y != 0) { // printf("x: %d, y: %d, dir: %d\n", st.x, st.y, st.dir); st = at(st); // cout << st.dir << endl; } // printf("x: %d, y: %d, dir: %d\n", st.x, st.y, st.dir); cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) #define repb(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl //#define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; string s = "URDL"; int dx[4] = { 0, 1, 0, -1}; int dy[4] = {-1, 0, 1, 0}; signed main() { string ans = ""; int f[5][5][4] = {0}; string wx[5]; string wy[4]; rep(i, 0, 9){ if(i % 2 == 0){ cin >> wx[i / 2]; }else{ cin >> wy[i / 2]; } } f[0][0][0] = 1; int y = 0, x = 0, dir = 0; while(1){ rep(i, 0, 4){ int tmp = (dir + i) % 4; int ny = y + dy[tmp]; int nx = x + dx[tmp]; if(!(0 <= ny && ny <= 4 && 0 <= nx && nx <= 4)) continue; if(tmp == 0 && wy[y - 1][x] == '0') continue; if(tmp == 1 && wx[y][x] == '0') continue; if(tmp == 2 && wy[y][x] == '0') continue; if(tmp == 3 && wx[y][x - 1] == '0') continue; if(f[ny][nx][(dir + i + 3) % 4] == true) continue; f[ny][nx][(dir + i + 3) % 4] = true; y = ny; x = nx; dir = (dir + i + 3) % 4; ans += s[tmp]; //o(y<<" "<<x<<" "<<dir<<" "<<ny<<" "<<nx<<" "<<tmp<<" "<<s[tmp]); break; } if(f[0][0][2] || f[0][0][3]) break; } cout << ans << endl; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int xdir[4] = {0, 1, 0, -1}, ydir[4] = {-1, 0, 1, 0}; char dir[4] = {'U', 'R', 'D', 'L'}; int main() { int l, x, y, d; string str; bool a[11][11][11][11]; while(cin>>str){ l = str.size()+1; y = 1; fill(&a[0][0][0][0], &a[l+1][0][0][0], false); for(int i = 1; i < l; ++i) if(str[i-1] == '1') a[0][i-1][0][i] = a[0][i][0][i-1] = true; for(int i = 1; i <= 8; ++i){ cin >> str; if(i%2){ for(int j = 0; j < l; ++j) if(str[j] == '1') a[y-1][j][y][j] = a[y][j][y-1][j] = true; } else { for(int j = 1; j < l; ++j) if(str[j-1] == '1') a[y][j-1][y][j] = a[y][j][y][j-1] = true; y++; } } x = y = d = 0; do { for(int i = 3; i < 7; ++i){ int nd = (d+i)%4; int dx = x+xdir[nd], dy = y+ydir[nd]; if(0 <= dx && dx < l && 0 <= dy && dy < l && a[y][x][dy][dx]){ cout << dir[(d+i)%4]; a[y][x][dy][dx] = false; x = dx; y = dy; d = nd; break; } } } while(x+y != 0); cout << endl; } return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int xdir[4] = {0, 1, 0, -1}, ydir[4] = {-1, 0, 1, 0}; char dir[4] = {'U', 'R', 'D', 'L'}; int main() { int l, x, y, d; string str; bool a[11][11][11][11]; // current pos[h][w], next pos[h][w] while(cin>>str){ l = str.size()+1; y = 1; fill(&a[0][0][0][0], &a[l+1][0][0][0], false); for(int i = 1; i < l; ++i) if(str[i-1] == '1') a[0][i-1][0][i] = a[0][i][0][i-1] = true; for(int i = 1; i <= 8; ++i){ cin >> str; if(i%2){ for(int j = 0; j < l; ++j) if(str[j] == '1') a[y-1][j][y][j] = a[y][j][y-1][j] = true; } else { for(int j = 1; j < l; ++j) if(str[j-1] == '1') a[y][j-1][y][j] = a[y][j][y][j-1] = true; y++; } } x = y = d = 0; do { for(int i = 3; i < 7; ++i){ int dx = x+xdir[(d+i)%4], dy = y+ydir[(d+i)%4]; if(0 <= dx && dx < l && 0 <= dy && dy < l && a[y][x][dy][dx]){ cout << dir[(d+i)%4]; // cout<<": ("<<x<<", "<<y<<") -> ("<<dx<<", "<<dy<<")"<<endl; a[y][x][dy][dx] = false; x = dx; y = dy; d = (d+i)%4; break; } } } while(x+y != 0); cout << endl; } return 0; }
#include <iostream> #include <vector> #include <string> #include <utility> using namespace std; typedef pair<int, int> Point; int pack(int x, int y) { return x*5+y; } Point unpack(int p) { return make_pair(p/5, p%5); } bool can(const vector<vector<int> > &v, int x1, int y1, int x2, int y2) { if(x1 < 0 || y1 < 0 || x1 >= 5 || y1 >= 5) return false; if(x2 < 0 || y2 < 0 || x2 >= 5 || y2 >= 5) return false; return v[pack(x1,y1)][pack(x2,y2)]; } int main() { vector<vector<int> > v(30, vector<int>(30, 0)); for(int i = 0; i < 9; ++i) { string str; cin >> str; for(int j = 0; j < str.size(); ++j) { if(i % 2 == 0) v[pack(j, i/2)][pack(j+1, i/2)] = v[pack(j+1,i/2)][pack(j,i/2)] = str[j]-'0'; else v[pack(j, i/2)][pack(j, i/2+1)] = v[pack(j,i/2+1)][pack(j,i/2)] = str[j]-'0'; } } const char *dirchar = "RDLU"; const int delta[][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; int x = 0, y = 0; int dir = 0; while(true) { cout << dirchar[dir]; x += delta[dir][0]; y += delta[dir][1]; if(x == 0 && y == 0) break; int left = (dir+3)%4; int right = (dir+1)%4; if(can(v, x,y, x+delta[left][0],y+delta[left][1])) { dir = left; } else if(can(v, x,y, x+delta[dir][0],y+delta[dir][1])) { dir = dir; } else if(can(v, x,y, x+delta[right][0],y+delta[right][1])) { dir = right; } else dir = (dir+2)%4; } cout << endl; return 0; }
#include <iostream> #include <cstdio> #include <string> using namespace std; #define rep2(x,from,to) for(int x = (from); x < (to); ++(x)) #define rep(x,to) rep2(x,0,to) int r[7][6]; int c[6][7]; void turn() { int nr[7][6], nc[6][7]; rep(i,6) { rep(j,7) { nc[i][j] = r[6-j][i]; } } rep(i,7) { rep(j,6) { nr[i][j] = c[5-j][i]; } } rep(i,7) { rep(j,6) { r[i][j] = nr[i][j]; } } rep(i,6) { rep(j,7) { c[i][j] = nc[i][j]; } } } int main() { rep(i,7) { rep(j,6) { r[i][j] = 0; } } rep(i,6) { rep(j,7) { c[i][j] = 0; } } string s; rep2(i,1,5) { cin >> s; rep2(j,1,5) { r[i][j] = s[j-1] - '0'; } cin >> s; rep2(j,1,6) { c[i][j] = s[j-1] - '0'; } } cin >> s; rep2(i,1,5) { r[5][i] = s[i-1] - '0'; } int x = 0, y = 0; int gx = 0, gy = 0; char d[4] = {'R', 'U', 'L', 'D'}; int dir = 0; while(1) { if(!c[y][x+1] && !c[y+1][x+1] && !r[y+1][x+1]) { // turn 180 dir = (dir+2) % 4; cout << d[dir]; rep(i,2) turn(); x = 4 - x; y = 4 - y; gx = 4 - gx; gy = 4 - gy; x++; if(x == gx && y == gy) break; continue; } if(c[y][x+1] == 1) { // turn left dir = (dir+1) % 4; cout << d[dir]; turn(); swap(y = 4 - y, x); swap(gy = 4 - gy, gx); x++; if(x == gx && y == gy) break; continue; } if(r[y+1][x+1] == 1) { // go straight cout << d[dir]; x++; if(x == gx && y == gy) break; continue; } if(c[y+1][x+1] == 1) { // turn right dir = (dir+3) % 4; cout << d[dir]; rep(i,3) turn(); swap(x = 4 - x, y); swap(gx = 4 - gx, gy); x++; if(x == gx && y == gy) break; } } cout << endl; return 0; }
#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) { for (int j=1+i%2; j<=9; j+=2) { cin >> c; g[i][j] |= 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<iostream> #include<cstdio> using namespace std; int main(){ int field[6][6] = {0},a,next_x = 1,next_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[next_x][next_y]&2){ cout <<"U"; if(field[next_x][next_y]&1){t = 8;} else{next_y--;} } else{next_x++;t = 2;} } else if(t == 2){ if(field[next_x][next_y]&4){ cout <<"R"; if(field[next_x][next_y]&2){t = 1;} else{next_x++;} } else{next_y++;t = 4;} } else if(t == 4){ if(field[next_x][next_y]&8){ cout <<"D"; if(field[next_x][next_y]&4){t = 2;} else{next_y++;} } else{next_x--;t = 8;} } else if(t == 8){ if(field[next_x][next_y]&1){ cout <<"L"; if(field[next_x][next_y]&8){t = 4;} else{next_x--;} } else{next_y--;t = 1;} } if(!next_x && !next_y){break;} } cout <<endl; return 0; }
#include <iostream> using namespace std; char const * direct_text[] = {"R", "D", "L", "U"}; bool can_move(int a[10][6], int x, int y, int d) { if ( (d == 0 && a[2*x-1][y ] == 1) || (d == 1 && a[2*x ][y ] == 1) || (d == 2 && a[2*x-1][y-1] == 1) || (d == 3 && a[2*x-2][y ] == 1) ) return 1; else return 0; } int main(void) { int a[10][6] = {{}}; char c; for (int i = 1; i <= 9; ++i) for (int j = 1; j <= 5; ++j) if (j <= 4 || i%2==0) { cin >> c; a[i][j] = c - '0'; } int x = 1, y = 1, d = 0; while (true) { for (int i = 0; i < 4; ++i) { if (can_move(a, x, y, (d+3+i)%4)) { d = (d+3+i)%4; if (d == 0) y += 1; else if (d == 1) x += 1; else if (d == 2) y -= 1; else x -= 1; cout << direct_text[d]; break; } } if (x == 1 && y == 1) break; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; const char ch[] = {"ULDR"}; bool g[10][10][10][10]; int main() { char c; for (int i = 1; i <= 5; i++){ for (int j = 1; j <= 4; j++){ scanf(" %c", &c); if (c == '0') continue; g[i][j][i][j + 1] = true; g[i][j + 1][i][j] = true; } if (i == 5) continue; for (int j = 1; j <= 5; j++){ scanf(" %c", &c); if (c == '0') continue; g[i][j][i + 1][j] = true; g[i + 1][j][i][j] = true; } } int dir = 3; int x = 1, y = 2; while (true){ printf("%c", ch[dir]); if (x == 1 && y == 1) break; for (int t = 1; t >= -2; t--){ int nd = (dir + t + 4) % 4; int nx = x + dx[nd]; int ny = y + dy[nd]; if (g[x][y][nx][ny]){ x = nx; y = ny; dir = nd; break; } } } puts(""); 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);}///////05-2018,@siikya
#include<bits/stdc++.h> #define for0(i, n) for(int i = 0; i < (n); i++) using namespace std; string s[10]; int x, y, d = 1; bool fU() { if (y == 0)return 0; else return s[y * 2 - 1][x] == '1'; } void mU() { cout << "U"; y--; return; } bool fD() { if (y == 4)return 0; else return s[y * 2 + 1][x] == '1'; } void mD() { cout << "D"; y++; return; } bool fL() { if (x == 0)return 0; else return s[y * 2][x - 1] == '1'; } void mL() { cout << "L"; x--; return; } bool fR() { if (x == 4)return 0; else return s[y * 2][x] == '1'; } void mR() { cout << "R"; x++; return; } signed main() { for0(i, 9)cin >> s[i]; mR(); while (x + y > 0) { for0(i, 4) { if ((i + 3 + d) % 4 == 0) { if (fU()) { mU(); d = 0; break; } } else if ((i + 3 + d) % 4 == 1) { if (fR()) { mR(); d = 1; break; } } else if ((i + 3 + d) % 4 == 2) { if (fD()) { mD(); d = 2; break; } } else if ((i + 3 + d) % 4 == 3) { if (fL()) { mL(); d = 3; break; } } } } printf("\n"); }
#include<iostream> using namespace std; int di[4] = {0, 1, 0, -1}; int dj[4] = {1, 0, -1, 0}; string s = "RDLU"; #define inRange(x,a,b) (a <= x && x < b) int main(){ char mat[9][5]; for(int i = 0; i < 9; i++){ for(int j = 0; j < (i%2 == 0 ? 4 : 5); j++){ cin >> mat[i][j]; } } int i = 0, j = 1, dir = 0; string ans = "R"; while(1){ if(i == 0 && j == 0) break; if(dir % 2 == 0){ int ti = 2*i + (dir-1); if(inRange(ti, 0, 9)){ if(mat[ti][j] == '1'){ dir = (dir - 1 + 4)%4; i += di[dir], j += dj[dir]; ans += s[dir]; continue; } } int tj = j - dir/2; if(!inRange(tj, 0, 4) || mat[2*i][tj] == '0'){ dir = (dir + 1)%4; }else{ i += di[dir], j += dj[dir]; ans += s[dir]; } }else{ int tj = j - dir/2; if(inRange(tj, 0, 4)){ if(mat[2*i][tj] == '1'){ dir = (dir - 1 + 4)%4; i += di[dir], j += dj[dir]; ans += s[dir]; continue; } } int ti = 2*i + (2-dir); if(!inRange(ti, 0, 9) || mat[ti][j] == '0'){ dir = (dir + 1)%4; }else{ i += di[dir], j += dj[dir]; ans += s[dir]; } } } cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <utility> #include <string> #include <sstream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <cctype> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #define rep(i, n) for(int i = 0; i < (n); i++) #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<int> vi; string sdir = "URDL"; int dx[] = {0, 0, 0, -1}; int dy[] = {-1, 0, 1, 0}; int main(){ vector<string> v(9); rep(i, 9){ cin >> v[i]; v[i] += '0'; } int x = 0, y = 0; int dir = 1; do{ dir += 3; rep(i, 4){ int ndir = (dir+i)%4; int nx = x + dx[ndir]; int ny = y*2 + dy[ndir]; if(nx < 0 || nx >= 5 ||ny < 0 || ny >= 9) continue; if(v[ny][nx] == '0') continue; x += dx[ndir]+(ndir==1); y += dy[ndir]; dir = ndir; cout << sdir[ndir]; break; } }while(x!=0||y!=0); cout << endl; }
#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() #define pb emplace_back #define INF (1e9+1) 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> #include <cstdio> #include <algorithm> using namespace std; char d[10][6]; void solve(int y,int x,int way){ if(y==0&&x==0&&way==3){ cout<<"L"; return; } if(y==1&&x==0&&way==2){ cout<<"U"; return; } if(way==1){ cout<<"R"; if(y>0&&d[y-1][x+1]=='1')solve(y-1,x+1,2); else if(x<5&&d[y][x+1]=='1')solve(y,x+1,1); else if(y<8&&d[y+1][x+1]=='1')solve(y+1,x+1,4); else if(x>0&&d[y][x]=='1')solve(y,x,3); } else if(way==2){ cout<<"U"; if(x>0&&d[y-1][x-1]=='1')solve(y-1,x-1,3); else if(y>1&&d[y-2][x]=='1')solve(y-2,x,2); else if(x<4&&d[y-1][x]=='1')solve(y-1,x,1); else if(y<7&&d[y][x]=='1')solve(y,x,4); } else if(way==3){ cout<<"L"; if(y<8&&d[y+1][x]=='1')solve(y+1,x,4); else if(x>0&&d[y][x-1]=='1')solve(y,x-1,3); else if(y>0&&d[y-1][x]=='1')solve(y-1,x,2); else if(x<4&&d[y][x]=='1')solve(y,x,1); } else if(way==4){ cout<<"D"; if(x<4&&d[y+1][x]=='1')solve(y+1,x,1); else if(y<7&&d[y+2][x]=='1')solve(y+2,x,4); else if(x>0&&d[y+1][x-1]=='1')solve(y+1,x-1,3); else if(y>1&&d[y][x]=='1')solve(y,x,2); } return; } int main(void){ for(int i=0;i<9;i++){ cin>>d[i]; } solve(0,0,1); cout<<endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <numeric> #include <stack> #include <queue> #include <map> #include <set> #include <utility> #include <sstream> #include <complex> using namespace std; #define FOR(i,a,b) for(long long i=(a);i<(b);i++) #define REP(i,N) for(long long i=0;i<(N);i++) #define ALL(s) (s).begin(),(s).end() #define fi first #define se second #define PI acos(-1.0) #define INF 1000000007 #define EPS 1e-10 #define MAX_M 100100 typedef long long ll; typedef pair<ll, ll> P; typedef pair<double, double> PD; typedef pair<string, ll> PS; typedef vector<ll> V; typedef pair<P, char> PC; int table[100][100]; string s; int dx[] = { 0, 1, 0, -1, 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0, -1, 0, 1, 0 }; char c[] = "URDLURDL"; int main(){ REP(i, 9){ cin >> s; REP(j, s.size()){ if (i % 2 == 0) table[i][2 * j + 1] = s[j] - '0'; else table[i][2 * j] = s[j] - '0'; } } bool f = 0; string ans = ""; int x = 0, y = 0, muki = 0; while (1){ if (f&&x == 0 && y == 0)break; int ny, nx; for (int i = muki; i < muki + 4; i++){ nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx <= 8 && ny >= 0 && ny <= 8){ if (table[ny][nx] == 1){ x += 2 * dx[i]; y += 2 * dy[i]; ans += c[i]; if (c[i] == 'U')muki = 3; else if (c[i] == 'R')muki = 0; else if (c[i] == 'D')muki = 1; else muki = 2; break; } } } f = 1; } cout << ans << endl; }
#include "bits/stdc++.h" #define REP(i,num) for(int i=0;i<(num);++i) #define LOOP(i) while(i--) #define ALL(c) c.begin(),c.end() #define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl; #define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;} using namespace std; using ll = long long; constexpr ll atcoder_mod = 1e9+7; template<typename T=int> T in(){T x; cin >> x; return (x);} template<typename T=int,typename C=vector<T>> C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);} void vout(){cout << endl;} template<typename Head,typename... Tail> void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);} void out(){cout << endl;} template<typename Head,typename... Tail> void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);} int main(){ cin.tie(0); ios::sync_with_stdio(false); vector<vector<int>> H(5,vector<int>(4)),V(4,vector<int>(5)); REP(i,9){ string S=in<string>(); if(!(i%2)){ REP(j,4) H[i/2][j] = S[j]-'0'; } else{ REP(j,5) V[i/2][j] = S[j]-'0'; } } enum{R=0,D,L,U}; int X=0,Y=0,dir=R; auto check = [&](){return (!X && !Y);}; string S; while(true){ if(dir==R){ if(Y>0 && V[Y-1][X]){ dir = U; Y--; S.push_back('U'); if(check()) break; } else if(X<4 && H[Y][X]){ X++; S.push_back('R'); } else if(Y<4 && V[Y][X]){ dir = D; Y++; S.push_back('D'); } else{ dir = L; X--; S.push_back('L'); if(check()) break; } } else if(dir==D){ if(X<4 && H[Y][X]){ dir = R; X++; S.push_back('R'); } else if(Y<4 && V[Y][X]){ Y++; S.push_back('D'); } else if(X>0 && H[Y][X-1]){ dir = L; X--; S.push_back('L'); if(check()) break; } else{ dir = U; Y--; S.push_back('U'); if(check()) break; } } else if(dir==L){ if(Y<4 && V[Y][X]){ dir = D; Y++; S.push_back('D'); } else if(X>0 && H[Y][X-1]){ X--; S.push_back('L'); if(check()) break; } else if(Y>0 && V[Y-1][X]){ dir = U; Y--; S.push_back('U'); if(check()) break; } else{ dir = R; X++; S.push_back('R'); } } else{ if(X>0 && H[Y][X-1]){ dir = L; X--; S.push_back('L'); if(check()) break; } else if(Y>0 && V[Y-1][X]){ Y--; S.push_back('U'); if(check()) break; } else if(X<4 && H[Y][X]){ dir = R; X++; S.push_back('R'); } else{ dir = D; Y++; S.push_back('D'); } } } out(S); return 0; }
#include<iostream> #include<string> using namespace std; int main(){ int field[6][6]={0}; string s; for(int i=0; i<5; i++){ cin >>s; for(int j=0; j<4; j++){ if(s[j] == '1'){ field[j+1][i]+=4; field[j+1][i+1]+=1; } } if(i<4){ cin >>s; for(int j=0; j<5; j++){ if(s[j] == '1'){ field[j][i+1]+=2; field[j+1][i+1]+=8; } } } } int px = 1,py=0,d=2; for(int i=0;; i++){ if(d == 1){ if(!(field[px][py]&2)){px++;d = 2;} else{ if(field[px][py]&1){cout <<"U";d = 8;} else{cout <<"U";py--;} } } else if(d == 2){ if(!(field[px][py]&4)){py++;d = 4;} else{ if(field[px][py]&2){cout <<"R";d = 1;} else{cout <<"R";px++;} } } else if(d == 4){ if(!(field[px][py]&8)){px--;d = 8;} else{ if(field[px][py]&4){cout <<"D";d = 2;} else if(field[px][py]&8){cout <<"D";py++;} } } else if(d == 8){ if(!(field[px][py]&1)){py--;d = 1;} else{ if(field[px][py]&8){cout <<"L";d = 4;} else{cout <<"L";px--;} } } if(px == 1 && py == 0){cout <<endl; break;} } return 0; }
#include<iostream> #include<string> #include<string.h> using namespace std; int main(){ char ma[11][11]; memset(ma,'0',sizeof(ma)); for(int i=1;i<10;i++){ if(i%2==1) for(int j=2;j<9;j+=2) cin>>ma[i][j]; else for(int j=1;j<10;j+=2) cin>>ma[i][j]; } int x=1,y=1,dir=0; int mv[4][2]={{2,0},{0,2},{-2,0},{0,-2}}; int ts[4][8]={{1,-1,1,0,0,-1,0,1},{1,1,0,1,1,0,-1,0},{-1,1,-1,0,0,1,0,-1},{-1,-1,0,-1,-1,0,1,0}}; string str[4]={"R","D","L","U"}; string path=""; int ch=0; for(int k=0;k<40;k++){ int wen=1; while(ma[y+ts[dir][3]][x+ts[dir][2]]=='1'){ y+=mv[dir][1]; x+=mv[dir][0]; wen=0; path+=str[dir]; wen++; // cout<<x<<" "<<y<<" "<<dir<<endl; if(ma[y+ts[dir][5]][x+ts[dir][4]]=='1'){ // cout<<"dir: "<<dir<<" "<<x<<" "<<y<<" "<<x+ts[dir][4]<<" "<<y+ts[dir][5]<<" "<<ma[y+ts[dir][5]][x+ts[dir][4]]<<endl; break; } } if(x==1&&y==1) break; // cout<<x<<" "<<y<<" "<<dir<<endl; if(ma[y+ts[dir][5]][x+ts[dir][4]]=='1')dir=(dir+3)%4; else do{ dir=(dir+1)%4; }while((y+ts[dir][3]>10||y+ts[dir][3]<0||x+ts[dir][2]>10||x+ts[dir][2]<0)||ma[y+ts[dir][1]][x+ts[dir][0]]=='1'); } cout<<path<<endl; } //RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL //RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL //RRRRDDDDLLLUUURRDDLURULLDDDRRRUUUULLLL
#include<iostream> #include<string> using namespace std; int main(){ long i,j,k; string s[9]; for(i=0;i<9;i++)cin>>s[i]; i=j=k=0; cout<<"R";j++; for(;;){ if(k==0&&i>0&&s[i-1][j]=='1'){cout<<"U";i-=2;k=3;} else if(k==0&& s[i][j] =='1'){cout<<"R";j++; k=0;} else if(k==0&&i<8&&s[i+1][j]=='1'){cout<<"D";i+=2;k=1;} else if(k==0) {cout<<"L";j--; k=2;} else if(k==1&& s[i][j] =='1'){cout<<"R";j++; k=0;} else if(k==1&&i<8&&s[i+1][j]=='1'){cout<<"D";i+=2;k=1;} else if(k==1&&j>0&&s[i][j-1]=='1'){cout<<"L";j--; k=2;} else if(k==1) {cout<<"U";i-=2;k=3;} else if(k==2&&i<8&&s[i+1][j]=='1'){cout<<"D";i+=2;k=1;} else if(k==2&&j>0&&s[i][j-1]=='1'){cout<<"L";j--; k=2;} else if(k==2&&i>0&&s[i-1][j]=='1'){cout<<"U";i-=2;k=3;} else if(k==2) {cout<<"R";j++; k=0;} else if(k==3&&j>0&&s[i][j-1]=='1'){cout<<"L";j--; k=2;} else if(k==3&&i>0&&s[i-1][j]=='1'){cout<<"U";i-=2;k=3;} else if(k==3&& s[i][j] =='1'){cout<<"R";j++; k=0;} else if(k==3) {cout<<"D";i+=2;k=1;} if(i==0&&j==0)break; } cout<<endl; return 0; }
#include <cstdio> #include <cctype> #include <utility> #include <array> using namespace std; #define gcu getchar_unlocked #define pcu putchar_unlocked #define _T template <typename T> #define _HT template <typename H, typename... T> #define _il inline #define _in _il int in #define _sc _il bool scan _T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu(); do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;} _in() {return in<int>(gcu());} _T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);} _sc(char &c){c=gcu();gcu();return c!=EOF;} #ifdef _GLIBCXX_STRING _sc(string &s){int c;s=""; for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}} #endif _HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);} #define _vo _il void out #define _vl _il void outl _vo(bool b) {pcu('0'+b);} _vo(const char *s){while(*s)pcu(*s++);} _vo(char c){pcu(c);} #ifdef _GLIBCXX_STRING _vo(string s){for(char c:s)pcu(c);} #endif _T _vo(T n){static char buf[20];char *p=buf; if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10; while (p!=buf)pcu(*--p);} _vl(){out('\n');} #ifdef _GLIBCXX_VECTOR _T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();} #endif _HT _vo(H&& h, T&&... t){out(h);out(move(t)...);} template <typename... T> _vl(T&&... t){out(move(t)...);outl();} struct P {int x, y;}; int main() { const int d[4][4] = {{0, 0, 1, 0}, {0, -1, 0, -2}, {-1, 0, -1, 0}, {0, 1, 0, 2}}; array<array<int, 6>, 11> m{}; for (int i = 1; i <= 9; i++) { for (int j = 1, e = 5 - i % 2; j <= e; j ++) m[i][j] = gcu() - '0'; gcu(); } P p = {1, 1}; int r = 0; do { for (r = (r + 1) % 4;; r = (r + 3) % 4) if (m[p.y + d[r][1]][p.x + d[r][0]]) { p.x += d[r][2], p.y += d[r][3]; break; } out("RULD"[r]); } while(!(p.x == 1 && p.y == 1)); outl(); }
#include <iostream> #include <vector> #include <string> using namespace std; vector< vector<int> > map(11, vector<int>(7)); int direction = 0; //R:0 L:1 U:2 D:3 int 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 j = 0; j < 4; ++j)map[i][j + 1] = char(a[j]) - '0'; } else { cin >> b; for (int j = 0; j < 5; ++j)map[i][j + 1] = char(b[j]) - '0'; } } } void Right() { if (map[y - 1][x])direction = 2, y -= 2, cout << 'U'; else if (map[y][x])direction = 0, x += 1, cout << 'R'; else if (map[y + 1][x])direction = 3, y += 2, cout << 'D'; else direction = 1, x -= 1, cout << 'L'; } void Left() { if (map[y + 1][x])direction = 3, y += 2, cout << 'D'; else if (map[y][x - 1])direction = 1, x -= 1, cout << 'L'; else if (map[y - 1][x])direction = 2, y -= 2, cout << 'U'; else direction = 0, x += 1, cout << 'R'; } void Up() { if (map[y][x - 1])direction = 1, x -= 1, cout << 'L'; else if (map[y - 1][x])direction = 2, y -= 2, cout << 'U'; else if (map[y][x])direction = 0, x += 1, cout << 'R'; else direction = 3, y += 2, cout << 'D'; } void Down() { if (map[y][x])direction = 0, x += 1, cout << 'R'; else if (map[y + 1][x])direction = 3, y += 2, cout << 'D'; else if (map[y][x - 1])direction = 1, x -= 1, cout << 'L'; else direction = 2, y -= 2, cout << 'U'; } void solve() { while (!(x == 1 && y == 1)) { switch (direction) { 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 << endl; return(0); }
#include<iostream> #include<vector> using namespace std; string ans=""; bool a[5][5][4] = {{{0}}}; int main(){ string str; for(int i=0;i<9;i++){ if(i%2==0){ cin>>str; for(int j=0;j<4;j++){ if(str[j]=='1'){ a[i/2][j][1] = true; a[i/2][j+1][3] = true; //cout<<"-("<<i/2<<" ,"<<j<<")("<<i/2<<" ,"<<j+1<<")"<<endl; } } } else{ cin>>str; for(int j=0;j<5;j++){ if(str[j]=='1'){ a[i/2][j][2] = true; a[i/2+1][j][0] = true; //cout<<"|("<<i/2<<" ,"<<j<<")("<<i/2+1<<" ,"<<j<<")"<<endl; } } } } cout<<"R"; int x=1,y=0,way=1; while(1){ if(x==0&&y==0)break; if(way == 0){ if(x!=0&&a[y][x][3]){ cout<<"L"; x--; way = 3; } else if(y!=0&&a[y][x][0]){ cout<<"U"; y--; way = 0; } else if(x!=4&&a[y][x][1]){ cout<<"R"; x++; way = 1; } else if(y!=4&&a[y][x][2]){ cout<<"D"; y++; way = 2; } } else if(way == 1){ if(y!=0&&a[y][x][0]){ cout<<"U"; y--; way = 0; } else if(x!=4&&a[y][x][1]){ cout<<"R"; x++; way = 1; } else if(y!=4&&a[y][x][2]){ cout<<"D"; y++; way = 2; } else if(x!=0&&a[y][x][3]){ cout<<"L"; x--; way = 3; } } else if(way == 2){ if(x!=4&&a[y][x][1]){ cout<<"R"; x++; way = 1; } else if(y!=4&&a[y][x][2]){ cout<<"D"; y++; way = 2; } else if(x!=0&&a[y][x][3]){ cout<<"L"; x--; way = 3; } else if(y!=0&&a[y][x][0]){ cout<<"U"; y--; way = 0; } } else if(way == 3){ if(y!=4&&a[y][x][2]){ cout<<"D"; y++; way = 2; } else if(x!=0&&a[y][x][3]){ cout<<"L"; x--; way = 3; } else if(y!=0&&a[y][x][0]){ cout<<"U"; y--; way = 0; } else if(x!=4&&a[y][x][1]){ cout<<"R"; x++; way = 1; } } } 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; //cout << "cur | (" << x << "," << y << ") = " << G[y][x] << endl; int hx = x + dx[(dir+1)%4],hy = y + dy[(dir+1)%4]; //cout << "hand | (" << hx << "," << hy <<") = " << G[hy][hx]<< endl; 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'))); //cout << endl; } } int main() { init(); makeGraph(); compute(); //print(); return 0; }
#include <iostream> using namespace std; const int dx[4]={1, 0, -1, 0}; const int dy[4]={0, 1, 0, -1}; struct pt{ int x, y, d; void move(int dir){ x+=dx[dir]; y+=dy[dir]; d=dir; } }; int main() { char buf[8]; bool h[5][4]; bool v[4][5]; for(int i=0; i<5; i++){ cin.getline(buf, 8); for(int j=0; j<4; j++) h[i][j]=(buf[j]=='1'?true:false); if(i==4) break; cin.getline(buf, 8); for(int j=0; j<5; j++) v[i][j]=(buf[j]=='1'?true:false); } pt pos={0,0,0}; do{ for(int dd=3; dd<=6; dd++){ int next=(pos.d+dd)%4; if(next==0){ if(pos.x!=4&&h[pos.y][pos.x]){ pos.move(0); cout<<'R'; break; } }else if(next==1){ if(pos.y!=4&&v[pos.y][pos.x]){ pos.move(1); cout<<'D'; break; } }else if(next==2){ if(pos.x!=0&&h[pos.y][pos.x-1]){ pos.move(2); cout<<'L'; break; } }else if(next==3){ if(pos.y!=0&&v[pos.y-1][pos.x]){ pos.move(3); cout<<'U'; break; } } } }while(pos.x!=0||pos.y!=0); cout<<'\n'; return 0; }
#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) return (x < 4 && ok(horz[y][x])); if (vx == -1) return (x > 0 && ok(horz[y][x-1])); if (vy == 1) return (y < 4 && ok(vert[y][x])); /* if (vy == -1) */ return (y > 0 && ok(vert[y-1][x])); } 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 <bits/stdc++.h> using namespace std; typedef struct { int x, y; bool root[4]; } Node; Node v[5][5]; const string ord = "URDL"; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; void init() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { v[i][j].x = i; v[i][j].y = j; for (int k = 0; k < 4; k++) v[i][j].root[k] = false; } } } void solve(int x, int y, char c) { cout << c; if (x == 0 && y == 0) { cout << '\n'; return; } int start = (ord.find(c) + 3) % 4; for (int i = 0; i < 4; i++) { int ni = (i + start) % 4; if (v[x][y].root[ni]) { solve(x + dx[ni], y + dy[ni], ord[ni]); return; } } } int main() { init(); for (int i = 0; i < 9; i++) { string input; cin >> input; for (int j = 0; j < input.size(); j++) { if (input[j] != '1') continue; if (i % 2) { v[(i + 1) / 2 - 1][j].root[2] = true; v[(i + 1) / 2][j].root[0] = true; } else { v[i / 2][j].root[1] = true; v[i / 2][j + 1].root[3] = true; } } } solve(0, 1, 'R'); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<cstdio> #include<climits> #include<cmath> #include<cstring> #include<string> #include<complex> #include<map> #define f first #define s second #define mp make_pair #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++) #define ALL(c) (c).begin(), (c).end() #define EPS (1e-10) using namespace std; typedef unsigned int uint; typedef long long ll; typedef complex<double> P; int kabe[5][5][5][5]; enum direct { RIGHT = 0, DOWN = 1, LEFT = 2, UP = 3 }; const char *dstr = "RDLU"; const int dx[4] = { 1, 0,-1, 0}; const int dy[4] = { 0, 1, 0,-1}; #define D(d) (((d)+4)%4) bool go(int x1, int y1, int x2, int y2){ if(x2 < 0 || y2 < 0 || x2 >= 5 || y2 >= 5) return false; return kabe[x1][y1][x2][y2] == 1; } int main(){ string str; REP(i,9){ cin>>str; if(i%2 == 0){ REP(j,4){ int x = j; int y = i/2; if(str[j] == '1'){ kabe[x][y][x+1][y] = kabe[x+1][y][x][y] = 1; } } }else{ REP(j,5){ int x = j; int y = i/2; if(str[j] == '1'){ kabe[x][y][x][y+1] = kabe[x][y+1][x][y] = 1; } } } } int x = 0, y = 0; int d = RIGHT; while(x != 0 || y != 0 || d == RIGHT){ d = D(d-1); while(!go(x,y,x+dx[d],y+dy[d])) d = D(d+1); putchar(dstr[d]); x += dx[d]; y += dy[d]; } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef complex<double> P; typedef pair<int,int> pii; #define REP(i,n) for(ll i=0;i<n;++i) #define REPR(i,n) for(ll i=1;i<n;++i) #define FOR(i,a,b) for(ll i=a;i<b;++i) #define DEBUG(x) cout<<#x<<": "<<x<<endl #define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl #define ALL(a) (a).begin(),(a).end() #define MOD (ll)(1e9+7) #define ADD(a,b) a=((a)+(b))%MOD #define FIX(a) ((a)%MOD+MOD)%MOD int points[5][5]; int dir[4] = {1,2,4,8}; char dirc[4] = {'R','D','L','U'}; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; // [0,1,2,3] : [right,bottom,left,top] int main(){ REP(i,9){ int x; cin>>x; if(i%2==0){ // yokonaga REP(_j,4){ int j = 3-_j; int val = x%10; x/=10; if(val==1){ points[j][i/2] |= dir[0]; points[j+1][i/2] |= dir[2]; } } }else{ // tatenaga REP(_j,5){ int j = 4-_j; int val = x%10; x/=10; if(val==1){ points[j][i/2] |= dir[1]; points[j][i/2+1] |= dir[3]; } } } } // input end int x=0, y=0, d=0; string res = ""; while(!(x==0 && y==0) || res.size()==0){ int kabe = (d+3)%4; if(points[x][y] & dir[kabe]) d = kabe; while(!(points[x][y] & dir[d])) d = (d+1)%4; res += dirc[d]; x += dx[d]; y += dy[d]; } cout<<res<<endl; return 0; }
#include <cstdio> #include <unistd.h> #define U 0 #define R 1 #define D 2 #define L 3 int dx[] = {0,1,0,-1}; int dy[] = {-1,0,1,0}; using namespace std; int wall[5][5]; char buf[128]; void readH(int y){ // read(0,buf,6);buf[4]=0; scanf("%s",buf); for (int i=0; i<4; i++){ wall[i][y] |= (buf[i]=='1') << R; wall[i+1][y] |= (buf[i]=='1') << L; } } void readV(int y){ scanf("%s",buf); for (int i=0; i<5; i++){ wall[i][y] |= (buf[i]=='1') << D; wall[i][y+1] |= (buf[i]=='1') << U; } } int main(void){ int x,y,dir; for (int i=0; i<4; i++){ readH(i); readV(i); } readH(4); /* for (int y=0; y<=4; y++){ for (int x=0; x<=4; x++){ printf("%x ", wall[x][y]); }puts(""); } */ // for(x=y=(dir=R)^R;x+=dx[dir],y+=dy[dir],write(1,&(*buf="URDL"[dir]),1),x+y;){ for(x=y=(dir=R)^R;x+=dx[dir],y+=dy[dir],putchar("URDL"[dir]),x+y;){ for((dir+=3)&=~4;!(wall[x][y]&(1<<dir)); (dir+=1)&=~4){ } } puts(""); return 0; }
#include <iostream> #include <vector> using namespace std; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; char go[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 += go[dir]; dir = (dir+3)%4; }while(!(x==0 && y==0 && dir==0)); cout << ans << endl; return 0; }
#include<iostream> #include<string> #include<algorithm> #include<vector> using namespace std; #define rep(i,n) for(int i=0; i<n; i++) void split( vector<int>& vi, string in ) { int i=0; while( i < in.length() ) { int x = 0; while( i < in.length() && in[i]!=' ' ) { x *= 10; x += in[i]-'0'; i++; } vi.push_back(x); i++; } } int main(){ string in; while( getline(cin, in), !(in.length()==1 && in[0]=='0') ) { vector<int> vi; split( vi, in ); int score=0, one=0; rep(i, vi.size()) { if( vi[i] == 1 ) { one++; score += 1; } else { score += min(10, vi[i]); } } while( one && score+10<=21 ) { score += 10; one--; } cout << (score<=21 ? score : 0) << endl; } return 0; }
#include <iostream> using namespace std; int main() { string str; while (1){ getline(cin, str); if (str[0] == '0') break; int sum = 0; int cnt = 0; for (int i = 0; i < str.size(); i++){ if (str[i] == '1' && '0' <= str[i + 1] && str[i + 1] <= '3'){ sum += 10; i++; continue; } if (str[i] == '1') cnt++; if (str[i] != ' ') sum += (str[i] - '0'); } while(cnt--){ if (sum + 10 <= 21){ sum += 10; } } if (sum > 21){ sum = 0; } cout << sum << endl; } }
#include <bits/stdc++.h> using namespace std; const double eps=1e-9; typedef vector<int> vi; typedef long long ll; int A[5],B[5]; string rule[5][5]; #define pb push_back string s; int main(void) { while(getline(cin,s) && s!="0") { int lowerten=0; int one=0; int score=0; int lastscore=0; string num=""; //cout << int(s.size()) << endl; for(int i=0;i<int(s.size());i++) { if(s[i]!=' ') { num+=s[i]; } if(s[i]==' ' || i==int(s.size()-1)) { int getnum=stoi(num); if(getnum>=10) score+=10; else if(getnum==1) one++; else score+=getnum; num=""; } } for(int i=0;i<=one;i++) { int addscore=11*i+1*(one-i); if(score+addscore>21) { continue; } else { lastscore=max(lastscore,score+addscore); } } if(score>21) { cout << 0 << endl; } else { cout << lastscore << endl; } } return 0; }
#include <iostream> #include <deque> #include <string> #include <sstream> using namespace std; int main(void){ string str; while(getline(cin,str)){ deque<int> q; int tmp; istringstream iss(str); iss >> tmp; if(tmp == 0) break; if(tmp == 1){ q.push_back(1); q.push_back(11); }else if(tmp >= 10){ q.push_back(10); }else{ q.push_back(tmp); } while(iss >> tmp){ int times = q.size(); for(int i=0;i<times;i++){ if(tmp == 1){ if(q.front() + 1 <= 21){ q.push_back(q.front() + 1); } if(q.front() + 11 <= 21){ q.push_back(q.front() + 11); } }else if(tmp >= 10){ if(q.front() + 10 <= 21){ q.push_back(q.front() + 10); } }else{ if(q.front() + tmp <= 21){ q.push_back(q.front() + tmp); } } q.pop_front(); } } int ret=0; while(!q.empty()){ if(q.front() >= 22){ //do nothing }else if(q.front() > ret){ ret = q.front(); } q.pop_front(); } cout << ret << endl; } return 0; }
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int t[10000],size,sum; char ch; int main(){ while(cin>>t[0]&&t[0]!=0){ size=1; while(1){ scanf("%c",&ch); if(ch=='\n')break; cin>>t[size++]; } sort(t,t+size); sum=0; for(int i=size-1;i>=0;i--){ if(t[i]!=1)sum+=min(t[i],10); else{ if(sum+11+i<=21)sum+=11; else sum++; } } cout<<(sum<=21?sum:0)<<endl; } return 0; }
#include<cstdio> #include<vector> using namespace std; int main() { for(int a;scanf("%d",&a),a;){ vector<int> card; card.push_back(a); while(getchar()!='\n'){ int b; scanf("%d",&b); card.push_back(b); } int cnt1=0,total=0; for(int i=0;i<card.size();i++){ if(card[i]==1) cnt1++; total+=card[i]>10?10:card[i]; } for(int i=0;i<cnt1;i++,total+=10) if(total+10>21) break; printf("%d\n",total>21?0:total); } return 0; }
#include<stdio.h> int main(void){ int sum=0,card,one=0,i; char code; while(0==0){ scanf("%d%c",&card,&code); if(card>10) card=10; else if(card==1) one++; sum+=card; if(code=='\n'){ if(sum==0) break; else if(sum>21) sum=0; else if(one>0){ for(i=0;i<one;i++) if(sum<=11) sum+=10; } printf("%d\n",sum); sum=0; one=0; } } return 0; }
#include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string s; while(getline(cin,s)){ if(s=="0") break; stringstream ss; vector<int> a; int n,f=0,sum=0; ss<<s; while(ss>>n){ if(n==1)f=1; a.push_back((n>=10)?10:n); if(ss.eof()) break; } for(int i=0;i<a.size();++i) sum+=a[i]; if(f&&sum<=11) sum+=10; if(sum>21)cout<<0<<endl; else cout<<sum<<endl; } return 0; }
#include<iostream> #include<sstream> using namespace std; int main(){ string s; while(getline(cin,s)){ if(s == "0") break; int sum = 0; int one = 0; stringstream ss(s); int c; while(ss >> c){ if(c == 1) one++; if(c > 10) c = 10; sum += c; } if(sum > 21){ sum = 0; }else if(one > 0){ while(one > 0 && sum+10 <= 21){ sum += 10; one--; } } cout << sum << endl; } return 0; }
#include<iostream> #include<vector> #include<cstdio> #include<functional> #include<algorithm> using namespace std; int main(){ while(true){ int t; int one_num = 0; int ans = 0; vector< int > cards; scanf("%d", &t); if(t==0) break; cards.push_back( t ); while( true ){ char c; scanf("%c", &c); if( c == '\n' ) break; else{ scanf("%d", &t); cards.push_back( t ); } } sort( cards.begin(), cards.end(), greater<int>() ); for(unsigned int i = 0; i < cards.size(); ++i){ one_num += cards[i] == 1 ? 1 : 0; } for(unsigned int i = 0; i < cards.size(); ++i){ cards[i] = cards[i] > 10 ? 10 : cards[i]; if( cards[i] == 1 ){ int tt = one_num * 11; while( ans + tt > 21 ){ tt -= 10; } if( tt < one_num ) ans = 22; else ans = ans + tt; break; }else{ ans += cards[i]; } } printf("%d\n", ans > 21 ? 0 : ans); } return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <memory> #include <string> #include <algorithm> #include <complex> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #include <bitset> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype #define strtok_r strtok_s #endif #define ITER(c) __typeof__((c).begin()) #define FOREACH(it, c) for (ITER(c) it=(c).begin(); it != (c).end(); ++it) #define RITER(c) __typeof__((c).rbegin()) #define RFOREACH(it, c) for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it) #define REP(i, n) REPEAT(i, 0, n) #define RREP(i, n) RREPEAT(i, 0, n) #define REPEAT(i, k, n) for(int i = (k); i < ((k)+(n)); ++i) #define RREPEAT(i, k, n) for(int i = (k)+(n)-1; i >= (k); --i) #define FROMTO(i,f,t) for(int i = f; i < t; i++) #define ALL(c) (c).begin(), (c).end() #define LLPOW(p1,p2) ((ll)pow((double)(p1), (int)p2)) #define ESIZEOF(A) (sizeof(A)/sizeof((A)[0])) #define CIN_NO_SYNC do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0) #define GETSTR(p) fgets((p), sizeof(p), stdin) #define CHOMP(p) do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0) #define FILL(m,v) memset(m, v, sizeof(m)) #define mp make_pair #define pb push_back template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} template <class _Tv> inline string join(const _Tv &v,string sep=" "){ ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str(); } typedef long long ll; typedef unsigned long long ull; typedef double D; typedef complex<D> P; typedef vector<int> VI; typedef vector<VI> VVI; #define X real() #define Y imag() #define EPS (1e-9) #define DEQ(p1,p2) (abs((p1)-(p2)) < EPS) #ifdef _DEBUG template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){ ITER(_c) _it=_c.begin(); if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<" "<<*_it<<endl;cout<<"]"<<endl; } else{cout<<_n<<" = [ "<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;cout<<" ]"<<endl; } } ostream &operator<<(ostream &os, const pair<int,int> &p){return(os<<"("<<p.first<<","<<p.second<<")");} #define pf printf #define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0) #define prfe(n) _prfe(#n, n) #define prfep(n) _prfe(#n, n, true) #define dbgchk(n) do{if(n)throw;}while(0) #else #define pf(...) /* */ #define pr(...) /* */ #define prfe(...) /* */ #define prfep(...) /* */ #define dbgchk(...) /* */ #endif int solve(VI v){ int t = 0, ac = 0, max = 0; FOREACH(it, v){ if(*it > 10) *it = 10; if(*it == 1) ac++; t += *it; } max = MAX(max, (t>21?0:t)); REP(i,ac){ t += 10; max = MAX(max, (t>21?0:t)); } return max; } int main(){ string s; while(getline(cin,s)){ stringstream ss(s); int n; ss >> n; if(n == 0) break; VI v; v.pb(n); while(ss >> n) v.pb(n); cout << solve(v) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int lld; int main(){ string str; while(getline(cin,str) , str != "0"){ int sum = 0; int cnt = 0; for(int i=0;i<str.size();i++){ if(str[i] == ' '){ } else if(i+1 != str.size() && str[i+1] != ' '){ sum += (str[i]-'0')*10; i++; } else { if(str[i] == '1') { cnt++; sum += 10; } sum += str[i]-'0'; } } while(sum > 21 && cnt != 0){ cnt--; sum -= 10; } if(sum > 21) cout << "0" << endl; else cout << sum << endl; } }
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<sstream> #include<string> #include<vector> #include<queue> #include<set> #include<map> #include<utility> #include<numeric> #include<algorithm> #include<bitset> #include<complex> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int,int> pint; #define mp make_pair template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template<class T> void chmin(T &t, T f) { if (t > f) t = f; } template<class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } int main() { int n; char c; int s=0,o=0; while(scanf("%d%c",&n,&c),n){ if(n==1)o++; if(n>=10)s+=10; else s+=n; if(c!=32){ if(s>21)cout<<0<<endl; else{ if(o&&s<=11)cout<<s+10<<endl; else cout<<s<<endl; } s=o=0; } } return 0; }
#include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> //#include <utility> #include <set> #include <iostream> //#include <memory> #include <string> #include <vector> #include <algorithm> //#include <functional> #include <sstream> //#include <deque> #include <complex> #include <stack> #include <queue> #include <cstdio> //#include <cctype> #include <cstring> //#include <ctime> #include <iterator> #include <bitset> //#include <numeric> //#include <list> //#include <iomanip> #if __cplusplus >= 201103L #include <array> #include <tuple> #include <initializer_list> #include <unordered_set> #include <unordered_map> #include <forward_list> #define cauto const auto& #else #endif using namespace std; namespace{ typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vint; typedef vector<vector<int> > vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long> > vvll, vvLL; #define VV(T) vector<vector< T > > template <class T> void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){ v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t){ stringstream ss; ss << f; ss >> t; } #define reep(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) reep((i),0,(n)) #define ALL(v) (v).begin(),(v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(),(v).rend() #define MOD 1000000007LL #define EPS 1e-8 static const int INF=1<<24; void mainmain(){ string s; while(getline(cin,s)){ // cout<<s<<endl; stringstream ss; ss<<s; vint v; int t; vint dp(30,0); while(ss>>t){ // cout<<t<<endl; if(t==0) return; v.PB(t); } dp[0]=1; rep(i,v.size()){ vint tmp=dp; if(i) dp=vint(30,0); int tt=v[i]; if(v[i]>=10) tt=10; // cout<<tt<<endl; // cout<<i<<endl; // rep(j,30){ // cout<<j<<" "<<tmp[j]<<endl; // } for(int j=29;j>=tt;j--){ if(tmp[j-tt]) dp[j]=1; } if(v[i]==1){ tt=11; for(int j=29;j>=tt;j--){ if(tmp[j-tt]) dp[j]=1; } } dp[0]=0; } dp[0]=1; for(int i=21;i>=0;i--){ if(dp[i]){ cout<<i<<endl; break; } } } } } int main() try{ mainmain(); } catch(...){ }
#include "bits/stdc++.h" using namespace std; int main(){ string s; while(getline(cin,s)){ if(s=="0") break; stringstream ss; vector<int> a; int n,f=0,sum=0; ss<<s; while(ss>>n){ if(n==1)f=1; a.push_back((n>=10)?10:n); if(ss.eof()) break; } for(int i=0;i<a.size();++i) sum+=a[i]; if(f&&sum<=11) sum+=10; if(sum>21)cout<<0<<endl; else cout<<sum<<endl; } return 0; }
#include <iostream> int getScore(int h[], int n) { int res = 0; int one_count = 0; for (int i=0; i<n; i++) { if (2 <= h[i] && h[i] <= 9) res += h[i]; else if (h[i] >= 10) res += 10; else { res++; one_count++; } } if (res >= 22) return 0; else { while (res<=11 && one_count>0) { res += 10; one_count--; } return res; } } int main() { int hand[100]; std::string input; while (true) { getline(std::cin, input); int offset = 0; int pos; int index = 0; int h; while ( (pos = input.find(' ', offset)) != std::string::npos ) { h = std::stoi(input.substr(offset, pos-offset)); hand[index] = h; index++; offset = pos + 1; } h = std::stoi(input.substr(offset)); if (h==0) break; hand[index] = h; std::cout << getScore(hand, index+1) << std::endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string in; while (getline(cin, in), in != "0") { stringstream s(in); int sum = 0, n; bool flg = false; while (s >> n) { if (2 <= n && n <= 9) { sum += n; } else if (n >= 10) { sum += 10; } else { sum++; flg = true; } } if (sum <= 11 && flg) { sum += 10; } if (sum > 21) { cout << 0 << endl; } else { cout << sum << endl; } } return 0; }
#include<iostream> #include<string> using namespace std; int fixNumberOne( int cnt, int a ) { if( cnt == 0 ) return a; if( a + 11 <= 22 - cnt ) return a + 10 + cnt; else return a + cnt; } int main() { int a[128], b[100]; for( int i = '1'; i < '1' + 9; i++ ) a[i] = i - '0'; a['1'+'0'] = 10; a['1'+'1'] = 10; a['1'+'2'] = 10; a['1'+'3'] = 10; string str; while( getline( cin, str ) ) { if( str == "0" ) break; str += " "; for( int i = 0; i < 100; i++ ) b[i] = 0; int j = 0, sum = 0, cnt = 0; for( int i = 0; i < str.size(); i++ ) { if( str[i] == ' ' ) { if( a[sum] == 1 ) cnt++; else b[j] = a[sum]; sum = 0; j++; } else sum += str[i]; } sum = 0; for( int i = 0; i < 100; i++ ) if( b[i] != 1 ) sum += b[i]; sum = fixNumberOne( cnt, sum ); if( sum > 21 ) cout << 0 << endl; else cout << sum << endl; } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <cassert> #include <iostream> #include <cctype> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <set> #include <stack> #include <map> #include <utility> #include <numeric> #include <algorithm> #include <iterator> #include <bitset> #include <complex> #include <fstream> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for(int i = x; i < n; i++) template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); } template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; } template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; } template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); } int main() { for(string line; getline(cin, line) && line[0] != '0';) { int points = 0, one = 0; istringstream iss(line); for(string word; iss >> word; ) { int tmp = atoi(word.c_str()); if(tmp == 1) one++; else points += (tmp < 10 ? tmp : 10); } int total = 0; for(int i = 0; i <= one; i++) { int tmp = points + i * 10 + one; if(tmp <= 21) chmax(total, tmp); } cout << (total <= 21 ? total : 0) << endl; } }
#define _USE_MATH_DEFINES #include <iostream> #include <memory> #include <memory.h> #include <fstream> #include <cmath> #include <math.h> #include <numeric> #include <vector> #include <stack> #include <string> #include <queue> #include <sstream> #include <cstdlib> #include <cassert> #include <cstdio> #include <map> #include <iomanip> #include <list> #include <cctype> #include <algorithm> #include <complex> #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, m, n) for(int i = m; i < n; i++) using namespace std; typedef complex<double> xy_t; typedef pair<xy_t, xy_t> line; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; typedef pair<int, PP> PPP; const int INF = 1 << 29; const double EPS = 1E-10; int main(){ string s; while(getline(cin, s) && s != "0"){ int num; int sum = 0; bool one = false; stringstream in(s); while(in >> num){ if(num == 1) one = true; sum += min(num, 10); } if(sum <= 11 && one) sum += 10; if(sum > 21) sum = 0; cout << sum << endl; } return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <sstream> using namespace std; // ?????????????????????????????§????????£??????????????? vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str); string buffer; while(getline(ss, buffer, sep)){ v.push_back(buffer); } return v; } int int_str(string str){ int num = 0; bool minus = false; if(str[0] == '-'){ str = str.substr(1); minus = true; } for(int i = 0; i < str.size(); i++){ num *= 10; num += (str[i] - '0'); } if(minus){ num = -num; } return num; } vector<int> str_to_int_vector(vector<string> v_str){ vector<int> v_int; for(int i = 0; i < v_str.size(); i++){ v_int.push_back(int_str(v_str.at(i))); } return v_int; } int main(){ while(true){ string str; getline(cin, str); if(cin.eof()){ break; } vector<string> v_str = split(str, ' '); vector<int> v_int = str_to_int_vector(v_str); if(v_int.at(0) == 0){ return 0; } int ans = 0; int count_1 = 0; for(int i = 0; i < v_int.size(); i++){ if(v_int.at(i) == 1){ count_1++; ans += v_int.at(i); }else if(v_int.at(i) <= 10){ ans += v_int.at(i); }else{ ans += 10; } } if(count_1 > 0){ if(ans <= 11){ ans += 10; count_1--; } } if(ans > 21){ ans = 0; } cout << ans << endl; } return 0; }
#include <iostream> #include <cstdio> using namespace std; int main(){ int fla=0,cou,sum,b; char a; while(1){ sum=0; cou=0; while(1){ scanf("%d%c",&b,&a); if(b>=10)sum+=10; else sum+=b; if(b==1)cou++; if(a=='\n')break; } if(sum==0)break; for(int i=0;i<cou;i++){ if(21-sum>=10)sum+=10; else break; } if(sum>21)cout << "0"<<endl; else cout << sum << endl; } return 0; }
#include<iostream> #include<string> #include<sstream> #include<algorithm> using namespace std; int main(){ string line; while(1){ getline(cin, line); if ( line == "0" ) break; stringstream sin(line); int x, one = 0, sum = 0; while( sin >> x ){ if ( 10 <= x ) x = 10; if ( x == 1 ) one++; sum += x; } for ( int i = 0; i < one; i++ ){ if ( sum + 10 <= 21 ) sum += 10; } if ( sum > 21 ) sum = 0; cout << sum << endl; } }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { string s; while(getline(cin, s)) { if(s[0] == '0') break; vector<int> card; int one = 0; for(int i = 0; i < s.size(); i++) { if('0' <= s[i] && s[i] <= '9') { if(i+1 != s.size() && '0' <= s[i+1] && s[i+1] <= '3') { card.push_back((s[i]-'0')*10 + (s[i+1]-'0')); i++; } else { card.push_back(s[i]-'0'); } if(i > 0 && s[i-1] == ' ' && s[i] == '1') one++; else if(i == 0 && s[i+1] == ' ' && s[i] == '1') one++; } } sort(card.begin(), card.end(), greater<int>()); int ans = 0; for(int i = 0; i < card.size(); i++) { if(card[i] != 1) { if(card[i] > 10) ans += 10; else ans += card[i]; } else { if(ans+10+one <= 21) ans += 11; else ans += 1; one--; } } if(ans > 21) ans = 0; cout << ans << endl; } }
#include <iostream> #include <sstream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { int n, p, q, s, t; vector<int> v; string buf; while (1) { getline(cin, buf); istringstream iss(buf); iss >> p; if (!p) break; v.clear(); v.push_back(p); while (iss >> p) { v.push_back(p); } s = t = 0; for (int i=0; i<(int)v.size(); i++) { if (v[i] < 10) { s += v[i]; if (v[i] == 1) { ++t; } } else { s += 10; } } if (s > 21) { s = 0; } else { s += 10 * min(t, (21-s)/10); } cout << s << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> v; int dp(int p, int n){ if(p == v.size() && n <= 21) return n; else if(21 < n) return 0; else { if(v[p] == 1){ return max(dp(p+1, n+1), dp(p+1, n+11)); } else return dp(p+1, n+v[p]); } } int main(){ string input; while(getline(cin, input), input != "0"){ int n; v.clear(); while(input.find_first_of(" ") != string::npos){ n = atoi(input.substr(0, input.find_first_of(" ")).c_str()); if(n/10 == 1) n = n-n%10; v.push_back(n); input = input.substr(input.find_first_of(" ")+1); } n = atoi(input.c_str()); if(n/10 == 1) n = n-n%10; v.push_back(n); cout << dp(0, 0) << endl; } }
#define _USE_MATH_DEFINES #define MAX_N 1000000 #include <iostream> #include <sstream> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> using namespace std; typedef long long ll; typedef pair<int,int> P; static const double eps = 1e-8; vector<int> split(string delim,string str){ vector<int> res; str += delim; for(int i=0;i<str.size();i++){ for(int j=1;i+j<=str.size();j++){ if(str.substr(i,j).find(delim) != string::npos){ int num = atoi(str.substr(i,j-1).c_str()); if(str.substr(i,j-1).size() >0 ) res.push_back(num); i+=j-1; break; } } } return res; } int main(){ string str; while(getline(cin,str)){ vector<int> data = split(" ",str); if(data.size()==1 && data[0]==0) break; set<int>* dp = new set<int>[data.size()+1]; dp[0].insert(0); for(int i=0;i<data.size();i++){ if(data[i] == 1){ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ dp[i+1].insert(*it+1); dp[i+1].insert(*it+11); } } else if(2 <= data[i] && data[i] <= 9){ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ dp[i+1].insert(*it+data[i]); } } else{ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ dp[i+1].insert(*it+10); } } } int res=MAX_N; for(set<int>::iterator it = dp[data.size()].begin(); it != dp[data.size()].end(); it++){ if(*it <= 21 && abs(*it-21) < abs(res-21)){ res = * it; } } printf("%d\n",res == MAX_N ? 0 : res); delete[] dp; } }
#include <iostream> #include <string> #include <cstdlib> #include <vector> using namespace std; vector<int> nums; vector<string> split(string str, string delim) { int pos = 0; vector<string> vec; while((pos=str.find_first_of(delim)) != str.npos) { if(pos > 0) { vec.push_back(str.substr(0, pos)); } str = str.substr(pos + 1); } if(str.length() > 0) { vec.push_back(str); } return vec; } void solve() { int sum = 0; int c = 0; for(int i=0; i<nums.size(); i++) { if(nums[i]>=10) sum += 10; else if(nums[i] != 1) sum += nums[i]; else sum += 11, c++; } for(int i=0; i<c; i++) { if(sum > 21) sum -= 10; else break; } sum = sum > 21 ? 0 : sum; cout << sum << endl; } int main(int argc, char** argv) { string s; vector<string> vec; while(getline(cin, s) && s!="0") { vec = split(s, " "); nums.clear(); for(int i=0; i<vec.size(); i++) { nums.push_back(atoi(vec[i].c_str())); } solve(); } }
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <stack> #include <queue> using namespace std; typedef long long LL; typedef pair<int, int> PII; static const double EPS = 1e-5; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) int main(void){ string in; while(getline(cin,in)){ if(in=="0") break; vector<int> num; stringstream ss(in); int tmp; while(ss>>tmp) num.push_back(tmp); int ace = 0; int ans = 0; REP(i,num.size()){ if(num[i]>10) num[i] = 10; if(num[i]==1){ ace++; num[i] = 11; } ans += num[i]; } while(ace&&ans>21){ ace--; ans-=10; } cout<<((ans>21)?0:ans)<<endl; } return 0; }
#include<iostream> #include<string> using namespace std; int main(){ string str; while(getline(cin,str)&&str!="0"){ int ans=0,one=0; for(int i=0;i<str.size();i++){ if(str[i]!=' '){ if(str[i]=='1'){ if(str[i+1]>='0'){ ans+=10; i++; } else one++,ans++; } else ans+=str[i]-'0'; } } for(int i=0;i<one;i++){ if(ans+10<22)ans+=10; } if(ans>21)ans=0; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int rec(int sum, const vector<int> &v, int k) { if(v.size() > 21) return 0; if(k == v.size()) return sum <= 21 ? sum : 0; if(v[k] == 1) return max(rec(sum+1, v, k+1), rec(sum+11, v, k+1)); else return rec(sum+v[k], v, k+1); } int main() { string s; while(getline(cin,s) && s != "0") { stringstream ss(s); int a; vector<int> v; while(ss >> a) { v.push_back(min(a,10)); } cout << rec(0, v, 0) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; int ans,f,num; string s; int getNum(int x){ int res=0; for(int i=x;s[i]!=' '&&i!=s.size();i++){ res=res*10+s[i]-'0'; } return res; } int main(){ int d[14]={0,1,2,3,4,5,6,7,8,9,10,10,10,10}; while(1){ getline(cin,s); if(s=="0")break; ans=0,f=0; for(int i=0;i<s.size();i++){ num=getNum(i); while(s[i]!=' '&&i!=s.size())i++; ans+=d[num]; if(num==1)f=1; } if(f&&ans+10<=21)cout<<ans+10<<endl; else if(ans<=21)cout<<ans<<endl; else cout<<0<<endl; } return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<map> #include<set> #include<queue> #include<cstdio> #include<climits> #include<cmath> #include<cstring> #include<string> #include<sstream> #include<numeric> #include<complex> #define f first #define s second #define mp make_pair #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++) #define ALL(c) (c).begin(), (c).end() using namespace std; typedef unsigned int uint; typedef long long ll; typedef complex<double> P; int main(){ while(true){ vector<int> v; string str; getline(cin, str); stringstream ss(str); int tmp; while(ss>>tmp){ if(tmp == 0) goto end; if(tmp > 10) tmp = 10; v.push_back(tmp); } sort(v.begin(), v.end()); int sum = accumulate(ALL(v), 0); if(sum < 12 && v[0] == 1) sum += 10; if(sum > 21) sum = 0; cout << sum << endl; } end: return 0; }
// 2011/02/26 Tazoe #include <iostream> #include <string> #include <sstream> using namespace std; int main() { while(true){ string str; getline(cin, str); istringstream ist(str); int crd; int sum = 0; int flg = false; while(ist >> crd){ if(crd==0){ return 0; } else if(crd==1){ sum += 1; flg = true; } else if(crd>=2&&crd<=9){ sum += crd; } else if(crd>=10&&crd<=13){ sum += 10; } if(sum>21) break; } if(sum<=11&&flg) sum += 10; if(sum>21) sum = 0; cout << sum << endl; } }
#include <iostream> #include <sstream> using namespace std; int main(){ string in; while(getline(cin,in) ,in != "0"){ stringstream s(in); int sum = 0,n; bool flg = false; while(s >> n){ if(2 <= n && n <= 9){ sum += n; }else if(n >= 10){ sum += 10; }else{ sum++; flg = true; } } if(sum <= 11 && flg){ sum += 10; } if(sum > 21){ cout << 0 << endl; }else{ cout << sum << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; /*int stoi(string a){ int ret; stringstream ss; ss<<a; ss>>ret; return ret; }*/ int main(){ string s; while(getline(cin,s)&&s!="0"){ s.push_back(' '); int one=0; int sum=0; string a; for(int i=0;i<s.size();i++){ if(s[i]==' '){ int b=stoi(a); if(b==1)one++; else if(b>10)sum+=10; else sum+=b; a.clear(); } else a.push_back(s[i]); } sum+=one; for(int i=0;i<one;i++){ if(sum<12)sum+=10; } if(sum>21)sum=0; cout<<sum<<endl; } return 0; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <cstdio> #include <functional> #include <numeric> #include <stack> #include <queue> #include <map> #include <set> #include <utility> #include <sstream> #include <complex> #include <fstream> using namespace std; #define FOR(i,a,b) for(long long i=(a);i<(b);i++) #define REP(i,N) for(long long i=0;i<(N);i++) #define ALL(s) (s).begin(),(s).end() #define fi first #define se second #define PI acos(-1.0) #define INF 1000000007 #define MOD 1000000007 #define EPS 1e-10 #define MAX_N 100100 #define MAX_M 100100 typedef long long ll; typedef pair<ll, ll> P; typedef pair<double, double> PD; typedef pair<string, ll> PS; typedef vector<ll> V; typedef pair<P, char> PC; typedef pair<int, string> PSI; string s; int stringtoi(string s){ int res; stringstream ss; ss << s; ss >> res; return res; } template <typename List> void split(const string s, const string delim, List& result){ result.clear(); string::size_type pos = 0; while (pos != string::npos){ string::size_type p = s.find(delim, pos); if (p == -1){ result.push_back(stringtoi(s.substr(pos))); break; } else{ result.push_back(stringtoi(s.substr(pos, p - pos))); } pos = p + delim.size(); } } int main(){ while (getline(cin, s) && s != "0"){ vector<int> vi; int cnt = 0, sum = 0; split(s, " ", vi); REP(i, vi.size()){ if (vi[i] == 1)cnt++; if (vi[i] >= 10)sum += 10; else sum += vi[i]; } while (sum <= 11 && cnt > 0){ cnt--; sum += 10; } if (sum <= 21)cout << sum << endl; else cout << 0 << endl; } }
#include <iostream> using namespace std; int main() { int n, p, t, i; while (1) { p = 0; t = 0; while (1) { cin >> n; if (n == 0) return 0; if (2 <= n && n <= 9) p += n; else if (n >= 10) p += 10; else { t++; p++; } if (cin.get() == '\n') break; } for (i = 0; i < t; i++) { if (p + 10 <= 21) p += 10; } if (p > 21) { p = 0; } cout << p << endl; } }
#include <iostream> #include <string> #include <queue> using namespace std; int main() { queue<int> q; string a; while (true) { getline(cin, a); if (a[0] == '0') { break; } int counter = 0; int b = 0; int sum = 0; for (int i = 0; i <= a.size(); i++) { if (a[i] >= '0' && a[i] <= '9') { b *= 10; b += a[i] - '0'; } else { if (b > 10) { b = 10; } else if (b == 1) { counter++; } sum += b; b = 0; } } while (counter > 0) { if (sum + 10 <= 21) { sum += 10; counter--; } else { break; } } if (sum > 21) { sum = 0; } q.push(sum); } while (!q.empty()) { cout << q.front() << endl; q.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ while(1){ int n=0,card[100]={}; string str; getline(cin,str); str+=' '; for(int i=0,num=0;i<str.size();i++){ if(isdigit(str[i]))num=num*10+str[i]-'0'; else card[n++]=num,num=0; } if(card[0]==0)break; sort(card,card+n,greater<int>()); int score=0; for(int i=0;i<n;i++){ if(card[i]>=10)score+=10; else if(card[i]>1)score+=card[i]; else if(score+11+(n-i-1)<=21)score+=11; else score++; } cout <<(score<=21? score:0)<<endl; } return 0; }
#include <iostream> #include <string> #include <sstream> using namespace std; int main(){ string str; while(getline(cin,str),str!="0"){ int sum = 0,num,cnt=0; stringstream s; s << str; while(s >> num){ if(2 <= num && num <= 9) sum += num; else if(num >= 10) sum += 10; else{ sum++; cnt++; } } if(sum <= 11 && cnt){ sum += 10; cnt--; } if(sum > 21) cout << '0' << endl; else cout << sum << endl; } return 0; }
#include<iostream> #include<algorithm> #include<sstream> #include<string> #include<vector> using namespace std; int main(){ string s; while(getline(cin,s)){ stringstream ss(s); vector<int> V; int x; while(ss>>x){ V.push_back(x); } if(V[0] == 0)break; int ans = 0; int num = 0; for(int i = 0;i < V.size();i++){ if(V[i] == 1)ans += 1,num++; else if(V[i] > 9)ans += 10; else ans += V[i]; } if(num>0 && ans<=11)ans += 10; if(ans > 21)ans = 0; cout<<ans<<endl; } }
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include <vector> #include <map> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #include <iostream> #include <string> #define REP(i,n) for(long long i=0;i<n;++i) #define REPR(i,n) for(long long i=n;i>=0;--i) #define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr) #define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr) #define FOR(i,a,b) for(long long i=a;i<b;++i) #define SORT(v,n) sort(v, v+n) #define SORTV(v) sort(v.begin(), v.end()) #define ALL(v) v.begin(),v.end() #define llong long long #define INF 999999999 #define MOD 1000000007 #define pb push_back #define pf push_front #define MP make_pair #define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}} int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; using namespace std; typedef pair<int,int> pii; int solve(vector<int> &v, int i, int sum){ if(sum > 21) return 0; if(i == v.size()){ if(sum > 21) sum = 0; return sum; } int res = 0; if(v[i] == 1){ res = max(solve(v, i+1, sum+1), solve(v, i+1, sum+11)); }else if(v[i] >= 2 && v[i] <= 9){ res = solve(v, i+1, sum+v[i]); }else{ res = solve(v, i+1, sum+10); } return res; } int main(){ string s; while(true){ getline(cin, s); if(s == "0") break; vector<int> v; int len = s.length(); int idx = 0; REP(i,len){ if(s[i] == ' '){ //cout << s.substr(idx, i-idx) << endl; v.pb(stoi(s.substr(idx, i-idx))); idx = i + 1; } } v.pb(stoi(s.substr(idx, len-idx))); cout << solve(v, 0, 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int CardNumber[100]; int main() { int len, sum; string str; stringstream ss; while (1) { getline(cin, str); if (str.size() == 1 && str[0] == '0') { break; } memset(CardNumber, 0, sizeof(CardNumber)); len = 0; ss << str; while (ss >> CardNumber[len]) { len++; } sort(CardNumber, CardNumber + len); /*cout << len << endl; for (int i = 0; i < len; i++) { cout << CardNumber[i] << ends; } cout << endl;*/ sum = 0; for (int i = len - 1; i > -1; i--) { if (CardNumber[i] != 1) { sum += min(CardNumber[i], 10); } else { if (sum + i < 11) { sum += 11; } else { sum++; } } } sum = sum > 21 ? 0 : sum; cout << sum << endl; ss.clear(); str = ""; } return 0; }
#include <sstream> #include <algorithm> #include <iomanip> #include <iostream> #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; /** Problem0169 : Blackjack **/ int main() { int ans, one, t; string s; while (!cin.eof()) { getline(cin, s); if (s == "0") break; stringstream ss(s); ans=0; one=0; while (!ss.eof()) { ss >> t; if (2 <= t && t < 10) ans += t; else if (t == 1) { one++; ans+=11; } else { ans+=10; } } while (ans>21 && one>0) { one--; ans-=10; } if (ans > 21) ans=0; cout << ans << endl; } return 0; }
#define _USE_MATH_DEFINES #include <iostream> #include <algorithm> #include <functional> #include <vector> #include <cstdio> #include <cstring> #include <cmath> #include <cfloat> #include <map> #include <queue> #include <stack> #include <list> #include <string> using namespace std; int main(){ string s; while(1){ getline(cin,s); long long n=0; int count=0; if(s=="0") break; else{ string a=""; for(int i=0;i<s.size();i++){ if(s[i]!=' '){ a=a+s[i]; } else{ int b=stoi(a); if(b==1) count+=11; else if(b>=2&&b<=9) n+=b; else n+=10; a=""; } } int b=stoi(a); if(b==1) count+=11; else if(b>=2&&b<=9) n+=b; else n+=10; a=""; while(count>0){ if(n+count>21) n++; else n=n+11; count-=11; } if(n>21) cout<<0<<endl; else cout<<n<<endl; } } }
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<sstream> #include<stack> using namespace std; int main(){ string str; while(getline(cin,str)){ stringstream ss(str); vector<int>V; while(ss){ int a; ss>>a; V.push_back(a); } if(V[0]==0)break; V.pop_back(); stack<pair<int,int> >S; S.push(pair<int,int>(0,0)); int Max=0; while(!S.empty()){ int pnt=S.top().first,cnt=S.top().second; S.pop(); if(pnt>21)continue; if(cnt==V.size()){ Max=max(Max,pnt); continue; } int to=V[cnt]; if(to>10)to=10; S.push(pair<int,int>(pnt+to,cnt+1)); if(to==1)S.push(pair<int,int>(pnt+11,cnt+1)); } cout<<Max<<endl; } return 0; }
#include <iostream> #include <sstream> #include <string> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <algorithm> #include <numeric> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> using namespace std; typedef istringstream ISS; typedef ostringstream OSS; typedef vector<string> VS; typedef vector<int> VI; typedef vector<VI> VVI; template<class T> T gcd( T a, T b ) { return !b ? a : gcd( b, a % b ); } template<class T> T lcm( T a, T b ) { return a / gcd( a, b ) * b; } template<class T> string print_v( vector<T> v ) { OSS oss; for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) { oss << *it_i << ", "; } return oss.str(); } VI A; int n; int solve( int k, int d ) { if ( k >= n ) return d; if ( A[k] == 1 ) { return max( ( d + 1 > 21 ? 0 : solve( k + 1, d + 1 ) ), ( d + 11 > 21 ? 0 : solve( k + 1, d + 11 ) ) ); } else if ( A[k] >= 10 ) { if ( d + 10 > 21 ) return 0; return solve( k + 1, d + 10 ); } else { if ( d + A[k] > 21 ) return 0; return solve( k + 1, d + A[k] ); } return 0; } int main() { string line; while ( getline( cin, line ) && line != "0" ) { A.clear(); int w; ISS iss( line ); while ( iss >> w ) A.push_back( w ); n = A.size(); cout << solve( 0, 0 ) << endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cstdlib> #define REP(i,k,n) for(int i=k;i<n;i++) #define rep(i,n) for(int i=0;i<n;i++) using namespace std; vector<string> split(string &str,char delim) { vector<string> res; size_t current = 0,found; while((found = str.find_first_of(delim,current)) != string::npos) { res.push_back(string(str,current,found - current)); current = found + 1; } res.push_back(string(str,current,str.size() - current)); return res; } int main() { string s; int ans; bool flag; while(true) { ans = 0; flag = false; getline(cin,s); vector<string> temp = split(s,' '); if(atoi(temp[0].c_str()) == 0) break; vector<int> res; rep(i,temp.size()) { res.push_back(atoi(temp[i].c_str())); } sort(res.begin(),res.end(),greater<int>()); rep(i,res.size()) { if(res[i] == 1) { if(i == res.size()-1) { if(ans + 11 > 21) ans += 1; else ans += 11; } else ans += 1; } else if(res[i] >= 10) { ans += 10; } else ans += res[i]; if(ans > 21) { flag = true; } } if(flag) { cout << 0 << endl; } else { cout << ans << endl; } } return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int main(){ string base; int sum,tmp,count; while(getline(cin,base)){ if(base[0]=='0')break; base+=' '; count=sum=tmp=0; for(int i=0;i<base.size();i++){ if(base[i]==' '){ if(tmp>10)tmp=10; if(tmp==1)count++,tmp+=10; sum+=tmp; tmp=0; } else{ tmp*=10; tmp+=(base[i]-'0'); } } while(sum>21 && count>0){ sum-=10; count--; } if(sum>21)cout<<0<<endl; else cout<<sum<<endl; } return 0; }
#include <iostream> using namespace std; int rec(int sum, int one) { if (sum > 21) return -1; else if (one == 0 && sum > 21) return -1; else if (one == 0 && sum <= 21) return sum; int res = rec(sum + 1, one - 1); res = max(res, rec(sum + 11, one - 1)); return res; } bool solve(string &s) { int sum = 0, one = 0; int index = 0; s += " "; while (index < s.size()) { int num = 0; while (index < s.size()) { if ('0' <= s[index] && s[index] <= '9') { num *= 10; num += (int)(s[index] - '0'); index++; } else break; } if (num == 1) one++; else if (num / 10 == 1) sum += 10; else sum += num; index++; } if (sum == 0 && one == 0) return false; if (sum > 21) { cout << 0 << endl; return true; } else if (one == 0) { cout << sum << endl; return true; } else { int ans = rec(sum, one); if (ans >= 0) cout << ans << endl; else cout << 0 << endl; return true; } } int main() { string s; while (true) { getline(cin, s); if (!solve(s)) break; } return 0; }
#define _USE_MATH_DEFINES #define MAX_N 1000000 #include <iostream> #include <sstream> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> using namespace std; typedef long long ll; typedef pair<int,int> P; static const double eps = 1e-8; vector<int> split(string delim,string str){ vector<int> res; str += delim; for(int i=0;i<str.size();i++){ for(int j=1;i+j<=str.size();j++){ if(str.substr(i,j).find(delim) != string::npos){ int num = atoi(str.substr(i,j-1).c_str()); if(str.substr(i,j-1).size() >0 ) res.push_back(num); i+=j-1; break; } } } return res; } int main(){ string str; while(getline(cin,str)){ vector<int> data = split(" ",str); if(data.size()==1 && data[0]==0) break; set<int>* dp = new set<int>[data.size()+1]; dp[0].insert(0); for(int i=0;i<data.size();i++){ if(data[i] == 1){ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ if(*it+1 <= 21) dp[i+1].insert(*it+1); if(*it+11 <= 21) dp[i+1].insert(*it+11); } } else if(2 <= data[i] && data[i] <= 9){ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ if(*it+data[i] <= 21) dp[i+1].insert(*it+data[i]); } } else{ for(set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++){ if(*it+10<= 21) dp[i+1].insert(*it+10); } } } int res=MAX_N; for(set<int>::iterator it = dp[data.size()].begin(); it != dp[data.size()].end(); it++){ if(*it <= 21 && abs(*it-21) < abs(res-21)){ res = * it; } } printf("%d\n",res == MAX_N ? 0 : res); delete[] dp; } }
#include <iostream> #include <sstream> using namespace std; int main(){ int a,r,x; string s; for(;getline(cin,s);cout<<(r>21?0:a&&r<12?r+10:r)<<endl){ istringstream is(s); for(a=r=0;is>>x;r+=x>9?10:x)if(x==1)a=1;else if(!x)return 0; } }