アットウィキロゴ

[aoj]0038

#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;
}
 
最終更新:2011年06月16日 23:33
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。