###1:俊俊家里有矿,连通块问题,dfs经典入门例题,希望大家掌握。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100+8;
char oil[maxn][maxn]; // 油田
int vis[maxn][maxn]; // 标记数组 去重 判断结点是否被访问
int cnt; // 连通块数量
int n, m; // n行 m列
void dfs(int x, int y) // 深搜
{
if(x < 0 || x >= n || y < 0 || y >= m) return ; // 出界
if(oil[x][y] != '@' || vis[x][y]) return ; // 不是油田 已经访问
vis[x][y] = 1; // 标记结点已经被访问
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){ // 向八个方向搜索
if(i || j) dfs(x+i, y+j);
}
}
}
int main()
{
while(scanf("%d %d",&n, &m) == 2 && n && m){
memset(vis, 0, sizeof(vis)); // 初始化 每个结点都没有被访问
cnt = 0;
for(int i = 0; i < n; i++) scanf("%s", oil[i]);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(oil[i][j] == '@' && !vis[i][j]) { // 找到油田 而且没有被访问 才深搜
dfs(i, j);
cnt++;
}
}
}
printf("%d\n", cnt);
}
return 0;
}
###问题 B: 路痴的XX(搜索例题)BFS + 路径打印
题解:先广搜找到终点确定最短路径,然后回溯打印路径。BFS 经典题目 希望大家掌握。
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 10+8;
int a[maxn][maxn]; // 地图数组
int vis[maxn][maxn]; // 标记数组 去重 避免访问重复状态
int step[maxn][maxn]; // step[i][j] 表示起点(i, j)的最短路径
int dx[] = {-1, 1, 0, 0}; // 方向数组
int dy[] = {0, 0, -1, 1}; //
struct node{ // 用结点 表示 某个位置的状态
int x; // 行
int y; // 列
int cnt; // 走到当前位置的步数
node(int x = 0, int y = 0, int cnt = 0){ // 构造器初始化
this->x = x;
this->y = y;
this->cnt = cnt;
}
};
node fa[maxn][maxn]; // 父亲数组 用来回溯打印路径
int check(int x, int y) // 判断下一个结点能不能被访问
{
if(x < 0 || x > 4 || y < 0 || y > 4) return 0; //出界
if(a[x][y] == 1 || vis[x][y]) return 0; // 不能走 已经被走过
return 1;
}
void bfs()
{
memset(vis, 0, sizeof(vis)); // 初始化 每个位置都没有被走过
queue<node>q; // 队列
node now; // 初始化起点
now.x = 0, now.y = 0, now.cnt = 0;
q.push(now); // 起点入队
step[now.x][now.y] = 0;
while(!q.empty()){ // 当队列不空
now = q.front(); // 取队首
q.pop(); // 弹出队列
vis[now.x][now.y] = 1; // 标记被访问
step[now.x][now.y] = now.cnt; // 步数赋值
if(now.x == 4 && now.y == 4) return; // 找到终点
for(int i = 0; i < 4; i++){ // 向四个方向搜索
node next;
next.x = now.x + dx[i];
next.y = now.y + dy[i];
if(check(next.x, next.y)){ // 判断是否能走
next.cnt = now.cnt + 1; // 步数+1
q.push(next); // 结点入队
fa[next.x][next.y] = now; // now结点走到 next结点
} // 所以 next 的父亲 是 now
}
}
return ;
}
void print_path(node u)
{
stack<node>s; // 栈
for(;;){
s.push(u); // 入栈
if(step[u.x][u.y] == 0) break; // 找到起点
u = fa[u.x][u.y]; // 回溯
}
while(!s.empty()){ // 栈不空
printf("(%d, %d)\n", (s.top()).x, (s.top()).y); // 打印位置
s.pop(); // 出栈
}
return ;
}
int main()
{
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
scanf("%d", &a[i][j]); // 读入地图
}
}
bfs(); // 搜索
printf("%d\n",step[4][4]);
node ans(4, 4, step[4][4]); // 构造器初始化
print_path(ans);
return 0;
}
###问题 C: “下班啦,打卡成功!”
#####题解:结构体排序。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 200+8;
struct student{
string s;
int time;
};
student st[maxn];
int n;
bool cmp(student a, student b)
{
if(a.time > b.time || (a.time == b.time && a.s < b.s)) return 1;
return 0;
}
int main()
{
int h, m;
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; i++){
cin >> st[i].s;
scanf("%d:%d", &h, &m);
st[i].time = h*60 + m;
}
sort(st, st+n, cmp);
cout << st[0].s << endl;
}
return 0;
}
###问题 F: 搜索例题 倒水问题
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 100+1; // 108 TLE
int vis[maxn][maxn][maxn]; // 标记数组
int v[5];
struct cup{ // 结构体 记录状态
int v[5];
int cnt;
}temp;
void pour(int a, int b) // 将a杯中的水 倒入 b杯中
{
int sum = temp.v[a] + temp.v[b];
if(sum >= v[b]){
temp.v[b] = v[b];
}
else temp.v[b] = sum;
temp.v[a] = sum - temp.v[b];
}
void bfs()
{
queue<cup>q; // 声明队列
memset(vis, 0, sizeof(vis)); // 初始化
cup cur;
cur.v[1] = v[1];
cur.v[2] = 0;
cur.v[3] = 0;
cur.cnt = 0;
q.push(cur); // 入队
vis[v[1]][0][0] = 1;
while(!q.empty()){ // 队列非空
cur = q.front(); // 取队首
q.pop(); // 出队
if(cur.v[2] == 0 && cur.v[1] == cur.v[3]) { //找到目标状态
// printf("%d %d\n", cur.v[1], cur.v[3]);
printf("%d\n",cur.cnt);
return ;
}
for(int i = 1; i <= 3; i++){ // 互相倒水
for(int j = 1; j <= 3; j++){
if(i != j){ // 不能将水倒给自己
temp = cur;
pour(i, j); // 倒水
if(!vis[temp.v[1]][temp.v[2]][temp.v[3]]){ // 判重
temp.cnt++;
q.push(temp);
vis[temp.v[1]][temp.v[2]][temp.v[3]] = 1;
}
}
}
}
}
printf("NO\n");
return ;
}
int main()
{
while(scanf("%d %d %d", &v[1], &v[2], &v[3]) == 3 && v[1] && v[2] && v[3]){
if(v[2] > v[3]){ // 最后平分的水一定会在最大的两个杯子里里面
int t = v[2];
v[2] = v[3];
v[3] = t;
}
bfs();
}
return 0;
}
###问题 H: 八数码 (搜索例题) 状态空间搜索
//解法1: 用set 判重
#include<cstdio> // 用 set 判重
#include<cstring>
#include<set>
using namespace std;
typedef int State[9]; // 定义状态类型 定义了一个数组 别名为State
const int maxn = 1000000 + 8; // 状态太多 不开大数组 无法搜索
State st[maxn], goal; // 状态数组
int dist[maxn]; // 距离数组
set<int>vis; // set 判重 将九宫格映射到int
const int dx[] = {-1, 1, 0, 0}; // 方向
const int dy[] = {0, 0, -1, 1};
// BFS 返回目标状态在st数组中的下标
void init_lookup_table() // 初始化
{
vis.clear();
}
int try_to_insert(int s) // 状态下标
{
int v = 0;
for(int i = 0; i < 9; i++) v = v*10 + st[s][i];
if(vis.count(v)) return 0; // 判重
vis.insert(v);
return 1;
}
int bfs()
{
init_lookup_table(); // 初始化查找表
int front = 1, rear = 2; // 不使用下标 0 因为 0 被当作不存在
while(front < rear){ // 判断队列是否为空
State& s = st[front]; // 用引用 简化代码 起始状态
if(memcmp(goal, s, sizeof(s)) == 0) return front; // 找到目标状态
int z;
for(z = 0; z < 9; z++) if(!s[z]) break; // 找到 0 的 位置
int x = z/3, y = z%3; // 找到 行列
for(int d = 0; d < 4; d++){
int newx = x + dx[d];
int newy = y + dy[d];
int newz = newx*3 + newy;
if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3){
// 合法移动
State& t = st[rear]; // 声明应用 改变t 就会改变st[rear]
memcpy(&t, &s, sizeof(s)); // 整体复制 扩展新结点 内存拷贝
t[newz] = s[z]; // 移动 单个移动
t[z] = s[newz]; // 下一个状态
dist[rear] = dist[front] + 1; // 更新新结点距离值
if(try_to_insert(rear)) rear++; // 如果成功插入查找表 修改队尾指针
}
}
front++;
}
return 0;
}
int main()
{
for(int i = 0; i < 9; i++) scanf("%d", &st[1][i]);
for(int i = 0; i < 9; i++) scanf("%d", &goal[i]);
int ans = bfs();
if(ans > 0) printf("%d\n", dist[ans]);
else printf("-1\n");
return 0;
}
解法2: 用hash判重
#include<cstdio> // 用 hash 判重
#include<cstring>
using namespace std;
const int maxn = 1000000+8;
typedef int State[9];
State st[maxn], goal;
int dist[maxn];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int hashsize = 1000000+8;
int head[hashsize];
int next[hashsize];
void init_lookup_table()
{
memset(head, 0, sizeof(head));
}
int hash(State& s)
{
int v = 0;
for(int i = 0; i < 9; i++) v = v*10 + s[i];
return v%hashsize;
}
int try_to_insert(int s )
{
int h = hash(st[s]);
int u = head[h]; // 从表头开始查找链表
while(u){
if(memcmp(st[u], st[s], sizeof(st[s])) == 0) return 0;
u = next[u];
}
next[s] = head[h]; // 链表链接
head[h] = s;
return 1;
}
int bfs()
{
init_lookup_table();
int front = 1, rear = 2;
while(front < rear){
State& s = st[front];
if(memcmp(goal, s, sizeof(s)) == 0) return front;
int z;
for(z = 0; z < 9; z++) if(!s[z]) break;
int x = z/3, y = z%3;
for(int d = 0; d < 4; d++){
int newx = x + dx[d];
int newy = y + dy[d];
int newz = newx*3 + newy;
if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3 ){
State& t = st[rear];
memcpy(&t, &s, sizeof(s)); // 整体移动九宫格
t[newz] = s[z]; // 移动一次
t[z] = s[newz];
dist[rear] = dist[front] + 1;
if(try_to_insert(rear)) rear++;
}
}
front++;
}
return 0;
}
int main()
{
for(int i = 0; i < 9; i++) scanf("%d", &st[1][i]);
for(int i = 0; i < 9; i++) scanf("%d", &goal[i]);
int ans = bfs();
if(ans > 0) printf("%d\n", dist[ans]);
else printf("-1\n");
return 0;
}