简单搜索题解

A - A

#include<iostream>
#include<cstring>
using namespace std;
int a[10];
char map[10][10];
int cnt=0;
int sum=0;
int n,k;
void dfs(int s){
	int i;
	if(cnt==k){
		sum++;
		return;
	}
	else{
		if(s>=n){
			return;	
		}
		else{
			for(int i=0;i<n;i++){
			if(map[s][i]=='#'&&!a[i]){
				a[i]=1;
				cnt++;
				dfs(s+1);
				cnt--;
				a[i]=0;
			    }
		    }
		    dfs(s+1);
		}		
	}
}
int main(){
	int i;
	while(cin>>n>>k){
		if(n==-1&&k==-1){
			break;
		}
		memset(a,0,sizeof(a));
		for(i=0;i<n;i++){
			cin>>map[i];
		}
		cnt=sum=0;
		dfs(0);
		cout<<sum<<endl;
	}
	
	return 0;
}

B - B

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int sx,sy,sz;
int ex,ey,ez;
char map[35][35][35];
int vis[35][35][35];
int L,R,C;
int dir[6][3]={{0,1,0},{0,-1,0},{1,0,0,},{-1,0,0},{0,0,1},{0,0,-1}};
struct node{
    int x;
    int y;
    int z;
    int step;
    int num[1000];
};
node a,b;
node bfs(){
	memset(vis,0,sizeof(vis));
	queue<node>q;
	a.x=sx;
	a.y=sy;
	a.z=sz;
	a.step=0;
	vis[sz][sx][sy]=1;
	q.push(a);
	while(!q.empty()){
		a=q.front();
		q.pop();
		if(a.x==ex&&a.y==ey&&a.z==ez){
			return a;
		}
		for(int i=0;i<6;i++){
			b=a;
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
			b.z=a.z+dir[i][2];
			if(vis[b.z][b.x][b.y]||map[b.z][b.x][b.y]=='#'){
				continue;
			} 
			if(b.x>=0&&b.x<R&&b.y>=0&&b.y<C&&b.z>=0&&b.z<L){
				vis[b.z][b.x][b.y]=1;
				b.step=a.step+1;
				b.num[a.step]=i;
				q.push(b);
			}
		}
		//cout<<a.step<<'!'<<endl;
	}
	//cout<<"Escaped in "<<a.step+1<<" minute(s)."<<endl;
	return a;
}
int main(){
	while(cin>>L>>R>>C){
		if(L==0&&R==0&&C==0){
			break;
		}
		else{
			for(int i=0;i<L;i++){
				for(int j=0;j<R;j++){
					for(int k=0;k<C;k++){
						cin>>map[i][j][k];
						if(map[i][j][k]=='S'){
							sz=i;
							sx=j;
							sy=k;
						}
						if(map[i][j][k]=='E'){
							ez=i;
							ex=j;
							ey=k;
						}
					}
				}
	       }
		}
        bfs();
        if(a.step==0){
        	cout<<"Trapped!"<<endl;
        }
        else{
        	cout<<"Escaped in "<<a.step<<" minute(s)."<<endl;
        }		
	}
	
	
} 

C - C

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct node{
	int x;
	int step;
}st;
queue<node>q;
int vis[200000];
int n,m;
int bfs(){
	while(!q.empty()){
		q.pop();
	}
	memset(vis,0,sizeof(vis));
	vis[st.x]=1;
	q.push(st);
	while(!q.empty()){
		node now=q.front();
		if(now.x==m){
			return now.step;
		}
		q.pop();
		for(int j=0;j<3;j++){
			node next=now;
			if(j==0){
				next.x=next.x+1;
			}
			else if(j==1){
				next.x=next.x-1;
			}
			else if(j==2){
				next.x=next.x*2;
			}
			++next.step;
			if(next.x==m){
				return next.step;
			}
			if(next.x>=0&&next.x<=200000&&!vis[next.x]){
				vis[next.x]=1;
				q.push(next);
			} 
		}
	}
	return 0;
}


int main(){
	while(cin>>n>>m){
		st.x=n;
		st.step=0;
		cout<<bfs()<<endl; 
	}
}

D - D

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int a1[6][6];
int vis[6][6];
struct node
{
    int x;
    int y;
    int step;
    int num[50];
};
node ans;
int i,j,len;
int dis[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int x1=0;
int y1=0;
node  bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node a,b;
    a.x=0;
    a.y=0;
    a.step=0;
    vis[0][0]=1;
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(a.x==4&&a.y==4)
        {
           // len=a.step;
            //prinf(a);
            return a;
        }
        for(i=0;i<4;i++)
        {
            b=a;
            b.x=a.x+dis[i][0];
            b.y=a.y+dis[i][1];
            if(vis[b.x][b.y]||a1[b.x][b.y]==1)
                continue;
            if(b.x>=0&&b.x<5&&b.y>=0&&b.y<5)
            {
                 vis[b.x][b.y]=1;
                b.step=a.step+1;
                //printf(" %d ",b.step); 
                b.num[a.step]=i;
                //printf("%d\n",b.num[a.step]);
                q.push(b);

            }
        }
        //printf("\n");
    }
    return a;
}
void prinf(node w)
{
    for(i=0;i<=w.step;i++)
    {

        printf("(%d, %d)\n",x1,y1);
        if(i!=w.step)
        {
            x1+=dis[w.num[i]][0];
            y1+=dis[w.num[i]][1];
        }
    }
}
int main()
{
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            scanf("%d",&a1[i][j]);
        }
    }
    prinf(bfs());
    return 0;
}

E - E

#include<iostream>
using namespace std;
int n;
int flag;
void dfs(int num,long long ans){
	if(flag==1||num>19){
		return;
	}
	if(ans%n==0){
		flag=1; 
		cout<<ans<<endl;
		return;
	}
	dfs(num+1,ans*10);
	dfs(num+1,ans*10+1);
}
int main(){
	 while(cin>>n){
	 	flag=0;
	 	if(n==0){
	 		break;
	 	}
	 	dfs(1,1);
	 }
	 return 0;
}

F - F

#include <iostream>
#include<stdio.h>
#include <string.h>
using namespace std;
char map[100][100]={};
int n,m;
void dfs(int x,int y)
{
    int a,b;
    int dir[8][2]={1,1, 1,-1, -1,1, -1,-1, 0,1, 0,-1, 1,0, -1,0};
    for(int i=0;i<8;i++)                                  
    {                                                 
     a=x+dir[i][0];                                   
     b=y+dir[i][1];                                   
     if(a<0||b<0||a>=n||b>=m||map[a][b]=='*')    
         continue;          //若该点不可行或越界,跳该点                            
     map[a][b]='*';       //此时该点可行,并把它标记为*,再向8个方向深搜
     dfs(a,b);                                        
    }
}                                                 
int main(int argc, char *argv[])
{
    while(cin>>n>>m)
    {   
        int ans=0;
        if(n==0||m==0)  break;      
        for(int i=0;i<n;i++)           
            scanf("%s",map[i]);       

        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(map[i][j]=='@')
                {
                dfs(i,j);
                ans++;          
                }

        cout<<ans<<endl;    
    }   
    return 0;
}

G - G

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
 
int n, m;
const int N = 1e4 + 100;
int vis[N];
struct node
{
    int x, step;
};
queue<node> Q;
 
bool judge_prime(int x) //判断素数
{
    if(x == 0 || x == 1)
        return false;
    else if(x == 2 || x == 3)
        return true;
    else
    {
        for(int i = 2; i <= (int)sqrt(x); i++)
            if(x % i == 0)
                return false;
        return true;
    }
}
 
void BFS()
{
    int X, STEP, i;
    while(!Q.empty())
    {
        node tmp;
        tmp = Q.front();
        Q.pop();
        X = tmp.x;
        STEP = tmp.step;
        if(X == m)
        {
            printf("%d\n",STEP);
            return ;
        }
        for(i = 1; i <= 9; i += 2) //个位
        {
            int s = X / 10 * 10 + i;
            if(s != X && !vis[s] && judge_prime(s))
            {
                vis[s] = 1;
                node temp;
                temp.x = s;
                temp.step = STEP + 1;
                Q.push(temp);
            }
        }
        for(i = 0; i <= 9; i++) //十位
        {
            int s = X / 100 * 100 + i * 10 + X % 10;
            if(s != X && !vis[s] && judge_prime(s))
            {
                vis[s] = 1;
                node temp;
                temp.x = s;
                temp.step = STEP + 1;
                Q.push(temp);
            }
        }
        for(i = 0; i <= 9; i++) //百位
        {
            int s = X / 1000 * 1000 + i * 100 + X % 100;
            if(s != X && !vis[s] && judge_prime(s))
            {
                vis[s] = 1;
                node temp;
                temp.x = s;
                temp.step = STEP + 1;
                Q.push(temp);
            }
        }
        for(i = 1; i <= 9; i++) //千位
        {
            int s = i * 1000 + X % 1000;
            if(s != X && !vis[s] && judge_prime(s))
            {
                vis[s] = 1;
                node temp;
                temp.x = s;
                temp.step = STEP + 1;
                Q.push(temp);
            }
        }
    }
    printf("Impossible\n");
    return ;
}
 
int main()
{
    int t, i;
    scanf("%d",&t);
    while(t--)
    {
        while(!Q.empty()) Q.pop();
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        vis[n] = 1;
        node tmp;
        tmp.x = n;
        tmp.step = 0;
        Q.push(tmp);
        BFS();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Helloirbd/article/details/81137883