My c++ accepted code!


  • 0
    Z

    class Solution {
    public:

    bool vecForCom(vector<int>row, vector<int>col, int i)

    {
    for (int j = 0; j != i; ++j)
    {
    if (row[i] == row[j] && col[i] == col[j])
    {
    return(1);//有重复位置则为1
    }
    }
    return(0);

    }

    int decode(int in, bool yn)
    {
    if (yn == 1)
    {
    if (in == 1 || in == 3)
    return(0);
    else if (in == 2)
    return(1);
    else
    return(-1);
    }
    if (yn ==0)
    {
    if (in == 2 || in ==4 )
    return(0);
    else if (in == 1 )
    return(1);
    else
    return(-1);
    }

    }
    bool surroundfind(vector<vector<char>>&table, char C, int row, int col, int dir)
    {
    int roww = table.size(), coll = table[0].size();
    switch (dir)
    {
    case 1:
    row = row;
    col = col + 1;
    break;

    case 2:
    	row = row+1;
    	col = col;
    	break;
    
    case 3:
    	row = row;
    	col = col - 1;
    	break;
    case 4:
    	row = row-1;
    	col = col ;
    	break;
    
    }
    if (row < 0 || row >= roww || col < 0 || col >= coll || table[row][col] != C)
    	return(0);
    else
    	return(1);
    

    }

    vector<int> *findchar(vector<vector<char>>&table, char C)
    {

    int posArray[2];
    
    vector<int> *posp = new vector<int>;
    
    posArray[0] = table.size();
    posArray[1] = table[0].size();
    
    for (int row = 0; row != posArray[0]; ++row)
    {
    	for (int col = 0; col != posArray[1]; ++col)
    	{
    		if (table[row][col] == C)
    		{
    			posp->push_back(row);
    			posp->push_back(col);
    
    		}
    	}
    }
    
    
    return(posp);
    

    }
    bool exist(vector<vector<char> > &board, string word)
    {

        int wordSize = word.size();
    
    vector<int> count(wordSize,0);
    vector<int> row(wordSize), col(wordSize);
    for (int i = 0; i != wordSize; ++i)
    {  
    	
    	char wordChar = word[i];
    	if (i == 0)
    	{
    		vector<int> position = *findchar(board, wordChar);
    		if (position.empty())
    			return(false);
    		else
    		{
                if (wordSize == 1)
    				return(true);
    			
    			if ((1 + count[i]) >= position.size())
    				return(false);
    			//cout << position[0];
    			row[i] = position[0+count[i]];
    			col[i] = position[1+count[i]];
    			count[i] +=2;
    			count[i+1]=0;
    
    			continue;
    		}
    	}
    	int con=0;
    	for (int j = 1+count[i]; j <= 4; ++j)
    	{
            con=1;
    
    		if (surroundfind(board, wordChar, row[i-1], col[i-1], j))
    		{
    			
    			row[i] = row[i-1]+decode(j, 1);
    			col[i] = col[i-1] + decode(j, 0);
    			count[i] += j;
    			if (vecForCom(row,col,i))
    				{if (j < 4)
    				continue;
    			else
    			{
    				i -= 2;
    				break;
    			}
    				}
    
    			if (i == wordSize - 1)
    
    				return(true);
    				count[i+1]=0;
    				break;
    			
    		}
    		else if (j == 4)
    			{	i -= 2;}
    
    	}
    	if(con==0)
    	{
    	  if(1+count[i]>4)
     	   i-=2;
        }
    }
    return (false);
    
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.