#include<iostream>#include<utility>#include<queue>usingnamespace std;int n, m;
queue<pair<int,int>> q;int cx[4]={0,0,1,-1}, cy[4]={1,-1,0,0};bool vis[110][110], map[110][110], result;intmain(){
cin >> n >> m;for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){char c;
cin >> c;if(c =='.'){
map[i][j]=true;}elseif(c =='#'){
map[i][j]=false;}}}
q.push({1,1});while(!q.empty()){
pair<int,int> now = q.front();
q.pop();if(now.first == n && now.second == m){
result =true;break;}if(vis[now.first][now.second]){continue;}
vis[now.first][now.second]=true;for(int i =0; i <4; i++){int nowx = now.first + cx[i];int nowy = now.second + cy[i];if(nowx >=1&& nowx <= n && nowy >=1&& nowy <= m){if(map[nowx][nowy]){
q.push({nowx, nowy});}}}}if(result){
cout <<"Yes"<< endl;}else{
cout <<"No"<< endl;}return0;}
2. [洛谷P1706] 全排列问题
2-1. 题干
2-2. 解析
深搜,对于未使用的所有数字,若满足未超过数字范围与未使用过两个条件则进行递归
存储
递归后还原使用状态,超出数字范围则输出并跳出循环
2-3. 原码
#include<iostream>#include<iomanip>#include<functional>usingnamespace std;int n, num[20];bool vis[20];void(*op)(int)=[](int cnt){for(int i =1; i <= cnt; i++){
cout <<setw(5)<< std::right << num[i];}
cout << endl;};voiddfs(int cnt,int begin){if(cnt > n){op(n);return;}for(int i = begin +1; i <= n; i++){if(vis[i]){continue;}
vis[i]=true;
num[cnt]= i;dfs(cnt +1,0);
vis[i]=false;}}intmain(){
cin >> n;dfs(1,0);return0;}
3. [洛谷P1451] 求细胞数量
3-1. 题干
3-2. 解析
使用函数判断一个位置八向是否有棋子
深搜,对于坐标位置上下左右四个方向,若满足未超过范围、未使用过与函数三个条件则进行递归
将本位置周围不为0处标记为true
递归后还原使用状态,超出数字范围则输出并跳出循环
3-3. 原码
#include<iostream>usingnamespace std;int n, m, ans;int cx[]={0,0,1,-1}, cy[]={1,-1,0,0};bool vis[110][110], map[110][110];voiddfs(int x,int y){if(vis[x][y]){return;}
vis[x][y]=true;if(!map[x][y]){return;}for(int i =0; i <4; i++){int nowx = x + cx[i];int nowy = y + cy[i];if(nowx >=1&& nowx <= n && nowy >=1&& nowy <= m){if(map[nowx][nowy]){dfs(nowx, nowy);}}}}intmain(){
cin >> n >> m;for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){char c;
cin >> c;if(c =='0'){
map[i][j]=false;}else{
map[i][j]=true;}}}for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){if(map[i][j]&&!vis[i][j]){
ans++;}dfs(i, j);}}
cout << ans << endl;return0;}
4. [洛谷P1219] [USACO1.5] 八皇后 Checker Challenge
4-1. 题干
4-2. 解析
深搜,对于坐标位置上下左右四个方向,若满足未超过范围、未使用过与不为0三个条件则进行递归
记录本位置到数组
递归后还原使用状态,递归开始时计数器+1
4-3. 原码
#include<iostream>usingnamespace std;int n, num[20], ans;bool vis[20], vis2[20][20], map[20][20];void(*op)(int)=[](int cnt){
cout << num[1];for(int i =2; i <= cnt; i++){
cout <<" "<< num[i];}
cout << endl;};boolcheck(int x,int y){for(int i =1; i <= n; i++){if(vis2[x][i]|| vis2[i][y]){returnfalse;}int newy1 = y - x + i;if(newy1 >=1&& newy1 <= n){if(vis2[i][newy1]){returnfalse;}}int newy2 = y + x - i;if(newy2 >=1&& newy2 <= n){if(vis2[i][newy2]){returnfalse;}}}returntrue;}voiddsf(int cnt,int begin){if(cnt > n){
ans++;if(ans <=3){op(n);}return;}for(int i = begin +1; i <= n; i++){if(vis[i]){continue;}if(check(cnt, i)){
vis[i]=true;
vis2[cnt][i]=true;
num[cnt]= i;dsf(cnt +1,0);
vis[i]=false;
vis2[cnt][i]=false;}}}intmain(){
cin >> n;dsf(1,0);
cout << ans << endl;return0;}