110. 字符串接龙 (kamacoder.com)
思路:抽象为无向图,使用广度优先遍历,使用set集合记录待访问的字符串,map记录是否访问过,并且记录路径长度。然后遍历使用26个字符遍历每个位置,找到出现在set集合里面的字符串,就将该字符串加入队列
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std;
int main() {
string beginStr, endStr, str;
int n;
cin >> n;
unordered_set<string> strSet;
cin >> beginStr >> endStr;
for (int i = 0; i < n; i++) {
cin >> str;
strSet.insert(str);
}
// 记录strSet里的字符串是否被访问过,同时记录路径长度
unordered_map<string, int> visitMap; // <记录的字符串,路径长度>
// 初始化队列
queue<string> que;
que.push(beginStr);
// 初始化visitMap
visitMap.insert(pair<string, int>(beginStr, 1));
while(!que.empty()) {
string word = que.front();
que.pop();
int path = visitMap[word]; // 这个字符串在路径中的长度
// 开始在这个str中,挨个字符去替换
for (int i = 0; i < word.size(); i++) {
string newWord = word; // 用一个新字符串替换str,因为每次要置换一个字符
// 遍历26的字母
for (int j = 0 ; j < 26; j++) {
newWord[i] = j + 'a';
if (newWord == endStr) { // 发现替换字母后,字符串与终点字符串相同
cout << path + 1 << endl; // 找到了路径
return 0;
}
// 字符串集合里出现了newWord,并且newWord没有被访问过
if (strSet.find(newWord) != strSet.end()
&& visitMap.find(newWord) == visitMap.end()) {
// 添加访问信息,并将新字符串放到队列中
visitMap.insert(pair<string, int>(newWord, path + 1));
que.push(newWord);
}
}
}
}
// 没找到输出0
cout << 0 << endl;
}
105. 有向图的完全可达性 (kamacoder.com)
思路:深度优先遍历,使用邻接表建图,有两种写法第一种处理当前的结点,第二种处理下一个结点
#include<bits/stdc++.h>
using namespace std;
void dfs(vector<list<int>>& g,int key,vector<bool>& vis){
if(vis[key]){
return;
}
vis[key]=true;
list<int>keys=g[key];
for(int key:keys){
dfs(g,key,vis);
}
}
int main(){
int n,m,s,t;
cin>>n>>m;
vector<list<int>>g(n+1);
while(m--){
cin>>s>>t;
g[s].push_back(t);//邻接表
}
vector<bool>vis(n+1,false);
dfs(g,1,vis);
for(int i=1;i<=n;i++){
if(vis[i]==false){
cout<<-1<<endl;
return 0;
}
}
cout<<1<<endl;
}
#include <iostream>
#include <vector>
#include <list>
using namespace std;
void dfs(const vector<list<int>>& graph, int key, vector<bool>& visited) {
list<int> keys = graph[key];
for (int key : keys) {
if (visited[key] == false) { // 确认下一个是没访问过的节点
visited[key] = true;
dfs(graph, key, visited);
}
}
}
int main() {
int n, m, s, t;
cin >> n >> m;
vector<list<int>> graph(n + 1);
while (m--) {
cin >> s >> t;
graph[s].push_back(t);
}
vector<bool> visited(n + 1, false);
visited[1] = true; // 节点1 预先处理
dfs(graph, 1, visited);
for (int i = 1; i <= n; i++) {
if (visited[i] == false) {
cout << -1 << endl;
return 0;
}
}
cout << 1 << endl;
}
106. 岛屿的周长 (kamacoder.com)
思路:每个单元格的周长为4,只要两个单元格相邻周长需要减2。算出所有岛屿的的个数然后减去相邻的个数,就是周长。sum*4-cover*2
#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={0,1,1,0,-1,0,0,-1};
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>>g(n,vector<int>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>g[i][j];
}
}
int sum=0;
int cover=0;
//每个单元格的周长为4,只要两个单元格相邻周长需要减2
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]==1){
sum++;
if(i-1>=0&&g[i-1][j]==1){//上方是否相邻
cover++;
}
if(j-1>=0&&g[i][j-1]==1){//左边是否相邻
cover++;
}
//不判断下和右是为了避免重复计算
}
}
}
cout<<sum*4-cover*2<<endl;
}
总结
遇到图的问题一般都dfs和bfs的题,但是有一部分可以不用dfs或者bfs来做,多思考避免出现思维定式