Esempio n. 1
0
 vector<int> searchRange(int A[], int n, int target) {
     vector <int> ans;
     int begin = findBegin(A, n, target);
     int end = findEnd(A, n, target);
     if (begin <= end) {
         ans.push_back(begin);
         ans.push_back(end);
     } else {
         ans.push_back(-1);
         ans.push_back(-1);
     }
     return ans;
 }
stack<Position> findString(string str, int row, int col, char** mtx)
{
  Position up = {0, -1};
  Position right = {1, 0};
  Position down = {0, 1};
  Position left = {-1,0};
  Position dir[] = {up, right, down, left};
 
  int **visited;
  visited = new int *[row];
  for(int i = 0; i <row; i++)
    visited[i] = new int[col];
	
  // int visited[row][col];
  for(int i=0; i<row; i++)
  {
    for(int j=0; j<col; j++)
	  visited[i][j]=0;
  }
  
  stack<Position> path;
  
  int len = str.length();
  Position m = findBegin(mtx, visited, row, col, str[0]);
  if( m.x!=-1 && m.y!=-1)
    path.push(m);
	
  std::cout<<"Ying Liu.....1..."<<path.empty()<<"\n";
  for(int i=1; i<len; i++)
  {    std::cout<<"Ying Liu.....2\n";
    bool flag = false;
    Position mm = path.top();
    for(int j=0; j<4; j++)
	   {
		 int x = mm.x+ dir[j].x;
		 int y = mm.y+ dir[j].y;
		 std::cout<<"Original_x...."<<mm.x<<"...Original_y..."<< mm.y<<"\n";
		 std::cout<<"x...."<<x<<"...y..."<< y<<"\n";
		 if(x>=0&& x< row && y>=0 && y< col && visited[x][y] == 0 && str[i]==mtx[x][y])
		    {    std::cout<<"Ying Liu.....3\n";
			   Position tmp;
			   tmp.x = x;
			   tmp.y = y;
			   path.push(tmp);
			   visited[x][y] = 1;
			   flag = true;
			   
			   cout<<"i....."<<i<<"...str[i]..."<<str[i]<<"\n";
			}
         std::cout<<"j....."<<j<<"\n";			
	   }
	 std::cout<<"Ying Liu....7\n";
	 if(flag==false && !path.empty())
	  {  std::cout<<"Ying Liu....6\n";
	    Position mn = path.top();
		path.pop();
		visited[mn.x][mn.y] =0;
		i--;
	  }
	if(path.empty())
	{
	  i = 1;
	  Position n = findBegin(mtx, visited, row, col, str[0]);
      if( n.x!=-1 && n.y!=-1)
      path.push(n);
	}
  }
  std::cout<<"Ying Liu....8\n";
  return path;
}