1) word ladder
描述
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence
from start to end, such that:
• Only one leer can be changed at a time
• Each intermediate word must exist in the dictionary
For example, Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is ”hit” -> ”hot” -> ”dot” -> ”dog” -> ”cog”, return its length 4.
Note:
• Return 0 if there is no such transformation sequence.
• All words have the same length.
• All words contain only lowercase alphabetic characters.
// OOD.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
int ladderlength(string start, string end, map<string,int> &dict)
{
queue<string> cur;
int length = 0;
cur.push(start);
string nullstring = "";
cur.push(nullstring);
map<string,int> visited;
visited[start]++;
int i,j;
char tmpc;
bool findsign = false;
while(!cur.empty() && !findsign)
{
string curstring = cur.front();
cur.pop();
if(curstring == nullstring && !cur.empty())
{
length++;
cur.push(nullstring);
}
else
{
for(i = 0; i < curstring.size(); i++)
{
for(tmpc = 'a'; tmpc <= 'z'; tmpc++)
{
swap(curstring[i],tmpc);
if(curstring == end)
{
length++;
findsign = true;
return length;
}
if(dict.find(curstring)!=dict.end() && visited.find(curstring) == visited.end())
{
cur.push(curstring);
visited[curstring]++;
}
swap(curstring[i],tmpc);
}
}
}
}
if(!findsign)
return -1;
else
return length;
}
int main()
{
string start = "hit";
string end = "cog";
map<string, int> dict;
string M[]={"hot","dot","dog","lot","log"};
for(int i = 0; i < sizeof(M)/sizeof(string); i++)
{
dict[M[i]]++;
}
int r = ladderlength(start,end,dict);
cout << "Change from "<< start << " to " << end << " needs "<<r<< " steps!"<<endl;
system("pause");
return 0;
}
2) word ladder ||
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
void printpath(map<string, vector<string> >father,string start,string end,vector<string> &path, vector<vector<string>> &result)
{
if(start == end)
{
path.push_back(start);
result.push_back(path);
path.pop_back();
return;
}
vector<string> cur = father[end];
path.push_back(end);
for(int i = 0; i < cur.size(); i++)
{
printpath(father,start,cur[i],path,result);
}
path.pop_back();
}
void buildpath(map<string,vector<string>> father, string start, string end)
{
vector<string> path;
vector<vector<string>> result;
printpath(father,start,end,path,result);
for(int i = 0; i < result.size(); i++)
{
cout << "[ ";
for(int j = result[i].size()-1; j >= 0; j--)
cout << result[i][j] << " ";
cout << " ]" << endl;
}
return;
}
void ladderlength(string start, string end, map<string,int> &dict)
{
queue<string> cur;
int length = 0;
cur.push(start);
string nullstring = "";
cur.push(nullstring);
map<string,int> visited;
map<string,vector<string>> father;
visited[start]++;
int i,j;
char tmpc;
bool findsign = false;
while(!cur.empty())
{
string curstring = cur.front();
cur.pop();
if(curstring == nullstring && !cur.empty())
{
length++;
cur.push(nullstring);
}
else
{
for(i = 0; i < curstring.size(); i++)
{
string prestring = curstring;
for(tmpc = 'a'; tmpc <= 'z'; tmpc++)
{
swap(curstring[i],tmpc);
if(curstring == end)
{
father[curstring].push_back(prestring);
length++;
findsign = true;
}
if(dict.find(curstring)!=dict.end() && visited.find(curstring) == visited.end())
{
father[curstring].push_back(prestring);
cur.push(curstring);
visited[curstring]++;
}
swap(curstring[i],tmpc);
}
}
}
}
if(!findsign)
{
cout << " NOT FIND a path!!!"<<endl;
return;
}
else
{
//print the result;
cout << "find!"<<endl;
cout << "The Dict is:------------------------ "<<endl;
map<string, int>::iterator dictitr;
cout << " [ ";
for(dictitr = dict.begin();dictitr!=dict.end();dictitr++)
{
cout << (*dictitr).first<< " ";
}
cout << " ]"<< endl;
cout << "Change From "<<start << " to " << end << " needs following ways "<<endl;
buildpath(father,start,end);
}
}
int main()
{
string start = "hit";
string end = "cog";
map<string, int> dict;
string M[]={"hot","dot","dog","lot","log"};
for(int i = 0; i < sizeof(M)/sizeof(string); i++)
{
dict[M[i]]++;
}
ladderlength(start,end,dict);
system("pause");
return 0;
}
3) surrounded region
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region .
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct position
{
int x;
int y;
position(int px, int py)
{
x = px;
y = py;
}
};
void print(vector<vector<char>> &board)
{
for(int i = 0; i < board.size(); i++)
{
for(int j = 0; j <board[0].size(); j++)
{
cout<<board[i][j] << " ";
}
cout<< endl;
}
}
bool isvalid(int x, int y, vector<vector<char>> &board)
{
if(x < 0 || y < 0 || x >= board.size()||y >= board[0].size())
return false;
if(board[x][y] == '0')
return true;
else
return false;
}
void SurroundRegion(vector<vector<char>>&board)
{
cout<<"The input board is: " << endl;
print(board);
queue<position> zerosqueue;
int height,width;
height = board.size();
width = board[0].size();
position tmp(-1,-1);
int i,j;
for( i = 0; i < height; i++)
{
if(board[i][0] == '0')
{
tmp = position(i,0);
zerosqueue.push(tmp);
}
if(board[i][width-1] == '0')
{
tmp = position(i,width-1);
zerosqueue.push(tmp);
}
}
for( j = 0 ; j < width; j++)
{
if(board[0][j] == '0')
{
tmp = position(0,j);
zerosqueue.push(tmp);
}
if(board[height-1][j] == '0')
{
tmp = position(height-1, j);
zerosqueue.push(tmp);
}
}
while(!zerosqueue.empty())
{
tmp = zerosqueue.front();
zerosqueue.pop();
if(board[tmp.x][tmp.y] != '0')
continue;
board[tmp.x][tmp.y] = '#'; // visited
if(isvalid(tmp.x-1,tmp.y,board))
{
zerosqueue.push(position(tmp.x-1,tmp.y));
}
if(isvalid(tmp.x+1,tmp.y,board))
{
zerosqueue.push(position(tmp.x+1,tmp.y));
}
if(isvalid(tmp.x,tmp.y-1,board))
{
zerosqueue.push(position(tmp.x,tmp.y-1));
}
if(isvalid(tmp.x,tmp.y+1,board))
{
zerosqueue.push(position(tmp.x,tmp.y+1));
}
}
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{
if(board[i][j] == '#')
board[i][j] = '0';
else if(board[i][j] == '0')
board[i][j] = 'X';
}
cout << "The final result is: "<<endl;
print(board);
}
int main()
{
vector<vector<char>> board(4, vector<char>(4));
char M[4][4] = {{'X','X','X','X'},{'X','0','0','X'},{'X','X','0','X'},{'X','0','X','X'}};
for(int i = 0; i < 4; i++)
board[i].assign(M[i],M[i]+4);
SurroundRegion(board);
system("pause");
return 0;
}
4) 迷宫:
描述:
一个迷宫由一个 01 矩阵表示,每个单元格要么是空地(用 0 表示),要么是障碍物(用 1 表示)。你的任务是找到一条从入口到出口的最短路径。任何时候都不能在障碍物格子中,也不能走到迷宫之外。只能横着走或竖着走,不能斜着走。数据保证有唯一解。
输入:
一个 5 5 的二维数组
输出:
左上角到右下角的最短路径
样例输入
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct position
{
int x;
int y;
position()
{
x = -1;
y = -1;
}
position(int px, int py)
{
x = px;
y = py;
}
};
bool isvalid(int x, int y, vector<vector<int>> &board)
{
if(x< 0 || y < 0 ||x >= board.size() || y >= board[0].size())
return false;
if(board[x][y] == 0)
return true;
else
return false;
}
void printpath(map<int,vector<int>> father, int start, int end, vector<int>&path, vector<vector<int>>&result)
{
if(start == end)
{
//path.push_back(end);
result.push_back(path);
//path.pop_back();
return;
}
vector<int> cur = father[end];
if(cur.size() < 1)
return;
for(int i = 0; i<cur.size(); i++)
{
path.push_back(cur[i]);
printpath(father,start,cur[i],path,result);
path.pop_back();
}
}
void buildpath(map<int, vector<int>>father,int start, int end, int height, int width)
{
vector<int> path;
vector<vector<int>> result;
printpath(father,start,end,path,result);
for(int i = 0; i < result.size(); i++)
{
for(int j = result[i].size()-1; j>=0; j--)
{
int tmpx = result[i][j]/width;
int tmpy = result[i][j]%width;
cout << "(" << tmpx << ","<< tmpy << ")---";
}
cout << "(" << height-1 << ","<<width-1 << ")" << endl;
}
return;
}
void print(vector<vector<int>> &board)
{
for(int i = 0; i < board.size(); i++)
{
for(int j = 0; j < board[i].size(); j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
void maze(vector<vector<int>> &board)
{
cout << "the original board is " << endl;
print(board);
queue<position> posqueue;
position start(0,0);
int height = board.size();
int width = board[0].size();
map<int,vector<int>> father;
posqueue.push(start);
bool findsign = false;
while(!posqueue.empty())
{
position tmp = posqueue.front();
int tmpint = tmp.x*width+tmp.y;
posqueue.pop();
board[tmp.x][tmp.y] = 2; // visited
if(tmp.x == height-1 && tmp.y == width-1)
{
findsign = true;
}
if(isvalid(tmp.x+1, tmp.y, board))
{
posqueue.push(position(tmp.x+1,tmp.y));
int posint = (tmp.x+1)*width + tmp.y;
father[posint].push_back(tmpint);
}
if(isvalid(tmp.x-1, tmp.y, board))
{
posqueue.push(position(tmp.x-1,tmp.y));
int posint = (tmp.x-1)*width + tmp.y;
father[posint].push_back(tmpint);
}
if(isvalid(tmp.x, tmp.y+1, board))
{
posqueue.push(position(tmp.x,tmp.y+1));
int posint = tmp.x*width + tmp.y+1;
father[posint].push_back(tmpint);
}
if(isvalid(tmp.x, tmp.y-1, board))
{
posqueue.push(position(tmp.x,tmp.y-1));
int posint = tmp.x*width + tmp.y-1;
father[posint].push_back(tmpint);
}
}
if(findsign)
{
cout << "The path from (0,0) to (" <<height-1 << ","<<width-1<<") is "<<endl;
buildpath(father, 0,height*width-1,height,width);
}
return;
}
int main()
{
const int N = 5;
vector<vector<int>> board(N, vector<int>(N));
int M[N][N] = {{0, 1, 0, 0, 0},{0, 1, 0, 1, 0},{0, 0, 0, 0, 0},{0, 1,1, 1, 0},{0, 0, 0, 1, 0}};
for(int i = 0; i < N; i++)
board[i].assign(M[i],M[i]+N);
maze(board);
system("pause");
return 0;
}