搜索与搜索剪枝
牛客网 2021秋季算法入门班第六章习题:搜索与搜索剪枝
1001 全排列
- DFS 回溯
#include <bits/stdc++.h>
using namespace std;
bool is[10] = {0} ;
int path[10];
void dfs(int n){
if(n == 8){
for(int i = 0 ; i < 8 ; i ++){
cout << path[i] << " " ;
}
cout << endl ;
}
for(int i = 1 ; i <= 8 ; i ++ ){
if(!is[i]){
path[n] = i ;
is[i] = true ;
dfs(n + 1);
is[i] = false ;
}
}
}
int main(){
dfs(0);
return 0 ;
}
1002 走出迷宫
- BFS
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<vector<int>> direction = {{1,0},{-1,0},{0,1},{0,-1}} ;
char ma[501][501] ;
queue<pair<int , int>> que ;
int visit[501][501] ;
memset(visit, 0, sizeof(visit)); // init
int n , m ;
while(cin >> n >> m){
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
cin >> ma[i][j] ;
if(ma[i][j] == 'S'){
que.push({i , j});
visit[i][j] = 1 ;
}
}
}
while(!que.empty()){
auto preque = que.front();
que.pop();
int x = preque.first , y = preque.second ;
for(int i = 0 ; i < 4 ; i ++){
int prex = direction[i][0] + x , prey = direction[i][1] + y ;
if(prex >= 0 && prex < n && prey >= 0 && prey < m && ma[prex][prey] == '.' && visit[prex][prey] == 0){
que.push({prex , prey});
visit[prex][prey] = 1 ;
}
if(ma[prex][prey] == 'E'){
cout << "Yes" << endl;
}
}
}
cout << "No" << endl;
}
return 0 ;
}
由于有多组输入,所以用 while 输入,不然会在第一组数据回城。
- 过了 75% 用例 BFS
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<vector<int>> direction = {{1,0},{-1,0},{0,1},{0,-1}} ;
char ma[501][501] ;
queue<pair<int , int>> que ;
int visit[501][501] ;
memset(visit, 0, sizeof(visit)); // init
int n , m ;
while(cin >> n >> m){
int flag = 0 ;
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
cin >> ma[i][j] ;
if(ma[i][j] == 'S'){
que.push({i , j});
visit[i][j] = 1 ;
}
}
}
while(!que.empty()){
auto preque = que.front();
que.pop();
int x = preque.first , y = preque.second ;
for(int i = 0 ; i < 4 ; i ++){
int prex = direction[i][0] + x , prey = direction[i][1] + y ;
if(prex >= 0 && prex < n && prey >= 0 && prey < m && ma[prex][prey] == '.' && visit[prex][prey] == 0){
que.push({prex , prey});
visit[prex][prey] = 1 ;
}
if(ma[prex][prey] == 'E'){
flag = 1 ;
}
if(flag == 1) break ;
}
}
if(flag == 1) cout << "Yes" << endl ;
else cout << "No" << endl;
}
return 0 ;
}
- AC BFS
#include <bits/stdc++.h>
using namespace std;
char mp[501][501];
int _next[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int visit[501][501];
void init(){
memset(visit,0,sizeof(visit));
}
int main(){
int n,m;
while(cin>>n>>m){
int bx,by;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>mp[i][j];
if(mp[i][j]=='S'){
bx=i;
by=j;
}
}
}
//cout<<bx<<" "<<by<<endl;
init();
queue<pair<int,int>>q;
q.push(make_pair(bx,by));
visit[bx][by]=1;
int flag=0;
while(!q.empty()){
pair<int,int>x=q.front();
q.pop();
for(int i=0;i<4;i++){
int c=x.first+_next[i][0];
int d=x.second+_next[i][1];
if(c<n&&d<m&&c>=0&&d>=0&&mp[c][d]=='.'&&!visit[c][d]){
q.push(make_pair(c,d));
visit[c][d]=1;
}
if(c<n&&d<m&&c>=0&&d>=0&&mp[c][d]=='E'){
flag=1;
break;
}
}
if(flag==1){
break;
}
}
if(flag==1){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}
1003 模拟战役
- BFS
#include<bits/stdc++.h>
using namespace std ;
vector<vector<int>> direction = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}} ;
int main(){ // BFS
int m ;
string s[10] ;
cin >> m ;
for(int i = 2 ; i <= 9 ; i ++){
cin >> s[i] ;
}
int sjtank = 0 , sj = 0 ;
for(int i = 2 ; i <= 5 ; i ++){ // siji
for(int j = 0 ; j < m ; j ++){
if(s[i][j] == '*'){
queue<pair<int,int>> que ;
que.push({i,j}) ;
s[i][j] = '.' ; sjtank ++ ;
while(!que.empty()){
auto tmp = que.front() ;
que.pop() ;
int x = tmp.first , y = tmp.second ;
for(int k = 0 ; k < 8 ; k ++){
int px = x + direction[k][0] , py = y + direction[k][1] ;
if(px >= 2 && px <= 5 && py >=0 && py < m && s[px][py] == '*'){
que.push({px,py}) ;
s[px][py] = '.' ;
sjtank ++ ;
}
}
}
sj ++ ;
}
}
}
int jjtank = 0 , jj = 0 ;
vector<int> vec ;
for(int i = 6 ; i <= 9 ; i ++){ // jiji
for(int j = 0 ; j < m ; j ++){
if(s[i][j] == '*'){
int pretank = 1 ;
queue<pair<int,int>> que ;
que.push({i,j}) ;
s[i][j] = '.' ; jjtank ++ ;
while(!que.empty()){
auto tmp = que.front() ;
que.pop() ;
int x = tmp.first , y = tmp.second ;
for(int k = 0 ; k < 8 ; k ++){
int px = x + direction[k][0] , py = y + direction[k][1] ;
if(px >= 6 && px <= 9 && py >=0 && py < m && s[px][py] == '*'){
que.push({px,py}) ;
s[px][py] = '.' ;
jjtank ++ ;
pretank ++ ;
}
}
}
vec.emplace_back(pretank) ;
jj ++ ;
}
}
}
int ans = 0 ;
if(sj > jj) {
cout << -1 << endl ;
return 0 ;
}
else{
sort(vec.begin() , vec.end()) ;
for(int x = sj - 1 ; x < jj ; x ++){
ans += vec[x] ;
}
cout << ans << endl ;
}
return 0 ;
}
可以用一个函数节省代码量。
1004 Jelly
- BFS 内存超限
#include<bits/stdc++.h>
using namespace std;
int main(){ // BFS
int n , ans = 0 , flag = 0 ;
cin >> n ;
int N = n * n ;
string s[N] ;
for(int i = 0 ; i < N ; i ++) cin >> s[i] ;
vector<vector<int>> direction = {{1,0},{-1,0},{0,1},{0,-1},{n,0},{-n,0}} ;
queue<pair<int ,int>> que ;
que.push({0,0});
while(!que.empty()){
int len = que.size() ;
while(len > 0){
auto tmp = que.front() ; que.pop() ;
int x = tmp.first , y = tmp.second ;
if(x == N - 1 && y == n - 1) {
flag = 1 ;
break;
}
int z = x / n ;
for(int i = 0 ; i < 4 ; i ++){
int px = x + direction[i][0] , py = y + direction[i][1];
if(px >= z * n && px < (z + 1) * n && py >= 0 && py < n && s[px][py] == '.'){
s[px][py] = '*' ;
que.push({px,py}) ;
}
}
for(int i = 4 ; i < 6 ; i ++){
int px = x + direction[i][0] ;
if(px >= 0 && px < N ){
s[px][y] = '*' ;
que.push({px,y}) ;
}
}
len -- ;
}
ans ++ ;
if(flag == 1) break ;
}
if(flag == 0) cout << -1 << endl ;
else cout << ans << endl ;
return 0;
}
看了一下所有过的提交,都是用三维数组 将下标通过结构体构造 放入queue中。
- 结构体 BFS
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
char m[N][N][N];
int dis[N][N][N];
typedef struct Edge
{
int a,b,c;
}ty;
int bfs()
{
queue<ty> q;
memset(dis, -1, sizeof dis);
q.push({1, 1, 1});
dis[1][1][1] = 1;
int dx[] = {0, 1, 0, -1 ,0 , 0}, dy[] = {1, 0, -1, 0, 0, 0}, dz[] = {0, 0, 0, 0, 1, -1};
while(q.size())
{
ty t = q.front();
q.pop();
for(int i = 0; i < 6; i++)
{
int x = t.a + dx[i], y = t.b + dy[i], z = t.c + dz[i];
if(x >= 1 && y >= 1 && z >= 1 && x <= n && y <= n && z <= n && dis[x][y][z] == -1 && m[x][y][z] != '*')
{
dis[x][y][z] = dis[t.a][t.b][t.c] + 1;
q.push({x, y, z});
}
}
}
return dis[n][n][n];
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
for(int k = 1; k <= n; k++)
cin >> m[i][j][k];
int t = bfs();
cout << t << endl;
return 0;
}
1005 [NOIP2014]寻找道路
- DFS 内存超限
#include <bits/stdc++.h>
using namespace std;
long long int n, m, cnt = 0 ;
vector<long long int> ans ;
int s, t ;
bool visit[10010][10010] = {0} ;
bool del[10010] = {0} ;
void dfs(int s){ // DFS
if(s == t) {
ans.emplace_back(cnt) ;
}
for(int i = 1 ; i <= n ; i ++){
if(visit[s][i] == true) {
visit[s][i] = false ;
cnt ++ ;
dfs(i);
visit[s][i] = true ;
cnt -- ;
}
}
}
void Del(int t){ // delete the wrong dots
//从t出发,所有与t相连的返回true,最后把false的路径删除
queue<int> que ;
que.push(t) ;
del[t] = true ;
while(!que.empty()){
int x = que.front() ;
que.pop() ;
for(int i = 1 ; i <= n ; i ++){
if(visit[i][t] == true){
del[i] = true ;
que.push(i) ;
}
}
}
for(int i = 1 ; i <= n ; i ++){
if(del[i] == false){
for(int j = 1 ; j <= n ; j ++){
visit[i][j] = false ;
visit[j][i] = false ;
}
}
}
}
int main(){
scanf("%lld%lld", &n, &m) ;
for(int i = 0 ; i < m ; i ++){
int x , y ;
scanf("%d%d", &x, &y) ;
visit[x][y] = true ;
}
scanf("%d%d", &s, &t);
Del(t);
dfs(s);
if(ans.empty()) cout << -1 << endl ;
else {
sort(ans.begin() , ans.end()) ;
printf("%lld", ans[0]) ;
}
return 0;
}
- BFS 能过用例了,但是只能过用例
#include <bits/stdc++.h>
using namespace std;
long long int n, m, cnt = 0 ;
int s, t, flag = 0 ;
bool visit[10010][10010] = {0} ;
void Bfs(int s){ // Bfs
queue<int> que ;
que.push(s) ;
while(!que.empty()){
int len = que.size() ;
while(len > 0){
int x = que.front();
if(x == t){
flag = 1 ;
return ;
}
que.pop() ;
for(int i = 1 ; i <= n ; i ++){
if(visit[x][i] == true){
que.push(i) ;
visit[x][i] = false ;
}
}
len -- ;
}
cnt ++ ;
}
}
void Del(int t){ // delete the wrong dots
//从t出发,所有与t相连的返回true,最后把false的路径删除
queue<int> que ;
bool del[10010] = {0} ;
que.push(t) ;
del[t] = true ;
while(!que.empty()){
int x = que.front() ;
que.pop() ;
for(int i = 1 ; i <= n ; i ++){
if(visit[i][x] == true){
del[i] = true ;
que.push(i) ;
}
}
}
for(int i = 1 ; i <= n ; i ++){
if(del[i] == false){
for(int j = 1 ; j <= n ; j ++){
visit[i][j] = false ;
visit[j][i] = false ;
}
}
}
}
int main(){
scanf("%lld%lld", &n, &m) ;
for(int i = 0 ; i < m ; i ++){
int x , y ;
scanf("%d%d", &x, &y) ;
visit[x][y] = true ;
}
scanf("%d%d", &s, &t);
Del(t);
Bfs(s);
cnt ++;
if(flag == 0) cout << -1 << endl ;
else printf("%lld", cnt) ;
return 0;
}
- AC
#include<bits/stdc++.h>
using namespace std;
int n,m;
int s,e;
int dis[10010];
int judge ;
bool arv[10010];
vector<int>v[10010],c[10010];
queue<int>q;
void bfs1(){
arv[e] = 1;
q.push(e);
while(!q.empty()){
int tmp = q.front();
q.pop();
for(int i = 0; i < c[tmp].size(); i++){
if(arv[c[tmp][i]]) continue;
arv[c[tmp][i]] = 1;
q.push(c[tmp][i]);
}
}
}
void bfs2(){
dis[s] = 0;
q.push(s);
while(!q.empty()){
int tmp = q.front();
q.pop();
judge = 0;
for(int i = 0;i < v[tmp].size();i++){
if(arv[v[tmp][i]] == 0){
judge = 1;
break;
}
}
for(int i = 0; i < v[tmp].size(); i++){
if(judge == 1) break;
if(dis[v[tmp][i]] == -1){
dis[v[tmp][i]] = dis[tmp] + 1;
q.push(v[tmp][i]);
}
}
}
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i = 1; i <= m; i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
c[y].push_back(x);
}
scanf("%d%d",&s,&e);
bfs1();
while(!q.empty()){
q.pop();
}
memset(dis,-1,sizeof(dis));
bfs2();
if(dis[e] == -1){
printf("-1");
}
else{
printf("%d",dis[e]);
}
}
行列分开记录,只记录有效位置,避开了原先的 10010*10010 个空间
1006 送外卖
- DFS 90%
#include<bits/stdc++.h>
using namespace std;
const int Maxsize = 100010 ;
int n ;
int a[Maxsize] , b[Maxsize] ;
bool visit[2][Maxsize] = {0} ; // 0-a, 1-b
string tmp = "";
vector<string> ans ;
int flag = 0 ; // flag == 1 : No flag == 2 : Inf
void dfs(int i){
if(i < 0 || i >= n) { flag = 1 ; return ; }
if(visit[0][i] && visit[1][i]) { flag = 2 ; return ;}
if(i == n - 1) {
ans.emplace_back(tmp) ;
return ;
}
if(!visit[0][i]){
visit[0][i] = true ;
tmp += "a" ;
dfs(i + a[i]) ;
visit[0][i] = false ;
tmp.pop_back() ;
}
if(!visit[1][i]){
visit[1][i] = true ;
tmp += "b" ;
dfs(i + b[i]) ;
visit[1][i] = false ;
tmp.pop_back() ;
}
}
int main(){
cin >> n ;
for(int i = 0 ; i < n ; i ++){
cin >> a[i] ;
}
for(int i = 0 ; i < n ; i ++){
cin >> b[i] ;
}
dfs(0) ;
if(ans.empty()){
if(flag == 1) cout << "No solution!" << endl ;
if(flag == 2) cout << "Infinity!" << endl ;
}
else cout << *min_element(ans.begin() , ans.end()) << endl ;
return 0 ;
}
有一个数据内存超限了。其他不好剪枝了,只能剪ans,不用vector存,直接判断。
- 不超限了,但是答案错了,还是 90%
#include<bits/stdc++.h>
using namespace std;
const int Maxsize = 100010 ;
int n ;
int a[Maxsize] , b[Maxsize] ;
bool visit[2][Maxsize] = {0} ; // 0-a, 1-b
string tmp = "";
string ans (Maxsize,'b') ;
string init (Maxsize,'b') ;
int flag = 0 ; // flag == 1 : No flag == 2 : Inf
void dfs(int i){
if(i < 0 || i >= n) { flag = 1 ; return ; }
if(visit[0][i] && visit[1][i]) { flag = 2 ; return ;}
if(i == n - 1) {
ans = min (ans , tmp) ;
return ;
}
if(!visit[0][i]){
visit[0][i] = true ;
tmp += "a" ;
dfs(i + a[i]) ;
visit[0][i] = false ;
tmp.pop_back() ;
}
if(!visit[1][i]){
visit[1][i] = true ;
tmp += "b" ;
dfs(i + b[i]) ;
visit[1][i] = false ;
tmp.pop_back() ;
}
}
int main(){
cin >> n ;
for(int i = 0 ; i < n ; i ++){
cin >> a[i] ;
}
for(int i = 0 ; i < n ; i ++){
cin >> b[i] ;
}
dfs(0) ;
if(ans == init){
if(flag == 1) cout << "No solution!" << endl ;
if(flag == 2) cout << "Infinity!" << endl ;
}
else cout << ans << endl ;
return 0 ;
}
- AC
#include<bits/stdc++.h>
using namespace std;
const int Maxsize = 100010 ;
int n ;
int a[Maxsize] , b[Maxsize] ;
bool visit[2][Maxsize] = {0} ; // 0-a, 1-b
string tmp = "";
string ans (Maxsize,'b') ;
string init (Maxsize,'b') ;
int flag = 0 ; // flag == 1 : No flag == 2 : Inf
void dfs(int i){
if(flag == 2) return ;
if(i < 0 || i >= n) { flag = 1 ; return ; }
if(visit[0][i] && visit[1][i]) { flag = 2 ; return ;}
if(i == n - 1) {
ans = min (ans , tmp) ;
return ;
}
if(!visit[0][i]){
visit[0][i] = true ;
tmp += "a" ;
dfs(i + a[i]) ;
visit[0][i] = false ;
tmp.pop_back() ;
}
if(!visit[1][i]){
visit[1][i] = true ;
tmp += "b" ;
dfs(i + b[i]) ;
visit[1][i] = false ;
tmp.pop_back() ;
}
}
int main(){
cin >> n ;
for(int i = 0 ; i < n ; i ++){
cin >> a[i] ;
}
for(int i = 0 ; i < n ; i ++){
cin >> b[i] ;
}
dfs(0) ;
if(ans == init){
if(flag == 1) cout << "No solution!" << endl ;
if(flag == 2) cout << "Infinity!" << endl ;
}
else cout << ans << endl ;
return 0 ;
}
加了一个if(flag == 2) return ;
,题意是去过的小区再去就算做infinity,所以这样判断会默许了一个小区去两次却不算重复,虽然AC,实则蒙过。大佬的AC:
#include<bits/stdc++.h>
using namespace std;
int a[100010],b[100010],n,flag = 0;
char s[100010];
int vis[100010],mp[100010];
bool dfs(int dep,int x)
{
if(dep < 1 || dep > n){
return false;
}
if(dep == n){
return true;
}
if(vis[dep]){
mp[dep] = 1;
return false;
}
vis[dep] = 1;
if(dfs(dep+a[dep],x+1)){
s[x] = 'a';
if(mp[dep]) flag = 1;
return true;
}
if(dfs(dep+b[dep],x+1)){
s[x] = 'b';
if(mp[dep]) flag = 1;
return true;
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i ++){
scanf("%d",&a[i]);
}
for(int i = 1;i <= n;i ++){
scanf("%d",&b[i]);
}
if(dfs(1,0)){
if(flag == 0) cout << s << endl;
else cout << "Infinity!" << endl;
}
else cout << "No solution!" << endl;
}
一个小区只做一个判断,逆推找结果。