Пример #1
0
 /** Moves the snake.
     @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 
     @return The game's score after the move. Return -1 if game over. 
     Game over when snake crosses the screen boundary or bites its body. */
 int move(string direction) {
     pair<int, int> head = snake.back(); 
     int x = head.first, y = head.second;
     if(direction == "U")    x--;
     else if(direction == "D") x++;
     else if(direction == "L") y--;
     else if(direction == "R") y++;
     
     if(isDead(x,y)){
         return -1;
     }
     else{
         if(!isFood(x,y)){
             // if not food, advance
             table[snake.front().first][snake.front().second] = false;
             snake.pop();
         }
         snake.push(make_pair(x,y));
         table[x][y] = true;
     }
     return score;
 }
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;

class Stack {
public:
    // Push element x onto stack.
    queue<int> q;
    void push(int x) {
        q.push(x);     
    }
    // Removes the element on top of the stack.
    void pop() {
      q.back()  
    }
    // Get the top element.
    int top() {
        
    }
    // Return whether the stack is empty.
    bool empty() {
        
    }
};
int main(){
	return 0;
}
Пример #3
0
//This function executes a command from the history queue
void historyCommand(char* toks[]){
  int size = history.size();

  string argument = toks[1];

  Command node;
  char **temp;

  //execute the most recent command
  if (argument.compare("!") == 0) {
    if (history.size() != 0) { //if there is history
      node = history.back();
      string comp = (node.getParts())[0]; //string used for comparison

      if(comp.compare("!") != 0) { //if previous command is not "!!" or "!X"
        temp = node.getParts();

        //pass the command
        if(commandIsInternal(temp[0])){
          executeInternalCommand(temp);
        } else {
          executeExternalCommand(temp);
        }
      }
      else { //previous command is !!
        cout << "Error: The previous command is \"!!\" or \"!X\"" << endl;
      }
    }
    else { //there is no history
      cout << "Error: There are no commands in history." << endl;
    }
  }
  else {
    //execute the Nth command

    //get the X from !X
    string convert = toks[1];

    int n = atoi(convert.c_str());

    //test if the history queue is even as long as the int
    if (n < history.size()+1) {

      //loop through queue by popping and pushing and grab Nth command
      int size = history.size();
      int ii;
      for(ii = 0; ii < size; ii++){
        Command xx = history.front();
        history.pop();
        history.push(xx);
        if (n == (size-ii)) {
          node = xx;
        }
      } //end for loop

      temp = node.getParts();
      string comp = temp[0];

      if (comp.compare("!") == 0) {
        cout << "The command you are asking for is \"!!\" or \"!X\"." << endl;
      }
      else{

        //pass the command
        if(commandIsInternal(temp[0])){
          executeInternalCommand(temp);
        }
        else {
          executeExternalCommand(temp);
        }
      }
    }
    else {
      //Command does not exist
      cout << "No such command in history" << endl;
    }
  } //end else
} //end historyCommand()
 // Get the top element.
 int top() {
     return rse.back();
 }
 /** Get the top element. */
 int top() {
     return que.back();
 }
// Get the top element.
int top() {
	return in.back();
}
Пример #7
0
int main(){
	que_sym.push({1,"me"});
	printf("%d:%s\n", que_sym.back().id_num, que_sym.back().name);
	return 0;
}
Пример #8
0
void
Audicle_Shell_UI::get_output( string& out ) { out = _outputs.back() ; 
											  _outputs.pop(); }
 T top()
 {
     if (q1.empty()) throw runtime_error("bad operation");
     return q1.back();
 }
 // Get the top element.
 int top() {
     return stack.back();
 }
int main(int argc, char* argv[])
{
	// Start 
    VideoCapture capture (CV_CAP_OPENNI);
    if(!capture.isOpened())
    {
        int error = -1;
        return 1;
    }

    namedWindow( "Color Image", 1 );
	//namedWindow( "Depth Map", 1);
    Mat view;
    bool blink = false;


	// NITE + openni
	
	XnStatus rc = XN_STATUS_OK;
	
	Context context;
	rc = context.Init();
	rc = g_GestureGenerator.Create(context);
	rc = g_HandsGenerator.Create(context);
	XnCallbackHandle hcb1,hcb2; 
	g_GestureGenerator.RegisterGestureCallbacks(Gesture_Recognized, Gesture_Process, NULL, hcb1);
	g_HandsGenerator.RegisterHandCallbacks(handCreate, handUpdate, handDestroy, NULL, hcb2);
	rc = context.StartGeneratingAll();
	rc = g_GestureGenerator.AddGesture(GESTURE_TO_USE, NULL);

	double d = 1.0;
	double angle = 0.0;
	double angleZ = 0.0;

	Mat result;
	
	Mat orig = imread("crocus.jpg");
	result.create(750, 750, CV_8UC3);
	double centerX = orig.cols/2;
    double centerY = orig.rows/2;
	warpPerspective(orig, orig, getScaleMatrix(1.0), orig.size(), INTER_CUBIC, BORDER_TRANSPARENT);

    while( capture.isOpened() )
    {
		rc = context.WaitAndUpdateAll();
		d = getZoom();
		angle = getAngle();
		angleZ = getAngle3D();
		if (abs(d - oldZoom) > 0.009) {
			//printf("angle = %f \n",angle);
			oldAngle += angle;

			//create the transformation to be passed to warp
			Mat openCVTransform = getRotationMatrix2D(Point2f(centerX, centerY), oldAngle, d);

			//warp image to apply transformation
			result.setTo(Scalar(0));
			warpAffine(orig, result, openCVTransform, result.size(), INTER_CUBIC, BORDER_TRANSPARENT);
			oldZoom = d;
			
		}
		imshow("Result", result);
		

        Mat bgrImage;
        capture.grab();

		capture.retrieve( bgrImage, CV_CAP_OPENNI_BGR_IMAGE );

		if (hand1ID != -1) {
			circle(bgrImage,Point(hand1.back().X + bgrImage.rows/2, bgrImage.cols/2 - hand1.back().Y),2,CV_RGB(0,255,0),3);
		
		}
		if (hand2ID != -1) {
			circle(bgrImage,Point(hand2.back().X + bgrImage.rows/2, bgrImage.cols/2 - hand2.back().Y),2,CV_RGB(0,255,0),3);
		}
		flip(bgrImage,bgrImage,1);
        imshow("Color Image", bgrImage);
		result.create(750, 750, CV_8UC3);
		
        if(waitKey(33) == 'q')
        {
            break;
        }
    }
	context.Shutdown();
    return 0;
}
 // Get the top element.
 int top() {
     return q.back();
 }
 // Get the top element.
 int top() {
     return my_que_A.back();
 }
Пример #14
0
int main() {
	int length;
	int x[8];
	int value;
	// 预先存储康托展开需要的阶层,7!, 6!, 5!, 4!, 3!, 2!, 1!
	int fact[] = { 5040, 720, 120, 24, 6, 2, 1}; 
	int cantor;
	while (cin >> length && length != -1) {
		bool isMatch = false;
		RubicsCube new_rc;
		RubicsCube first_rc;

		memset(isFound, false, sizeof(isFound));
		while (!que_rubicsCube.empty()) {
			que_rubicsCube.pop();
		}

		first_rc.value = 12348765;
		first_rc.step = "";
		que_rubicsCube.push(first_rc);

		for (int i = 0; i < 8; i++) {
			cin >> x[i];
		}
		Array2Int(x, value);

		if (value == 12348765) { // 若目标和初始状态一样,则直接得出结果
			isMatch = true;
		}

		while (!isMatch) {
			RubicsCube rc = que_rubicsCube.front(); // 取队列头部数据
			if (rc.step.length() == length) { // 若不满足在规定步数内达到目标状态,则退出循环
				break;
			}

			// A operation
			CubeOperation(rc, new_rc, 'A');	// 进行A操作
			cantor = GetCantor(new_rc.value, fact);	// 获得康托展开对应数值
			if (isFound[cantor] == false) { // 若没有访问过,则存入队列尾部,并令isFound为true
				isFound[cantor] = true;
				que_rubicsCube.push(new_rc);
				if (IsMatch(value, new_rc) == true) { // 若匹配到目标状态,则isMatch = true,退出循环
					isMatch = true;
					break;
				}
			}

			// B operation
			CubeOperation(rc, new_rc, 'B');
			cantor = GetCantor(new_rc.value, fact);
			if (isFound[cantor] == false) {
				isFound[cantor] = true;
				que_rubicsCube.push(new_rc);
				if (IsMatch(value, new_rc) == true) {
					isMatch = true;
					break;
				}
			}

			// C operation
			CubeOperation(rc, new_rc, 'C');
			cantor = GetCantor(new_rc.value, fact);
			if (isFound[cantor] == false) {
				isFound[cantor] = true;
				que_rubicsCube.push(new_rc);
				if (IsMatch(value, new_rc) == true) {
					isMatch = true;
					break;
				}
			}
			
			que_rubicsCube.pop();
		}
		if (isMatch == true) { // 匹配到则输出结果
			RubicsCube rc = que_rubicsCube.back();
			cout << rc.step.length() << " ";
			cout << rc.step << endl;
		}
		else {	// 无法在规定步数内得到目标状态
			cout << "-1" << endl;
		}
	}
	return 0;
}